Skip to content

Commit

Permalink
As suggested by sourcery.ai
Browse files Browse the repository at this point in the history
  • Loading branch information
ziriuz84 committed Nov 24, 2024
1 parent 2b5a9c3 commit 075ea7d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
44 changes: 17 additions & 27 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
//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<dyn std::error::Error>> {
// 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(())
}

Expand Down
3 changes: 2 additions & 1 deletion src/settings_tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ fn language_menu() -> Result<(), Box<dyn std::error::Error>> {
.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(())
}

Expand Down

0 comments on commit 075ea7d

Please sign in to comment.