Finish up error handling

This commit is contained in:
2024-02-04 01:42:30 -06:00
parent 30f4a0bb7c
commit 79ae00b7a8
2 changed files with 42 additions and 10 deletions

View File

@@ -1,4 +1,6 @@
use std::error::Error; use std::error::Error;
use std::fmt;
use clap::ArgMatches; use clap::ArgMatches;
use crate::config::config::Config; use crate::config::config::Config;
@@ -8,7 +10,7 @@ pub mod dotfile;
pub mod args; pub mod args;
pub fn run(args: ArgMatches, config: Config) -> Result<(), Box<dyn Error>> { pub fn run(args: ArgMatches, config: Config) -> Result<(), ManagerError> {
let copy_to_sys = args.get_flag("from-git"); let copy_to_sys = args.get_flag("from-git");
@@ -17,7 +19,7 @@ pub fn run(args: ArgMatches, config: Config) -> Result<(), Box<dyn Error>> {
let valid_dotfiles: Vec<_> = dotfiles.iter().filter_map(|dotfile| match dotfile { let valid_dotfiles: Vec<_> = dotfiles.iter().filter_map(|dotfile| match dotfile {
Ok(dotfile) => Some(dotfile), Ok(dotfile) => Some(dotfile),
Err(e) => { Err(e) => {
println!("Failed to read a dotfile: {}", e); eprintln!("Failed to read a dotfile: {:?}", e);
None None
}, },
}).collect(); }).collect();
@@ -26,7 +28,7 @@ pub fn run(args: ArgMatches, config: Config) -> Result<(), Box<dyn Error>> {
copy_results.for_each(|result| { copy_results.for_each(|result| {
match result.0 { 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<dyn Error>> {
Ok(()) 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<dotfile::dotfile::DotfileError> for ManagerError {
fn from(error: dotfile::dotfile::DotfileError) -> ManagerError {
ManagerError::DotfileCopyError(error)
}
}
impl From<config::config::ConfigParseError> for ManagerError {
fn from(error: config::config::ConfigParseError) -> ManagerError {
ManagerError::ConfigParseError(error)
}
}

View File

@@ -1,19 +1,15 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::error::Error;
use dotfiles_manager::args::args; use dotfiles_manager::args::args;
use dotfiles_manager::config::config::Config; use dotfiles_manager::config::config::Config;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), dotfiles_manager::ManagerError> {
let args = args::parse_args(); let args = args::parse_args();
let program_config = Config::parse(PathBuf::from("/home/eesim/.config/dotfiles/config"))?; let program_config = Config::parse(PathBuf::from("/home/eesim/.config/dotfiles/config"))?;
if let Err(e) = dotfiles_manager::run(args, program_config) { dotfiles_manager::run(args, program_config)
panic!("Error: {}", e)
};
Ok(())
} }