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

Fix/load_settings #66

Merged
merged 1 commit into from
May 11, 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
16 changes: 15 additions & 1 deletion src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,19 @@ impl App

pub fn new<B: Backend>(path: PathBuf, terminal: &mut ratatui::Terminal<B>) -> Result<Self,String>
{
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)?;
Expand All @@ -84,6 +96,8 @@ impl App
screen_size,
help_list: Self::help_list(&settings.key),
settings,
log,
notification,
..Default::default()
};

Expand Down
30 changes: 21 additions & 9 deletions src/app/settings/settings.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -13,29 +13,34 @@ pub struct Settings

impl Settings
{
pub fn load(path: Option<&Path>) -> Result<Settings, String>
pub fn load(path: Option<&Path>) -> Result<Settings, io::Error>
{
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))
)
})
}

Expand All @@ -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)
}
}
}
}
Expand Down