@@ -63,6 +63,9 @@ impl TmcConfig {
6363
6464 pub fn save ( self , client_name : & str ) -> Result < ( ) > {
6565 let path = Self :: get_location ( client_name) ?;
66+ if let Some ( parent) = path. parent ( ) {
67+ file_util:: create_dir_all ( parent) ?;
68+ }
6669 let mut lock = file_util:: create_file_lock ( & path) ?;
6770 let mut guard = lock. lock ( ) ?;
6871
@@ -81,23 +84,38 @@ impl TmcConfig {
8184
8285 pub fn load ( client_name : & str ) -> Result < TmcConfig > {
8386 let path = Self :: get_location ( client_name) ?;
84- let mut lock = file_util:: open_file_lock ( & path) ?;
85- let mut guard = lock. lock ( ) ?;
8687
87- let mut buf = vec ! [ ] ;
88- let config = match guard. read_to_end ( & mut buf) {
89- Ok ( _) => match toml:: from_slice ( & buf) {
90- Ok ( config) => Ok ( config) ,
91- Err ( _) => {
92- log:: error!(
93- "Failed to deserialize config at {}, resetting" ,
94- path. display( )
95- ) ;
96- Self :: init_at ( client_name, & path)
88+ // try to open config file
89+ let config = match file_util:: open_file_lock ( & path) {
90+ Ok ( mut lock) => {
91+ // found config file, lock and read
92+ let mut guard = lock. lock ( ) ?;
93+ let mut buf = vec ! [ ] ;
94+ let _bytes = guard. read_to_end ( & mut buf) ?;
95+ match toml:: from_slice ( & buf) {
96+ // successfully read file, try to deserialize
97+ Ok ( config) => config, // successfully read and deserialized the config
98+ Err ( _) => {
99+ log:: error!(
100+ "Failed to deserialize config at {}, resetting" ,
101+ path. display( )
102+ ) ;
103+ Self :: init_at ( client_name, & path) ?
104+ }
97105 }
98- } ,
99- Err ( _) => Self :: init_at ( client_name, & path) ,
100- } ?;
106+ }
107+ Err ( e) => {
108+ // failed to open config file, create new one
109+ log:: info!(
110+ "could not open config file at {} due to {}, initializing a new config file" ,
111+ path. display( ) ,
112+ e
113+ ) ;
114+ // todo: check the cause to make sure this makes sense, might be necessary to propagate some error kinds
115+ Self :: init_at ( client_name, & path) ?
116+ }
117+ } ;
118+
101119 if !config. projects_dir . exists ( ) {
102120 fs:: create_dir_all ( & config. projects_dir ) . with_context ( || {
103121 format ! (
@@ -111,6 +129,10 @@ impl TmcConfig {
111129
112130 // initializes the default configuration file at the given path
113131 fn init_at ( client_name : & str , path : & Path ) -> Result < TmcConfig > {
132+ if let Some ( parent) = path. parent ( ) {
133+ file_util:: create_dir_all ( parent) ?;
134+ }
135+
114136 let mut lock = file_util:: create_file_lock ( path)
115137 . with_context ( || format ! ( "Failed to create new config file at {}" , path. display( ) ) ) ?;
116138 let mut guard = lock. lock ( ) ?;
0 commit comments