diff --git a/lib/src/account/deserialized_config.rs b/lib/src/account/deserialized_config.rs index cc4280e..9da1798 100644 --- a/lib/src/account/deserialized_config.rs +++ b/lib/src/account/deserialized_config.rs @@ -5,7 +5,7 @@ use log::{debug, trace}; use serde::Deserialize; -use std::{collections::HashMap, env, fs, io, path::PathBuf, result}; +use std::{collections::HashMap, env, fs, io, path::PathBuf}; use thiserror::Error; use toml; @@ -14,15 +14,13 @@ use super::*; #[derive(Error, Debug)] pub enum DeserializeConfigError { #[error("cannot read config file")] - ReadConfigFile(#[from] io::Error), + ReadConfigFile(#[source] io::Error), #[error("cannot parse config file")] - ParseConfigFile(#[from] toml::de::Error), - #[error("cannot read environment variable")] - ReadEnvVar(#[from] env::VarError), + ParseConfigFile(#[source] toml::de::Error), + #[error("cannot read environment variable {1}")] + ReadEnvVar(#[source] env::VarError, &'static str), } -type Result = result::Result; - /// Represents the user config file. #[derive(Debug, Default, Clone, Deserialize)] #[serde(rename_all = "kebab-case")] @@ -51,13 +49,13 @@ pub struct DeserializedConfig { impl DeserializedConfig { /// Tries to create a config from an optional path. - pub fn from_opt_path(path: Option<&str>) -> Result { + pub fn from_opt_path(path: Option<&str>) -> Result { trace!(">> parse config from path"); debug!("path: {:?}", path); let path = path.map(|s| s.into()).unwrap_or(Self::path()?); - let content = fs::read_to_string(path)?; - let config = toml::from_str(&content)?; + let content = fs::read_to_string(path).map_err(DeserializeConfigError::ReadConfigFile)?; + let config = toml::from_str(&content).map_err(DeserializeConfigError::ParseConfigFile)?; trace!("config: {:?}", config); trace!("<< parse config from path"); @@ -66,21 +64,23 @@ impl DeserializedConfig { /// Tries to get the XDG config file path from XDG_CONFIG_HOME /// environment variable. - fn path_from_xdg() -> Result { - let path = env::var("XDG_CONFIG_HOME")?; + fn path_from_xdg() -> Result { + let path = env::var("XDG_CONFIG_HOME") + .map_err(|err| DeserializeConfigError::ReadEnvVar(err, "XDG_CONFIG_HOME"))?; let path = PathBuf::from(path).join("himalaya").join("config.toml"); Ok(path) } /// Tries to get the XDG config file path from HOME environment /// variable. - fn path_from_xdg_alt() -> Result { + fn path_from_xdg_alt() -> Result { let home_var = if cfg!(target_family = "windows") { "USERPROFILE" } else { "HOME" }; - let path = env::var(home_var)?; + let path = + env::var(home_var).map_err(|err| DeserializeConfigError::ReadEnvVar(err, home_var))?; let path = PathBuf::from(path) .join(".config") .join("himalaya") @@ -90,19 +90,20 @@ impl DeserializedConfig { /// Tries to get the .himalayarc config file path from HOME /// environment variable. - fn path_from_home() -> Result { + fn path_from_home() -> Result { let home_var = if cfg!(target_family = "windows") { "USERPROFILE" } else { "HOME" }; - let path = env::var(home_var)?; + let path = + env::var(home_var).map_err(|err| DeserializeConfigError::ReadEnvVar(err, home_var))?; let path = PathBuf::from(path).join(".himalayarc"); Ok(path) } /// Tries to get the config file path. - pub fn path() -> Result { + pub fn path() -> Result { Self::path_from_xdg() .or_else(|_| Self::path_from_xdg_alt()) .or_else(|_| Self::path_from_home())