@@ -39,44 +39,30 @@ impl TmcConfig {
3939 /// Reads or initialises for the client from the given path.
4040 pub fn load_from ( client_name : & str , path : PathBuf ) -> Result < TmcConfig , LangsError > {
4141 // try to open config file
42- let lock = Lock :: file ( & path, LockOptions :: Read ) ;
43- let config = match lock {
44- Ok ( mut lock) => {
45- // found config file, lock and read
46- let mut buf = String :: new ( ) ;
47- let _bytes = lock
48- . lock ( ) ?
49- . get_file ( )
50- . read_to_string ( & mut buf)
51- . map_err ( |e| FileError :: FileRead ( path. clone ( ) , e) ) ?;
52- match deserialize:: toml_from_str :: < Self > ( & buf) {
53- // successfully read file, try to deserialize
54- Ok ( mut config) => {
55- // set the path which was set to default during deserialization
56- config. location = path;
57- config // successfully read and deserialized the config
58- }
59- Err ( e) => {
60- log:: error!(
61- "Failed to deserialize config at {} due to {}, resetting" ,
62- path. display( ) ,
63- e
64- ) ;
65- drop ( lock) ; // unlock file before recreating it
66- Self :: init_at ( client_name, path) ?
67- }
42+ let config = if path. exists ( ) {
43+ // found config file
44+ let data = file_util:: read_file_to_string ( & path) ?;
45+ match deserialize:: toml_from_str :: < Self > ( & data) {
46+ // successfully read file, try to deserialize
47+ Ok ( mut config) => {
48+ // set the path which was set to default during deserialization
49+ config. location = path;
50+ config // successfully read and deserialized the config
51+ }
52+ Err ( e) => {
53+ log:: error!(
54+ "Failed to deserialize config at {} due to {}, resetting" ,
55+ path. display( ) ,
56+ e
57+ ) ;
58+ Self :: init_at ( client_name, path) ?
6859 }
6960 }
70- Err ( e) => {
71- // failed to open config file, create new one
72- log:: info!(
73- "could not open config file at {} due to {}, initializing a new config file" ,
74- path. display( ) ,
75- e
76- ) ;
77- // todo: check the cause to make sure this makes sense, might be necessary to propagate some error kinds
78- Self :: init_at ( client_name, path) ?
79- }
61+ } else {
62+ // failed to open config file, create new one
63+ log:: info!( "initializing a new config file at {}" , path. display( ) ) ;
64+ // todo: check the cause to make sure this makes sense, might be necessary to propagate some error kinds
65+ Self :: init_at ( client_name, path) ?
8066 } ;
8167
8268 if !config. projects_dir . exists ( ) {
0 commit comments