11use crate :: wrapper:: messages:: { Document , DocumentId , PersistedDocumentInfo } ;
22
3- // Wraps PersistedState (shared with the web frontend) and adds desktop-specific behavior like file I/O
43#[ derive( Default , serde:: Serialize , serde:: Deserialize ) ]
54pub ( crate ) struct PersistentData {
65 documents : Vec < PersistedDocumentInfo > ,
@@ -11,7 +10,6 @@ pub(crate) struct PersistentData {
1110
1211impl PersistentData {
1312 pub ( crate ) fn write_document ( & mut self , id : DocumentId , document : Document ) {
14- // Update or add the document metadata
1513 let info = PersistedDocumentInfo {
1614 id,
1715 name : document. name . clone ( ) ,
@@ -24,14 +22,10 @@ impl PersistentData {
2422 self . documents . push ( info) ;
2523 }
2624
27- // Write the document content to a separate file
2825 if let Err ( e) = std:: fs:: write ( Self :: document_content_path ( & id) , document. content ) {
2926 tracing:: error!( "Failed to write document {id:?} to disk: {e}" ) ;
3027 }
3128
32- if let Some ( order) = & self . document_order {
33- self . force_order ( & order. clone ( ) ) ;
34- }
3529 self . flush ( ) ;
3630 }
3731
@@ -55,7 +49,7 @@ impl PersistentData {
5549 }
5650 }
5751
58- pub ( crate ) fn all_documents ( & self ) -> Vec < ( DocumentId , Document ) > {
52+ pub ( crate ) fn documents ( & self ) -> Vec < ( DocumentId , Document ) > {
5953 self . documents . iter ( ) . filter_map ( |doc| Some ( ( doc. id , self . read_document ( & doc. id ) ?) ) ) . collect ( )
6054 }
6155
@@ -65,12 +59,20 @@ impl PersistentData {
6559 }
6660
6761 pub ( crate ) fn force_document_order ( & mut self , order : Vec < DocumentId > ) {
68- self . force_order ( & order) ;
62+ let mut ordered_prefix_length = 0 ;
63+ for id in & order {
64+ if let Some ( offset) = self . documents [ ordered_prefix_length..] . iter ( ) . position ( |doc| doc. id == * id) {
65+ let found_index = ordered_prefix_length + offset;
66+ if found_index != ordered_prefix_length {
67+ self . documents [ ordered_prefix_length..=found_index] . rotate_right ( 1 ) ;
68+ }
69+ ordered_prefix_length += 1 ;
70+ }
71+ }
6972 self . document_order = Some ( order) ;
7073 self . flush ( ) ;
7174 }
7275
73- // Reads serialized document content from disk and combines it with the stored metadata
7476 fn read_document ( & self , id : & DocumentId ) -> Option < Document > {
7577 let info = self . documents . iter ( ) . find ( |doc| doc. id == * id) ?;
7678 let content = std:: fs:: read_to_string ( Self :: document_content_path ( id) ) . ok ( ) ?;
@@ -82,20 +84,6 @@ impl PersistentData {
8284 } )
8385 }
8486
85- // Reorders the documents array to match a desired ordering, keeping unmentioned documents at the end
86- fn force_order ( & mut self , desired_order : & [ DocumentId ] ) {
87- let mut ordered_prefix_length = 0 ;
88- for id in desired_order {
89- if let Some ( offset) = self . documents [ ordered_prefix_length..] . iter ( ) . position ( |doc| doc. id == * id) {
90- let found_index = ordered_prefix_length + offset;
91- if found_index != ordered_prefix_length {
92- self . documents [ ordered_prefix_length..=found_index] . rotate_right ( 1 ) ;
93- }
94- ordered_prefix_length += 1 ;
95- }
96- }
97- }
98-
9987 fn flush ( & self ) {
10088 let data = match ron:: ser:: to_string_pretty ( self , Default :: default ( ) ) {
10189 Ok ( d) => d,
@@ -132,10 +120,8 @@ impl PersistentData {
132120 * self = loaded;
133121
134122 self . garbage_collect_document_files ( ) ;
135- Self :: delete_old_cef_browser_directory ( ) ;
136123 }
137124
138- // Remove orphaned document content files that have no corresponding entry in the persisted state
139125 fn garbage_collect_document_files ( & self ) {
140126 let valid_paths: std:: collections:: HashSet < _ > = self . documents . iter ( ) . map ( |doc| Self :: document_content_path ( & doc. id ) ) . collect ( ) ;
141127
@@ -159,14 +145,6 @@ impl PersistentData {
159145 }
160146 }
161147
162- // TODO: Eventually remove this cleanup code for the old "browser" CEF directory (renamed to "cef")
163- fn delete_old_cef_browser_directory ( ) {
164- let old_browser_dir = crate :: dirs:: app_data_dir ( ) . join ( "browser" ) ;
165- if old_browser_dir. is_dir ( ) {
166- let _ = std:: fs:: remove_dir_all ( & old_browser_dir) ;
167- }
168- }
169-
170148 fn state_file_path ( ) -> std:: path:: PathBuf {
171149 let mut path = crate :: dirs:: app_data_dir ( ) ;
172150 path. push ( crate :: consts:: APP_STATE_FILE_NAME ) ;
0 commit comments