From 6006f92d9c9a16a729067820bbb315aba0a45060 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Fri, 19 Jan 2024 08:54:36 +0100 Subject: [PATCH] app: better? config error handling --- crates/app/src/config.rs | 39 +++++++++++++++++++++++++++++---------- crates/app/src/main.rs | 2 +- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/crates/app/src/config.rs b/crates/app/src/config.rs index a0196fe..f68fa34 100644 --- a/crates/app/src/config.rs +++ b/crates/app/src/config.rs @@ -3,7 +3,6 @@ use clap::Parser; use self::{ cli::Args, config_file::{find_config_file, Configs}, - error::ConfigError, }; mod cli; @@ -15,20 +14,40 @@ pub struct Config { } impl Config { - pub fn read() -> Result { + pub fn read() -> Self { let args = Args::parse(); let config_path = if let Some(config_path) = args.config_path { - config_path + Ok(config_path) } else { - find_config_file()? + find_config_file() }; - let file_config = Configs::read(config_path)?; - Ok(Self { - // this is negated because to an outward api, the negative is more intuitive, - // while in the source the other way around is more intuitive - startup_msg: !(args.no_startup_message || file_config.no_startup_message), - }) + let file_config = if let Ok(config_path) = config_path { + let file_config = Configs::read(config_path); + + match file_config { + Ok(c) => Some(c), + Err(e) => { + eprintln!("Config error: {e:?}"); + eprintln!("Proceeding with defaults or cli args..."); + None + } + } + } else { + None + }; + + if let Some(file_config) = file_config { + Self { + // this is negated because to an outward api, the negative is more intuitive, + // while in the source the other way around is more intuitive + startup_msg: !(args.no_startup_message || file_config.no_startup_message), + } + } else { + Self { + startup_msg: !args.no_startup_message, + } + } } } diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index dd7f3e4..1347572 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -9,7 +9,7 @@ mod welcome_msg; fn main() { // TODO: proper error handling - let cfg = Config::read().unwrap(); + let cfg = Config::read(); if cfg.startup_msg { print_startup_msg();