refactor: replace unsafe unwrap with if let in config saving (#24)

Removed the unsafe `.unwrap()` call when attempting to get the parent directory of `config_path` in `DaemonConfig::save_to_file` and `GuiConfig::save_to_file`. Replaced it with an idiomatic `if let Some(config_dir)` check to improve code safety and maintainability.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Tarasov Aleksandr
2026-03-08 00:26:45 +03:00
committed by GitHub
parent 4b50645c93
commit aa77a8d212
+4 -2
View File
@@ -12,11 +12,12 @@ pub struct DaemonConfig {
impl DaemonConfig {
pub fn save_to_file(&self) -> Result<(), Box<dyn Error>> {
let config_path = get_config_path()?.join("daemon.json");
let config_dir = config_path.parent().unwrap();
if let Some(config_dir) = config_path.parent() {
if !config_path.exists() {
fs::create_dir_all(config_dir)?;
}
}
let config_json = serde_json::to_string_pretty(self)?;
fs::write(config_path, config_json.as_bytes())?;
@@ -63,11 +64,12 @@ impl Default for GuiConfig {
impl GuiConfig {
pub fn save_to_file(&mut self) -> Result<(), Box<dyn Error>> {
let config_path = get_config_path()?.join("gui.json");
let config_dir = config_path.parent().unwrap();
if let Some(config_dir) = config_path.parent() {
if !config_path.exists() {
fs::create_dir_all(config_dir)?;
}
}
// Do not save scale factor if user does not want to
if !self.save_scale_factor {