1- use graphite_desktop_wrapper:: messages:: { Document , DocumentId } ;
1+ use graphite_desktop_wrapper:: messages:: { Document , DocumentId , Preferences } ;
22
33#[ derive( Default , serde:: Serialize , serde:: Deserialize ) ]
44pub ( crate ) struct PersistentData {
55 documents : DocumentStore ,
66 current_document : Option < DocumentId > ,
77 #[ serde( skip) ]
88 document_order : Option < Vec < DocumentId > > ,
9+ preferences : Option < Preferences > ,
910}
1011
1112impl PersistentData {
@@ -72,21 +73,37 @@ impl PersistentData {
7273 self . flush ( ) ;
7374 }
7475
76+ pub ( crate ) fn write_preferences ( & mut self , preferences : Preferences ) {
77+ let Ok ( preferences) = ron:: ser:: to_string_pretty ( & preferences, Default :: default ( ) ) else {
78+ tracing:: error!( "Failed to serialize preferences" ) ;
79+ return ;
80+ } ;
81+ std:: fs:: write ( Self :: preferences_file_path ( ) , & preferences) . unwrap_or_else ( |e| {
82+ tracing:: error!( "Failed to write preferences to disk: {e}" ) ;
83+ } ) ;
84+ }
85+
86+ pub ( crate ) fn load_preferences ( & self ) -> Option < Preferences > {
87+ let data = std:: fs:: read_to_string ( Self :: preferences_file_path ( ) ) . ok ( ) ?;
88+ let preferences = ron:: from_str ( & data) . ok ( ) ?;
89+ Some ( preferences)
90+ }
91+
7592 fn flush ( & self ) {
76- let data = match ron:: to_string ( self ) {
93+ let data = match ron:: ser :: to_string_pretty ( self , Default :: default ( ) ) {
7794 Ok ( d) => d,
7895 Err ( e) => {
7996 tracing:: error!( "Failed to serialize persistent data: {e}" ) ;
8097 return ;
8198 }
8299 } ;
83- if let Err ( e) = std:: fs:: write ( Self :: persistence_file_path ( ) , data) {
100+ if let Err ( e) = std:: fs:: write ( Self :: state_file_path ( ) , data) {
84101 tracing:: error!( "Failed to write persistent data to disk: {e}" ) ;
85102 }
86103 }
87104
88105 pub ( crate ) fn load_from_disk ( & mut self ) {
89- let path = Self :: persistence_file_path ( ) ;
106+ let path = Self :: state_file_path ( ) ;
90107 let data = match std:: fs:: read_to_string ( & path) {
91108 Ok ( d) => d,
92109 Err ( e) if e. kind ( ) == std:: io:: ErrorKind :: NotFound => {
@@ -108,9 +125,15 @@ impl PersistentData {
108125 * self = loaded;
109126 }
110127
111- fn persistence_file_path ( ) -> std:: path:: PathBuf {
128+ fn state_file_path ( ) -> std:: path:: PathBuf {
129+ let mut path = crate :: dirs:: graphite_data_dir ( ) ;
130+ path. push ( crate :: consts:: APP_STATE_FILE_NAME ) ;
131+ path
132+ }
133+
134+ fn preferences_file_path ( ) -> std:: path:: PathBuf {
112135 let mut path = crate :: dirs:: graphite_data_dir ( ) ;
113- path. push ( format ! ( "{}.ron" , crate :: consts:: APP_AUTOSAVE_DIRECTORY_NAME ) ) ;
136+ path. push ( crate :: consts:: APP_PREFERENCES_FILE_NAME ) ;
114137 path
115138 }
116139}
0 commit comments