Finish up error handling
This commit is contained in:
42
src/lib.rs
42
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<dyn Error>> {
|
||||
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<dyn Error>> {
|
||||
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<dyn Error>> {
|
||||
|
||||
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<dyn Error>> {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
10
src/main.rs
10
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<dyn Error>> {
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user