11use serde:: { Deserialize , Serialize } ;
2- use std:: fs;
3- use std:: path:: Path ;
42use std:: error:: Error ;
3+ use std:: fs;
54use std:: io:: { self , Write } ;
5+ use std:: path:: Path ;
66
77const CONFIG_FILE : & str = "config.toml" ;
88
@@ -50,34 +50,36 @@ impl Config {
5050
5151 fn load_from_file ( ) -> Result < Self , Box < dyn Error > > {
5252 let content = fs:: read_to_string ( CONFIG_FILE ) ?;
53-
53+
5454 // Try to parse the config, but handle missing fields gracefully
5555 match toml:: from_str :: < Config > ( & content) {
5656 Ok ( config) => Ok ( config) ,
5757 Err ( e) => {
5858 // If parsing fails due to missing fields, merge with defaults
5959 if e. to_string ( ) . contains ( "missing field" ) {
6060 println ! ( "⚠️ Config file is missing new fields, updating..." ) ;
61-
61+
6262 // Parse as a generic value first
6363 let mut existing: toml:: Value = toml:: from_str ( & content) ?;
6464 let default_config = Self :: default ( ) ;
6565 let default_value = toml:: Value :: try_from ( & default_config) ?;
66-
66+
6767 // Merge missing fields from defaults
68- if let ( toml:: Value :: Table ( existing_table) , toml:: Value :: Table ( default_table) ) = ( & mut existing, default_value) {
68+ if let ( toml:: Value :: Table ( existing_table) , toml:: Value :: Table ( default_table) ) =
69+ ( & mut existing, default_value)
70+ {
6971 for ( key, value) in default_table {
7072 if !existing_table. contains_key ( & key) {
7173 existing_table. insert ( key, value) ;
7274 }
7375 }
7476 }
75-
77+
7678 // Convert back to Config and save the updated version
7779 let updated_config: Config = existing. try_into ( ) ?;
7880 let updated_content = toml:: to_string_pretty ( & updated_config) ?;
7981 fs:: write ( CONFIG_FILE , & updated_content) ?;
80-
82+
8183 println ! ( "✅ Config file updated with new fields" ) ;
8284 Ok ( updated_config)
8385 } else {
@@ -89,12 +91,12 @@ impl Config {
8991
9092 fn create_default_and_prompt ( ) -> Result < Self , Box < dyn Error > > {
9193 println ! ( "🔧 First time setup - Creating configuration file..." ) ;
92-
94+
9395 let default_config = Self :: default ( ) ;
9496 let toml_content = toml:: to_string_pretty ( & default_config) ?;
95-
97+
9698 fs:: write ( CONFIG_FILE , & toml_content) ?;
97-
99+
98100 println ! ( "✅ Created '{}'" , CONFIG_FILE ) ;
99101 println ! ( ) ;
100102 println ! ( "📝 Please edit the configuration file with your settings:" ) ;
@@ -105,22 +107,20 @@ impl Config {
105107 println ! ( ) ;
106108 print ! ( "Press Enter when you've finished editing the config file..." ) ;
107109 io:: stdout ( ) . flush ( ) ?;
108-
110+
109111 let mut input = String :: new ( ) ;
110112 io:: stdin ( ) . read_line ( & mut input) ?;
111-
113+
112114 // Reload the config after user edits
113115 Self :: load_from_file ( )
114116 }
115117
116118 pub fn rust_color ( & self ) -> u32 {
117119 // Parse hex color string to u32
118120 if self . appearance . embed_color . starts_with ( '#' ) {
119- u32:: from_str_radix ( & self . appearance . embed_color [ 1 ..] , 16 )
120- . unwrap_or ( 0xCD412B )
121+ u32:: from_str_radix ( & self . appearance . embed_color [ 1 ..] , 16 ) . unwrap_or ( 0xCD412B )
121122 } else {
122- u32:: from_str_radix ( & self . appearance . embed_color , 16 )
123- . unwrap_or ( 0xCD412B )
123+ u32:: from_str_radix ( & self . appearance . embed_color , 16 ) . unwrap_or ( 0xCD412B )
124124 }
125125 }
126126}
@@ -147,4 +147,4 @@ impl Default for Config {
147147 } ,
148148 }
149149 }
150- }
150+ }
0 commit comments