diff --git a/src/lib.rs b/src/lib.rs index fc47fda..cba7390 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ use std::error::Error; +use std::fmt; + use clap::ArgMatches; use crate::config::config::Config; @@ -8,7 +10,7 @@ pub mod dotfile; pub mod args; -pub fn run(args: ArgMatches, config: Config) -> Result<(), Box> { +pub fn run(args: ArgMatches, config: Config) -> Result<(), ManagerError> { let copy_to_sys = args.get_flag("from-git"); @@ -17,7 +19,7 @@ pub fn run(args: ArgMatches, config: Config) -> Result<(), Box> { let valid_dotfiles: Vec<_> = dotfiles.iter().filter_map(|dotfile| match dotfile { Ok(dotfile) => Some(dotfile), Err(e) => { - println!("Failed to read a dotfile: {}", e); + eprintln!("Failed to read a dotfile: {:?}", e); None }, }).collect(); @@ -26,7 +28,7 @@ pub fn run(args: ArgMatches, config: Config) -> Result<(), Box> { copy_results.for_each(|result| { match result.0 { - Err(e) => println!("Failed to copy dotfile: {}", e), + Err(e) => println!("Failed to copy dotfile: {:?}", e), _ => (), } }); @@ -35,3 +37,37 @@ pub fn run(args: ArgMatches, config: Config) -> Result<(), Box> { Ok(()) } + + +#[derive(Debug)] +pub enum ManagerError { + DotfileCopyError(dotfile::dotfile::DotfileError), + ConfigParseError(config::config::ConfigParseError), +} + +impl Error for ManagerError {} + +impl fmt::Display for ManagerError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ManagerError::DotfileCopyError(copy_error) => { + write!(f, "{}", copy_error) + }, + ManagerError::ConfigParseError(parse_error) => { + write!(f, "{}", parse_error) + } + } + } +} + +impl From for ManagerError { + fn from(error: dotfile::dotfile::DotfileError) -> ManagerError { + ManagerError::DotfileCopyError(error) + } +} + +impl From for ManagerError { + fn from(error: config::config::ConfigParseError) -> ManagerError { + ManagerError::ConfigParseError(error) + } +} diff --git a/src/main.rs b/src/main.rs index 400619f..8308575 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,15 @@ use std::path::PathBuf; -use std::error::Error; use dotfiles_manager::args::args; use dotfiles_manager::config::config::Config; -fn main() -> Result<(), Box> { +fn main() -> Result<(), dotfiles_manager::ManagerError> { let args = args::parse_args(); let program_config = Config::parse(PathBuf::from("/home/eesim/.config/dotfiles/config"))?; - if let Err(e) = dotfiles_manager::run(args, program_config) { - panic!("Error: {}", e) - }; - - Ok(()) + dotfiles_manager::run(args, program_config) } +