fix: use unwrap_or_deafult to simplify config loading logic

This commit is contained in:
Gabriele Musco 2024-09-05 22:58:58 +02:00
commit 5ff160241d

View file

@ -65,16 +65,10 @@ impl Config {
} }
fn from_path(path: &Path) -> Self { fn from_path(path: &Path) -> Self {
match File::open(path) { File::open(path)
Ok(file) => { .ok()
let reader = BufReader::new(file); .and_then(|file| serde_json::from_reader(BufReader::new(file)).ok())
match serde_json::from_reader(reader) { .unwrap_or_default()
Ok(config) => config,
Err(_) => Self::default(),
}
}
Err(_) => Self::default(),
}
} }
fn save_to_path(&self, path: &Path) -> Result<(), serde_json::Error> { fn save_to_path(&self, path: &Path) -> Result<(), serde_json::Error> {