diff --git a/src/error.rs b/src/error.rs index 1620ce6..3d7ec41 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,3 @@ -use crate::etcetera; use std::error::Error as StdError; use std::fmt::{self, Display}; use std::io; @@ -9,7 +8,7 @@ pub enum Error { TomlSer(toml::ser::Error), TomlDe(toml::de::Error), Quote(shlex::QuoteError), - HomeDir(etcetera::HomeDirError), + HomeDir, Bat(bat::error::Error), } @@ -39,12 +38,6 @@ impl From for Error { } } -impl From for Error { - fn from(error: etcetera::HomeDirError) -> Self { - Error::HomeDir(error) - } -} - impl From for Error { fn from(error: bat::error::Error) -> Self { Error::Bat(error) @@ -58,7 +51,7 @@ impl Display for Error { Error::TomlSer(e) => e.fmt(formatter), Error::TomlDe(e) => e.fmt(formatter), Error::Quote(e) => e.fmt(formatter), - Error::HomeDir(e) => e.fmt(formatter), + Error::HomeDir => formatter.write_str("could not locate home directory"), Error::Bat(e) => e.fmt(formatter), } } @@ -71,7 +64,7 @@ impl StdError for Error { Error::TomlSer(e) => e.source(), Error::TomlDe(e) => e.source(), Error::Quote(e) => e.source(), - Error::HomeDir(e) => e.source(), + Error::HomeDir => None, Error::Bat(e) => e.source(), } } diff --git a/src/etcetera/base_strategy.rs b/src/etcetera/base_strategy.rs index aa674d8..f3ed44e 100644 --- a/src/etcetera/base_strategy.rs +++ b/src/etcetera/base_strategy.rs @@ -1,4 +1,4 @@ -use crate::etcetera::HomeDirError; +use crate::error::Result; use std::path::PathBuf; pub trait BaseStrategy { @@ -7,7 +7,7 @@ pub trait BaseStrategy { macro_rules! create_strategies { ($base: ty) => { - pub fn choose_base_strategy() -> Result<$base, HomeDirError> { + pub fn choose_base_strategy() -> Result<$base> { <$base>::new() } }; diff --git a/src/etcetera/base_strategy/windows.rs b/src/etcetera/base_strategy/windows.rs index c90fe34..e3756b9 100644 --- a/src/etcetera/base_strategy/windows.rs +++ b/src/etcetera/base_strategy/windows.rs @@ -1,13 +1,12 @@ +use crate::error::Result; use std::path::PathBuf; -use crate::etcetera::HomeDirError; - pub struct Windows { home_dir: PathBuf, } impl Windows { - pub fn new() -> Result { + pub fn new() -> Result { Ok(Self { home_dir: crate::etcetera::home_dir()?, }) diff --git a/src/etcetera/base_strategy/xdg.rs b/src/etcetera/base_strategy/xdg.rs index 075d991..d22369e 100644 --- a/src/etcetera/base_strategy/xdg.rs +++ b/src/etcetera/base_strategy/xdg.rs @@ -1,14 +1,13 @@ +use crate::error::Result; use std::path::Path; use std::path::PathBuf; -use crate::etcetera::HomeDirError; - pub struct Xdg { home_dir: PathBuf, } impl Xdg { - pub fn new() -> Result { + pub fn new() -> Result { Ok(Self { home_dir: crate::etcetera::home_dir()?, }) diff --git a/src/etcetera/mod.rs b/src/etcetera/mod.rs index 3bf0257..708f6bf 100644 --- a/src/etcetera/mod.rs +++ b/src/etcetera/mod.rs @@ -1,18 +1,9 @@ +use crate::error::{Error, Result}; + pub mod base_strategy; pub use base_strategy::{choose_base_strategy, BaseStrategy}; -pub fn home_dir() -> Result { - home::home_dir().ok_or(HomeDirError) +pub fn home_dir() -> Result { + home::home_dir().ok_or(Error::HomeDir) } - -#[derive(Debug)] -pub struct HomeDirError; - -impl std::fmt::Display for HomeDirError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "could not locate home directory") - } -} - -impl std::error::Error for HomeDirError {}