@@ -31,23 +31,45 @@ impl Settings {
3131
3232 let user_config_path = get_user_config_path ( ) ;
3333
34- // If the user config doesn't exist, create it from the blueprint `whid.toml`
35- if !user_config_path. exists ( ) {
36- // Use the determined blueprint path to read the content
37- if let Ok ( blueprint_content) = fs:: read_to_string ( & blueprint_path) {
38- if let Some ( parent) = user_config_path. parent ( ) {
39- fs:: create_dir_all ( parent) . expect ( "Could not create config directory" ) ;
40- }
41- fs:: write ( & user_config_path, blueprint_content)
42- . expect ( "Could not write user config file from blueprint" ) ;
34+ // Ensure the user config directory exists
35+ if let Some ( parent) = user_config_path. parent ( ) {
36+ if !parent. exists ( ) {
37+ fs:: create_dir_all ( parent) . expect ( "Could not create config directory" ) ;
4338 }
44- // If whid.toml doesn't exist at blueprint_path, builder will fail. This is intended.
4539 }
4640
41+ // Read the blueprint
42+ let blueprint_content = fs:: read_to_string ( & blueprint_path)
43+ . expect ( "Could not read blueprint config file" ) ;
44+ let blueprint_table: toml:: Table = blueprint_content. parse ( )
45+ . expect ( "Could not parse blueprint config as TOML" ) ;
46+
47+ // Read the user config, or create an empty table if it doesn't exist
48+ let user_config_content = fs:: read_to_string ( & user_config_path) . unwrap_or_default ( ) ;
49+ let mut user_table: toml:: Table = user_config_content. parse ( )
50+ . unwrap_or_else ( |_| toml:: Table :: new ( ) ) ;
51+
52+ let mut config_was_updated = false ;
53+ // Iterate over the blueprint and add missing keys to the user config
54+ for ( key, value) in blueprint_table. iter ( ) {
55+ if !user_table. contains_key ( key) {
56+ user_table. insert ( key. clone ( ) , value. clone ( ) ) ;
57+ config_was_updated = true ;
58+ }
59+ }
60+
61+ // If the user config was modified, write it back to the file
62+ if config_was_updated || !user_config_path. exists ( ) {
63+ fs:: write ( & user_config_path, user_table. to_string ( ) )
64+ . expect ( "Could not write updated user config file" ) ;
65+ }
66+
67+
4768 let s = Config :: builder ( )
4869 // 1. Load project defaults from whid.toml (blueprint). Required.
70+ // This still acts as the base for deserialization structure.
4971 . add_source ( File :: from ( blueprint_path) . required ( true ) )
50- // 2. Merge user's global config. Required as we just created it if it was missing.
72+ // 2. Merge user's global config.
5173 . add_source ( File :: from ( user_config_path) . required ( true ) )
5274 // 3. Merge local whid.toml from CWD. Optional override.
5375 . add_source ( File :: with_name ( "whid.toml" ) . required ( false ) )
0 commit comments