From 17c35cf747872f35b9a1b85627027e12525a1d69 Mon Sep 17 00:00:00 2001 From: Ettore Ricci Date: Sat, 11 May 2024 15:31:02 +0200 Subject: [PATCH] Fixed behavior of Settings::load_or_create now returns error also if a file is present and not valid instead of overwriting it --- src/app/app.rs | 16 +++++++++++++++- src/app/settings/settings.rs | 30 +++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/app/app.rs b/src/app/app.rs index 9dccc12..92d18d3 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -69,7 +69,19 @@ impl App pub fn new(path: PathBuf, terminal: &mut ratatui::Terminal) -> Result { - let settings = Settings::load(None).unwrap_or_default(); + let mut log = Vec::new(); + let mut notification = NotificationLevel::None; + let settings = match Settings::load_or_create(None) + { + Ok(settings) => settings, + Err(e) => { + log.push(LogLine { level: NotificationLevel::Error, message: + format!("Error loading settings: {e}") + }); + notification = NotificationLevel::Error; + Settings::default() + }, + }; let path = path.to_string_lossy(); let path = shellexpand::full(&path).map_err(|e| e.to_string())?; Self::print_loading_status(&settings.color, &format!("Opening \"{}\"...", path), terminal)?; @@ -84,6 +96,8 @@ impl App screen_size, help_list: Self::help_list(&settings.key), settings, + log, + notification, ..Default::default() }; diff --git a/src/app/settings/settings.rs b/src/app/settings/settings.rs index ef95550..af0b4f8 100644 --- a/src/app/settings/settings.rs +++ b/src/app/settings/settings.rs @@ -1,5 +1,5 @@ #![allow(clippy::module_inception)] -use std::path::{Path, PathBuf}; +use std::{io, path::{Path, PathBuf}}; use super::{color_settings::ColorSettings, key_settings::KeySettings}; @@ -13,29 +13,34 @@ pub struct Settings impl Settings { - pub fn load(path: Option<&Path>) -> Result + pub fn load(path: Option<&Path>) -> Result { let path = match path { Some(path) => path.to_path_buf(), - None => Self::get_default_settings_path().ok_or("Could not get default settings path")? + None => Self::get_default_settings_path().ok_or( + io::Error::new(io::ErrorKind::Other, "Could not get default settings path") + )? }; if !path.exists() { - return Err("Settings file does not exist".to_string()) + return Err(io::Error::new(io::ErrorKind::NotFound, "Settings file not found")); } let settings = match std::fs::read_to_string(&path) { Ok(settings) => settings, - Err(e) => return Err(format!("Could not read settings file: {}", e)) + Err(e) => return Err(e) }; Ok(match serde_json::from_str(&settings) { Ok(settings) => settings, - Err(e) => return Err(format!("Could not parse settings file: {}", e)) + Err(e) => return Err(io::Error::new( + io::ErrorKind::InvalidData, + format!("Could not parse settings file: {}", e)) + ) }) } @@ -55,9 +60,16 @@ impl Settings { Ok(settings) => Ok(settings), Err(e) => { - let settings = Settings::default(); - settings.save(path).ok_or(format!("Could not save default settings: {}", e))?; - Ok(settings) + if e.kind() != io::ErrorKind::NotFound + { + Err(format!("Could not load settings: {}", e)) + } + else + { + let settings = Settings::default(); + settings.save(path).ok_or(format!("Could not save default settings: {}", e))?; + Ok(settings) + } } } }