22//! Context storage backends
33
44use crate :: context:: Context ;
5- use crate :: state:: { RepoState , SessionState } ;
5+ use crate :: state:: RepoState ;
66use crate :: { ContextError , Result } ;
77use std:: path:: { Path , PathBuf } ;
88use tracing:: { debug, info} ;
@@ -22,6 +22,8 @@ impl ContextStorage {
2222 }
2323
2424 /// Create storage in default location (~/.gitbot-fleet)
25+ // Fallible (`Result`), so it cannot implement the infallible std `Default` trait; name kept as public API.
26+ #[ allow( clippy:: should_implement_trait) ]
2527 pub fn default ( ) -> Result < Self > {
2628 let home = std:: env:: var ( "HOME" ) . map_err ( |_| {
2729 ContextError :: InvalidState ( "HOME environment variable not set" . to_string ( ) )
@@ -86,12 +88,11 @@ impl ContextStorage {
8688 for entry in std:: fs:: read_dir ( & sessions_dir) ? {
8789 let entry = entry?;
8890 let path = entry. path ( ) ;
89- if path. extension ( ) . map ( |e| e == "json" ) . unwrap_or ( false ) {
90- if let Some ( stem) = path. file_stem ( ) . and_then ( |s| s. to_str ( ) ) {
91- if let Ok ( id) = uuid:: Uuid :: parse_str ( stem) {
92- sessions. push ( id) ;
93- }
94- }
91+ if path. extension ( ) . map ( |e| e == "json" ) . unwrap_or ( false )
92+ && let Some ( stem) = path. file_stem ( ) . and_then ( |s| s. to_str ( ) )
93+ && let Ok ( id) = uuid:: Uuid :: parse_str ( stem)
94+ {
95+ sessions. push ( id) ;
9596 }
9697 }
9798
@@ -152,10 +153,10 @@ impl ContextStorage {
152153 for entry in std:: fs:: read_dir ( & repos_dir) ? {
153154 let entry = entry?;
154155 let path = entry. path ( ) ;
155- if path. extension ( ) . map ( |e| e == "json" ) . unwrap_or ( false ) {
156- if let Some ( stem) = path. file_stem ( ) . and_then ( |s| s. to_str ( ) ) {
157- repos . push ( stem . to_string ( ) ) ;
158- }
156+ if path. extension ( ) . map ( |e| e == "json" ) . unwrap_or ( false )
157+ && let Some ( stem) = path. file_stem ( ) . and_then ( |s| s. to_str ( ) )
158+ {
159+ repos . push ( stem . to_string ( ) ) ;
159160 }
160161 }
161162
@@ -174,17 +175,16 @@ impl ContextStorage {
174175 for entry in std:: fs:: read_dir ( & sessions_dir) ? {
175176 let entry = entry?;
176177 let path = entry. path ( ) ;
177- if path. extension ( ) . map ( |e| e == "json" ) . unwrap_or ( false ) {
178- if let Ok ( metadata) = entry. metadata ( ) {
179- if let Ok ( modified) = metadata. modified ( ) {
180- sessions. push ( ( path, modified) ) ;
181- }
182- }
178+ if path. extension ( ) . map ( |e| e == "json" ) . unwrap_or ( false )
179+ && let Ok ( metadata) = entry. metadata ( )
180+ && let Ok ( modified) = metadata. modified ( )
181+ {
182+ sessions. push ( ( path, modified) ) ;
183183 }
184184 }
185185
186186 // Sort by modification time (newest first)
187- sessions. sort_by ( |a , b| b . 1 . cmp ( & a . 1 ) ) ;
187+ sessions. sort_by_key ( | b| std :: cmp:: Reverse ( b . 1 ) ) ;
188188
189189 // Delete sessions beyond keep limit
190190 let mut deleted = 0 ;
@@ -223,6 +223,13 @@ pub struct MemoryStorage {
223223 repos : std:: collections:: HashMap < String , RepoState > ,
224224}
225225
226+ #[ cfg( test) ]
227+ impl Default for MemoryStorage {
228+ fn default ( ) -> Self {
229+ Self :: new ( )
230+ }
231+ }
232+
226233#[ cfg( test) ]
227234impl MemoryStorage {
228235 pub fn new ( ) -> Self {
0 commit comments