diff --git a/src/config.rs b/src/config.rs index 2b1767c..62b0315 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use config::{Config, FileFormat}; use serde::{Deserialize, Serialize}; @@ -13,25 +13,24 @@ pub struct S2Config { /// Path to the configuration file pub fn config_path() -> Result { - let mut path = dirs::config_dir().ok_or(S2ConfigError::ConfigDirNotFound)?; + let mut path = dirs::config_dir().ok_or(S2ConfigError::DirNotFound)?; path.push("s2"); path.push("config.toml"); Ok(path) } #[allow(dead_code)] -pub fn load_config(path: &PathBuf) -> Result { +pub fn load_config(path: &Path) -> Result { let cfg = Config::builder() .add_source(config::File::new( - path.to_str().ok_or(S2ConfigError::ConfigPathError)?, + path.to_str().ok_or(S2ConfigError::PathError)?, FileFormat::Toml, )) .build() - .map_err(|_| S2ConfigError::ConfigLoadError)?; + .map_err(|_| S2ConfigError::LoadError)?; - Ok(cfg - .try_deserialize::() - .map_err(|_| S2ConfigError::ConfigLoadError)?) + cfg.try_deserialize::() + .map_err(|_| S2ConfigError::LoadError) } pub fn create_config(config_path: &PathBuf, token: &str) -> Result<(), S2ConfigError> { @@ -40,11 +39,11 @@ pub fn create_config(config_path: &PathBuf, token: &str) -> Result<(), S2ConfigE }; if let Some(parent) = config_path.parent() { - std::fs::create_dir_all(parent).map_err(|_| S2ConfigError::ConfigWriteError)?; + std::fs::create_dir_all(parent).map_err(|_| S2ConfigError::WriteError)?; } let toml = toml::to_string(&cfg).unwrap(); - std::fs::write(config_path, toml).map_err(|_| S2ConfigError::ConfigWriteError)?; + std::fs::write(config_path, toml).map_err(|_| S2ConfigError::WriteError)?; Ok(()) } @@ -52,11 +51,11 @@ pub fn create_config(config_path: &PathBuf, token: &str) -> Result<(), S2ConfigE #[derive(Error, Debug)] pub enum S2ConfigError { #[error("Failed to find config directory")] - ConfigDirNotFound, + DirNotFound, #[error("Failed to find config file")] - ConfigPathError, + PathError, #[error("Failed to load config file")] - ConfigLoadError, + LoadError, #[error("Failed to write config file")] - ConfigWriteError, + WriteError, }