Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

As suggested by sourcery.ai #30

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading