From fd5b4872dd1e6c5181a861cedff81cb8d4acaf03 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt-Goertz Date: Sun, 27 Aug 2023 17:40:54 +0200 Subject: Display config file formatting errors --- src/context/args.rs | 9 +++-- src/context/config/toml/error.rs | 2 +- src/context/config/toml/mod.rs | 74 +++++++++++++++++++++++++++------------- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/src/context/args.rs b/src/context/args.rs index 0608a23..ce202b1 100644 --- a/src/context/args.rs +++ b/src/context/args.rs @@ -121,10 +121,9 @@ fn load_rc_config_args() -> Option { /// Loads an [`ArgMatches`] from `.erdtree.toml`. #[inline] fn load_toml_config_args(named_table: Option<&str>) -> Result, Error> { - config::toml::load().map_or(Ok(None), |toml_config| { - let parsed_args = config::toml::parse(toml_config, named_table)?; - let config_args = Context::command().get_matches_from(parsed_args); + let toml_config = config::toml::load()?; + let parsed_args = config::toml::parse(toml_config, named_table)?; + let config_args = Context::command().get_matches_from(parsed_args); - Ok(Some(config_args)) - }) + Ok(Some(config_args)) } diff --git a/src/context/config/toml/error.rs b/src/context/config/toml/error.rs index 3d2e782..3757c98 100644 --- a/src/context/config/toml/error.rs +++ b/src/context/config/toml/error.rs @@ -5,7 +5,7 @@ pub enum Error { #[error("Failed to load .erdtree.toml")] LoadConfig, - #[error("The configuration file is improperly formatted")] + #[error("The configuration file is improperly formatted: {0}")] InvalidFormat(#[from] ConfigError), #[error("Named table '{0}' was not found in '.erdtree.toml'")] diff --git a/src/context/config/toml/mod.rs b/src/context/config/toml/mod.rs index 0eb9858..95b8b2f 100644 --- a/src/context/config/toml/mod.rs +++ b/src/context/config/toml/mod.rs @@ -68,22 +68,29 @@ pub fn parse(config: Config, named_table: Option<&str>) -> Result, /// Reads in `.erdtree.toml` file. pub fn load() -> Result { #[cfg(windows)] - return windows::load_toml().ok_or(Error::LoadConfig); + return windows::load_toml(); #[cfg(unix)] - unix::load_toml().ok_or(Error::LoadConfig) + unix::load_toml() } -/// Attempts to load in `.erdtree.toml` from `$ERDTREE_TOML_PATH`. Will return `None` for whatever -/// reason. -fn toml_from_env() -> Option { +/// Attempts to load in `.erdtree.toml` from `$ERDTREE_TOML_PATH`. +fn toml_from_env() -> Result { let config = env::var_os(super::ERDTREE_TOML_PATH) .map(OsString::into_string) - .and_then(Result::ok)?; - - let file = config.strip_suffix(".toml").map(File::with_name)?; - - Config::builder().add_source(file).build().ok() + .transpose() + .map_err(|_| Error::LoadConfig)? + .ok_or(Error::LoadConfig)?; + + let file = config + .strip_suffix(".toml") + .map(File::with_name) + .ok_or(Error::LoadConfig)?; + + Config::builder() + .add_source(file) + .build() + .map_err(Error::from) } /// Simple utility used to extract the underlying value from the [`Value`] enum that we get when @@ -126,7 +133,8 @@ fn parse_argument(keyword: &str, arg: &Value) -> Result #[cfg(unix)] mod unix { use super::super::{CONFIG_DIR, ERDTREE_CONFIG_TOML, ERDTREE_DIR, HOME, XDG_CONFIG_HOME}; - use config::{Config, File}; + use super::Error; + use config::{Config, ConfigError, File}; use std::{env, path::PathBuf}; /// Looks for `.erdtree.toml` in the following locations in order: @@ -136,18 +144,20 @@ mod unix { /// - `$XDG_CONFIG_HOME/.erdtree.toml` /// - `$HOME/.config/erdtree/.erdtree.toml` /// - `$HOME/.erdtree.toml` - pub(super) fn load_toml() -> Option { + pub(super) fn load_toml() -> Result { super::toml_from_env() - .or_else(toml_from_xdg_path) - .or_else(toml_from_home) + .or_else(|_| toml_from_xdg_path()) + .or_else(|_| toml_from_home()) } /// Looks for `.erdtree.toml` in the following locations in order: /// /// - `$XDG_CONFIG_HOME/erdtree/.erdtree.toml` /// - `$XDG_CONFIG_HOME/.erdtree.toml` - fn toml_from_xdg_path() -> Option { - let config = env::var_os(XDG_CONFIG_HOME).map(PathBuf::from)?; + fn toml_from_xdg_path() -> Result { + let config = env::var_os(XDG_CONFIG_HOME) + .map(PathBuf::from) + .ok_or(Error::LoadConfig)?; let mut file = config .join(ERDTREE_DIR) @@ -164,15 +174,20 @@ mod unix { .map(File::with_name); } - Config::builder().add_source(file?).build().ok() + file.map_or_else( + || Err(Error::LoadConfig), + |f| Config::builder().add_source(f).build().map_err(Error::from), + ) } /// Looks for `.erdtree.toml` in the following locations in order: /// /// - `$HOME/.config/erdtree/.erdtree.toml` /// - `$HOME/.erdtree.toml` - fn toml_from_home() -> Option { - let home = env::var_os(HOME).map(PathBuf::from)?; + fn toml_from_home() -> Result { + let home = env::var_os(HOME) + .map(PathBuf::from) + .ok_or(Error::LoadConfig)?; let mut file = home .join(CONFIG_DIR) @@ -190,7 +205,16 @@ mod unix { .map(File::with_name); } - Config::builder().add_source(file?).build().ok() + file.map_or_else( + || Err(Error::LoadConfig), + |f| Config::builder() + .add_source(f) + .build() + .map_err(|err| match err { + ConfigError::FileParse { .. } | ConfigError::Type { .. } => Error::from(err), + _ => Error::LoadConfig, + }), + ) } } @@ -198,17 +222,18 @@ mod unix { #[cfg(windows)] mod windows { use super::super::{ERDTREE_CONFIG_TOML, ERDTREE_DIR}; + use super::Error; use config::{Config, File}; /// Try to read in config from the following location: /// - `%APPDATA%\erdtree\.erdtree.toml` - pub(super) fn load_toml() -> Option { + pub(super) fn load_toml() -> Result { super::toml_from_env().or_else(toml_from_appdata) } /// Try to read in config from the following location: /// - `%APPDATA%\erdtree\.erdtree.toml` - fn toml_from_appdata() -> Option { + fn toml_from_appdata() -> Result { let app_data = dirs::config_dir()?; let file = app_data @@ -218,6 +243,9 @@ mod windows { .and_then(|s| s.strip_suffix(".toml")) .map(File::with_name)?; - Config::builder().add_source(file).build().ok() + Config::builder() + .add_source(file) + .build() + .map_err(Error::from) } } -- cgit v1.2.3