From 7fd4df26aa5236f9d95273fb8159e2a19228512c Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Sat, 3 May 2025 16:58:37 +0200 Subject: [PATCH] startup when no config exists --- src/lib/config.rs | 39 ++++++++++++--------------------------- src/lib/gui.rs | 1 - src/main.rs | 2 +- 3 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/lib/config.rs b/src/lib/config.rs index b8cacf7..48e7d05 100644 --- a/src/lib/config.rs +++ b/src/lib/config.rs @@ -1,27 +1,13 @@ use std::path::PathBuf; use std::str::FromStr; -use std::{env, fmt, fs}; +use std::{env, fs}; -use anyhow::anyhow; +use crate::Error; use clap::{Parser, ValueEnum}; use serde::{Deserialize, Serialize}; use serde_json::Value; use thiserror::Error; -#[derive(Debug)] -pub enum ConfigurationError { - Open(String), - Parse(String), -} - -impl fmt::Display for ConfigurationError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - ConfigurationError::Open(e) | ConfigurationError::Parse(e) => write!(f, "{e}"), - } - } -} - #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize)] pub enum Anchor { Top, @@ -593,7 +579,7 @@ pub fn parse_args() -> Config { /// # Errors /// /// Will return Err when it cannot resolve any path or no style is found -fn style_path(full_path: Option) -> Result { +fn style_path(full_path: Option) -> Result { let alternative_paths = path_alternatives( vec![dirs::config_dir()], &PathBuf::from("worf").join("style.css"), @@ -604,7 +590,7 @@ fn style_path(full_path: Option) -> Result { /// # Errors /// /// Will return Err when it cannot resolve any path or no style is found -pub fn conf_path(full_path: Option) -> Result { +pub fn conf_path(full_path: Option) -> Result { let alternative_paths = path_alternatives( vec![dirs::config_dir()], &PathBuf::from("worf").join("config"), @@ -629,7 +615,7 @@ pub fn path_alternatives(base_paths: Vec>, sub_path: &PathBuf) - pub fn resolve_path( full_path: Option, alternatives: Vec, -) -> Result { +) -> Result { full_path .map(PathBuf::from) .and_then(|p| p.canonicalize().ok().filter(|c| c.exists())) @@ -639,7 +625,7 @@ pub fn resolve_path( .filter(|p| p.exists()) .find_map(|pb| pb.canonicalize().ok().filter(|c| c.exists())) }) - .ok_or_else(|| anyhow!("Could not find a valid file.")) + .ok_or(Error::MissingFile) } /// # Errors @@ -649,25 +635,24 @@ pub fn resolve_path( /// * cannot parse the config file /// * no config file exists /// * config file and args cannot be merged -pub fn load_config(args_opt: Option) -> Result { +pub fn load_config(args_opt: Option<&Config>) -> Result { let config_path = conf_path(args_opt.as_ref().and_then(|c| c.config.clone())); match config_path { Ok(path) => { - let toml_content = - fs::read_to_string(path).map_err(|e| ConfigurationError::Open(format!("{e}")))?; - let mut config: Config = toml::from_str(&toml_content) - .map_err(|e| ConfigurationError::Parse(format!("{e}")))?; + let toml_content = fs::read_to_string(path).map_err(|e| Error::Io(format!("{e}")))?; + let mut config: Config = + toml::from_str(&toml_content).map_err(|e| Error::ParsingError(format!("{e}")))?; if let Some(args) = args_opt { let merge_result = merge_config_with_args(&mut config, &args) - .map_err(|e| ConfigurationError::Parse(format!("{e}")))?; + .map_err(|e| Error::ParsingError(format!("{e}")))?; Ok(merge_result) } else { Ok(config) } } - Err(e) => Err(ConfigurationError::Open(format!("{e}"))), + Err(e) => Err(Error::Io(format!("{e}"))), } } diff --git a/src/lib/gui.rs b/src/lib/gui.rs index 0974801..3ad3350 100644 --- a/src/lib/gui.rs +++ b/src/lib/gui.rs @@ -888,7 +888,6 @@ fn parse_label(label: &str) -> (Option, Option) { (img, text) } - fn lookup_icon(icon_path: Option<&str>, config: &Config) -> Option { if let Some(image_path) = icon_path { let img_regex = Regex::new(&format!( diff --git a/src/main.rs b/src/main.rs index 3879f7f..5560408 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ fn main() -> anyhow::Result<()> { .init(); let args = config::parse_args(); - let config = config::load_config(Some(args)).map_err(|e| anyhow!(e))?; + let config = config::load_config(Some(&args)).unwrap_or(args); if let Some(show) = &config.show() { match show {