From 075ea7d7d1126377e6a01b4cefc1045c0040168b Mon Sep 17 00:00:00 2001 From: Sirio Negri Date: Sun, 24 Nov 2024 10:17:41 +0100 Subject: [PATCH] As suggested by sourcery.ai --- src/settings.rs | 44 +++++++++++++++++--------------------------- src/settings_tui.rs | 3 ++- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 50ab2ca..98d8d5d 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -283,38 +283,28 @@ impl Settings { /// Sets language value in config.toml /// /// * `lang`: lang to be set - pub fn set_lang(&mut self, lang: String) { - modify_field_in_file("lang".to_string(), &lang).expect("Error in setting lang, value"); + pub fn set_lang(&mut self, lang: String) -> Result<(), Box> { + //modify_field_in_file("lang".to_string(), &lang).expect("Error in setting lang, value"); + modify_field_in_file("lang".to_string(), &lang)?; + Ok(()) } /// Sets settings in config.toml /// /// * `settings`: settings data to be set - pub fn set_settings(&mut self, settings: Settings) -> Result<(), serde_json::Error> { - let observatory_value: serde_json::Value = serde_json::to_value(&settings.observatory)?; - - if let serde_json::Value::Object(map) = observatory_value { - for (key, value) in map { - let key_str = key.as_str(); - - // Modifichiamo il match per farlo sempre restituire una stringa - let processed_value = match key_str { - "place" | "observatory_name" | "observer_name" | "mpc_code" => { - value.as_str().unwrap_or_default().to_string() - } - "latitude" | "longitude" | "altitude" => { - value.as_f64().unwrap_or_default().to_string() - } - "north_altitude" | "south_altitude" | "east_altitude" | "west_altitude" => { - value.as_i64().unwrap_or_default().to_string() - } - _ => continue, - }; - - println!("Chiave: {}, Valore: {}", key_str, processed_value); - let _ = modify_field_in_file(key_str.to_string(), &processed_value); - } - } + pub fn set_settings(&mut self, settings: Settings) -> Result<(), Box> { + // Update all fields directly + self.observatory = settings.observatory; + + // Write to config file using serde directly + let config_path = dirs::config_local_dir() + .ok_or("Failed to get config dir")? + .join("asteroid_tui") + .join("config.toml"); + + let toml = toml::to_string(&self)?; + std::fs::write(config_path, toml)?; + Ok(()) } diff --git a/src/settings_tui.rs b/src/settings_tui.rs index fb99673..7faf92a 100644 --- a/src/settings_tui.rs +++ b/src/settings_tui.rs @@ -66,7 +66,8 @@ fn language_menu() -> Result<(), Box> { .listbox_lines(5) .prompt()?; let mut set: Settings = Settings::new().unwrap(); - set.set_lang(p.run().unwrap()); + set.set_lang(p.run().unwrap()) + .unwrap_or_else(|error| println!("Error: {}", error)); Ok(()) }