diff --git a/src/args/parse.rs b/src/args/parse.rs index e5cffcd..b2b0799 100644 --- a/src/args/parse.rs +++ b/src/args/parse.rs @@ -1,6 +1,8 @@ use clap::{Arg, Command, ArgAction, ArgMatches}; + + pub fn parse_args() -> ArgMatches { let matches = Command::new("dotfiles") diff --git a/src/config/cfg.rs b/src/config/cfg.rs index afc3ecd..ad858e6 100644 --- a/src/config/cfg.rs +++ b/src/config/cfg.rs @@ -99,15 +99,16 @@ impl Config { + #[derive(Debug)] pub enum ConfigParseError { FileReadError(std::io::Error), FromUtfError(std::string::FromUtf8Error), TomlParseError(toml::de::Error), + DotfilesCreateError(dot::DotfileError), DotfilesParseError, DotfilesArrayParseError, DotfilesTableParseError, - DotfilesCreateError(dot::DotfileError), } impl Error for ConfigParseError {} diff --git a/src/dotfile/dot.rs b/src/dotfile/dot.rs index d747e09..8a90195 100644 --- a/src/dotfile/dot.rs +++ b/src/dotfile/dot.rs @@ -11,7 +11,6 @@ use crate::fs::file; - pub enum Dotfile { File(file::File), Dir(dir::Directory) @@ -129,7 +128,6 @@ impl ManagedDotfile { - #[derive(Debug)] pub enum DotfileError { DotfileIOError(std::io::Error), diff --git a/src/fs/dir.rs b/src/fs/dir.rs index 6457eb6..ba51f55 100644 --- a/src/fs/dir.rs +++ b/src/fs/dir.rs @@ -87,17 +87,16 @@ impl Directory { .iter() .map(|file| { file.copy( &dest_path.join( PathBuf::from(&file.filename) ) ) - }) - .collect(); + }).collect(); let dir_copy_results = { let dirs = self.directories.iter(); - let result = dirs.map(|dir| { + let results = dirs.map(|dir| { let dir_name = match dir.path.file_name() { Some(filename) => filename, - None => return Err(DirError::NoDirNameError), + None => return Err(DirError::NoDirectoryNameError), }; let new_dest_path = dest_path.join(PathBuf::from(dir_name)); @@ -109,7 +108,7 @@ impl Directory { dir.copy(&new_dest_path) }).collect::>(); - result + results }; let mut copy_errors = Vec::new(); @@ -136,13 +135,12 @@ impl Directory { - #[derive(Debug)] pub enum DirError { DirCopyMetadataError(std::env::VarError), DirIOError(std::io::Error), DirFileCopyError(file::FileError), - NoDirNameError, + NoDirectoryNameError, } impl Error for DirError {} @@ -159,7 +157,7 @@ impl fmt::Display for DirError { DirError::DirFileCopyError(copy_error) => { write!(f, "{}", copy_error) }, - DirError::NoDirNameError => { + DirError::NoDirectoryNameError => { write!(f, "Directory does not have a valid name") } } diff --git a/src/fs/file.rs b/src/fs/file.rs index 7068d85..d7fe643 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -47,7 +47,6 @@ impl File { - #[derive(Debug)] pub enum FileError { CopyError(std::io::Error), diff --git a/src/lib.rs b/src/lib.rs index cb8e8ce..d660371 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,43 +12,58 @@ pub mod args; pub mod fs; + + pub fn run(args: ArgMatches, config: cfg::Config) -> Result<(), ManagerError> { let copy_to_sys = args.get_flag("from-git"); let dotfiles = config.dotfiles; - let valid_dotfiles: Vec<_> = dotfiles.iter().filter_map(|dotfile| match dotfile { - Ok(dotfile) => Some(dotfile), - Err(e) => { - eprintln!("Failed to read a dotfile: {:?}", e); - None - }, + let valid_dotfiles: Vec<_> = dotfiles + .iter() + .filter_map(|dotfile| match dotfile { + Ok(dotfile) => Some(dotfile), + Err(e) => { + eprintln!("Failed to read a dotfile: {:?}", e); + None + }, }).collect(); - let errored_dotfiles = valid_dotfiles.iter().filter_map(|dotfile| match dotfile.get_dotfile_dir_errors() { - errors if !errors.is_empty() => Some(dotfile), - _ => None - }); + + let errored_dotfiles = valid_dotfiles + .iter() + .filter_map(|dotfile| + match dotfile.get_dotfile_dir_errors() { + errors if !errors.is_empty() => Some(dotfile), + _ => None + } + ); + let _ = errored_dotfiles.map(|dotfile| { + if let dot::Dotfile::Dir(manager_dotfile) = &dotfile.manager_dotfile { println!("Error copying dotfile: {}", manager_dotfile.path.to_str()?); - manager_dotfile.errors.iter().for_each(|error| println!("Error: {:?}", error)); + manager_dotfile.errors + .iter() + .for_each(|error| println!("Error: {:?}", error)); }; if let dot::Dotfile::Dir(system_dotfile) = &dotfile.system_dotfile { println!("Error copying dotfile: {}", system_dotfile.path.to_str()?); - system_dotfile.errors.iter().for_each(|error| println!("Error: {:?}", error)); + system_dotfile.errors + .iter() + .for_each(|error| println!("Error: {:?}", error)); }; Some(()) }); - let copy_results = valid_dotfiles.iter().map(|dotfile| (dotfile.copy_dotfile(copy_to_sys), dotfile)); + let copy_results = valid_dotfiles.iter().map(|dotfile| dotfile.copy_dotfile(copy_to_sys)); copy_results.for_each(|result| { - match result.0 { + match result { Err(e) => println!("Failed to copy dotfile: {:?}", e), _ => (), } @@ -60,6 +75,8 @@ pub fn run(args: ArgMatches, config: cfg::Config) -> Result<(), ManagerError> { } + + #[derive(Debug)] pub enum ManagerError { DotfileCopyError(dot::DotfileError), diff --git a/src/main.rs b/src/main.rs index 4df897f..1a8825d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,10 @@ use dotfiles_manager::args::parse; use dotfiles_manager::config::cfg; + + fn main() -> Result<(), dotfiles_manager::ManagerError> { + let args = parse::parse_args(); let program_config = cfg::Config::parse(PathBuf::from("/home/eesim/.config/dotfiles/config"))?; @@ -12,4 +15,3 @@ fn main() -> Result<(), dotfiles_manager::ManagerError> { dotfiles_manager::run(args, program_config) } -