@@ -46,38 +46,36 @@ pub fn get_safe_path(id: &str) -> Result<PathBuf, String> {
4646 }
4747}
4848
49- fn walk_dir ( dir : & Path , notes : & mut Vec < Note > , base_path : & Path ) {
50- if let Ok ( entries) = fs:: read_dir ( dir) {
51- for entry in entries. flatten ( ) {
52- let path = entry. path ( ) ;
53- if path. is_dir ( ) {
54- walk_dir ( & path, notes, base_path) ;
55- } else if path. is_file ( ) {
56- let ext = path. extension ( ) . and_then ( |e| e. to_str ( ) ) . unwrap_or ( "" ) ;
57- if ext == "md" || ext == "json" {
58- // Ignore legacy Electron window-state files
59- if path. file_name ( ) . and_then ( |n| n. to_str ( ) ) == Some ( "window-state.json" ) {
60- continue ;
61- }
62-
63- if let Ok ( content) = fs:: read_to_string ( & path) {
64- let metadata = fs:: metadata ( & path) . ok ( ) ;
65- let mtime = metadata
66- . and_then ( |m| m. modified ( ) . ok ( ) )
67- . and_then ( |t| t. duration_since ( std:: time:: UNIX_EPOCH ) . ok ( ) )
68- . map ( |d| d. as_millis ( ) as u64 )
69- . unwrap_or ( 0 ) ;
70- let id = path
71- . strip_prefix ( base_path)
72- . unwrap_or ( & path)
73- . to_string_lossy ( )
74- . to_string ( ) ;
75- notes. push ( Note { id, content, mtime } ) ;
76- }
77- }
49+ fn walk_dir ( dir : & Path , notes : & mut Vec < Note > , base_path : & Path ) -> Result < ( ) , String > {
50+ let entries = fs:: read_dir ( dir) . map_err ( |e| e. to_string ( ) ) ?;
51+ for entry in entries {
52+ let entry = entry. map_err ( |e| e. to_string ( ) ) ?;
53+ let path = entry. path ( ) ;
54+ if path. is_dir ( ) {
55+ walk_dir ( & path, notes, base_path) ?;
56+ } else if path. is_file ( ) {
57+ let ext = path. extension ( ) . and_then ( |e| e. to_str ( ) ) . unwrap_or ( "" ) ;
58+ if ext == "md" || ext == "json" {
59+ let content = fs:: read_to_string ( & path)
60+ . map_err ( |e| format ! ( "Failed to read {}: {}" , path. display( ) , e) ) ?;
61+
62+ let metadata = fs:: metadata ( & path) . ok ( ) ;
63+ let mtime = metadata
64+ . and_then ( |m| m. modified ( ) . ok ( ) )
65+ . and_then ( |t| t. duration_since ( std:: time:: UNIX_EPOCH ) . ok ( ) )
66+ . map ( |d| d. as_millis ( ) as u64 )
67+ . unwrap_or_default ( ) ;
68+
69+ let id = path
70+ . strip_prefix ( base_path)
71+ . unwrap_or ( & path)
72+ . to_string_lossy ( )
73+ . to_string ( ) ;
74+ notes. push ( Note { id, content, mtime } ) ;
7875 }
7976 }
8077 }
78+ Ok ( ( ) )
8179}
8280
8381fn clean_empty_parents ( file_path : & Path , base : & Path ) {
@@ -104,7 +102,7 @@ fn clean_empty_parents(file_path: &Path, base: &Path) {
104102pub fn get_notes ( ) -> Result < Vec < Note > , String > {
105103 let base = get_papercache_dir ( ) ?;
106104 let mut notes = Vec :: new ( ) ;
107- walk_dir ( & base, & mut notes, & base) ;
105+ walk_dir ( & base, & mut notes, & base) ? ;
108106 Ok ( notes)
109107}
110108
0 commit comments