Cleaned up code a bit

This commit is contained in:
2024-02-05 01:42:43 -06:00
parent e1083962fd
commit 24375ae0c7
7 changed files with 44 additions and 27 deletions

View File

@@ -1,6 +1,8 @@
use clap::{Arg, Command, ArgAction, ArgMatches}; use clap::{Arg, Command, ArgAction, ArgMatches};
pub fn parse_args() -> ArgMatches { pub fn parse_args() -> ArgMatches {
let matches = Command::new("dotfiles") let matches = Command::new("dotfiles")

View File

@@ -99,15 +99,16 @@ impl Config {
#[derive(Debug)] #[derive(Debug)]
pub enum ConfigParseError { pub enum ConfigParseError {
FileReadError(std::io::Error), FileReadError(std::io::Error),
FromUtfError(std::string::FromUtf8Error), FromUtfError(std::string::FromUtf8Error),
TomlParseError(toml::de::Error), TomlParseError(toml::de::Error),
DotfilesCreateError(dot::DotfileError),
DotfilesParseError, DotfilesParseError,
DotfilesArrayParseError, DotfilesArrayParseError,
DotfilesTableParseError, DotfilesTableParseError,
DotfilesCreateError(dot::DotfileError),
} }
impl Error for ConfigParseError {} impl Error for ConfigParseError {}

View File

@@ -11,7 +11,6 @@ use crate::fs::file;
pub enum Dotfile { pub enum Dotfile {
File(file::File), File(file::File),
Dir(dir::Directory) Dir(dir::Directory)
@@ -129,7 +128,6 @@ impl ManagedDotfile {
#[derive(Debug)] #[derive(Debug)]
pub enum DotfileError { pub enum DotfileError {
DotfileIOError(std::io::Error), DotfileIOError(std::io::Error),

View File

@@ -87,17 +87,16 @@ impl Directory {
.iter() .iter()
.map(|file| { .map(|file| {
file.copy( &dest_path.join( PathBuf::from(&file.filename) ) ) file.copy( &dest_path.join( PathBuf::from(&file.filename) ) )
}) }).collect();
.collect();
let dir_copy_results = { let dir_copy_results = {
let dirs = self.directories.iter(); let dirs = self.directories.iter();
let result = dirs.map(|dir| { let results = dirs.map(|dir| {
let dir_name = match dir.path.file_name() { let dir_name = match dir.path.file_name() {
Some(filename) => filename, Some(filename) => filename,
None => return Err(DirError::NoDirNameError), None => return Err(DirError::NoDirectoryNameError),
}; };
let new_dest_path = dest_path.join(PathBuf::from(dir_name)); let new_dest_path = dest_path.join(PathBuf::from(dir_name));
@@ -109,7 +108,7 @@ impl Directory {
dir.copy(&new_dest_path) dir.copy(&new_dest_path)
}).collect::<Vec<_>>(); }).collect::<Vec<_>>();
result results
}; };
let mut copy_errors = Vec::new(); let mut copy_errors = Vec::new();
@@ -136,13 +135,12 @@ impl Directory {
#[derive(Debug)] #[derive(Debug)]
pub enum DirError { pub enum DirError {
DirCopyMetadataError(std::env::VarError), DirCopyMetadataError(std::env::VarError),
DirIOError(std::io::Error), DirIOError(std::io::Error),
DirFileCopyError(file::FileError), DirFileCopyError(file::FileError),
NoDirNameError, NoDirectoryNameError,
} }
impl Error for DirError {} impl Error for DirError {}
@@ -159,7 +157,7 @@ impl fmt::Display for DirError {
DirError::DirFileCopyError(copy_error) => { DirError::DirFileCopyError(copy_error) => {
write!(f, "{}", copy_error) write!(f, "{}", copy_error)
}, },
DirError::NoDirNameError => { DirError::NoDirectoryNameError => {
write!(f, "Directory does not have a valid name") write!(f, "Directory does not have a valid name")
} }
} }

View File

@@ -47,7 +47,6 @@ impl File {
#[derive(Debug)] #[derive(Debug)]
pub enum FileError { pub enum FileError {
CopyError(std::io::Error), CopyError(std::io::Error),

View File

@@ -12,43 +12,58 @@ pub mod args;
pub mod fs; pub mod fs;
pub fn run(args: ArgMatches, config: cfg::Config) -> Result<(), ManagerError> { pub fn run(args: ArgMatches, config: cfg::Config) -> Result<(), ManagerError> {
let copy_to_sys = args.get_flag("from-git"); let copy_to_sys = args.get_flag("from-git");
let dotfiles = config.dotfiles; let dotfiles = config.dotfiles;
let valid_dotfiles: Vec<_> = dotfiles.iter().filter_map(|dotfile| match dotfile { let valid_dotfiles: Vec<_> = dotfiles
Ok(dotfile) => Some(dotfile), .iter()
Err(e) => { .filter_map(|dotfile| match dotfile {
eprintln!("Failed to read a dotfile: {:?}", e); Ok(dotfile) => Some(dotfile),
None Err(e) => {
}, eprintln!("Failed to read a dotfile: {:?}", e);
None
},
}).collect(); }).collect();
let errored_dotfiles = valid_dotfiles.iter().filter_map(|dotfile| match dotfile.get_dotfile_dir_errors() {
errors if !errors.is_empty() => Some(dotfile), let errored_dotfiles = valid_dotfiles
_ => None .iter()
}); .filter_map(|dotfile|
match dotfile.get_dotfile_dir_errors() {
errors if !errors.is_empty() => Some(dotfile),
_ => None
}
);
let _ = errored_dotfiles.map(|dotfile| { let _ = errored_dotfiles.map(|dotfile| {
if let dot::Dotfile::Dir(manager_dotfile) = &dotfile.manager_dotfile { if let dot::Dotfile::Dir(manager_dotfile) = &dotfile.manager_dotfile {
println!("Error copying dotfile: {}", manager_dotfile.path.to_str()?); 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 { if let dot::Dotfile::Dir(system_dotfile) = &dotfile.system_dotfile {
println!("Error copying dotfile: {}", system_dotfile.path.to_str()?); 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(()) 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| { copy_results.for_each(|result| {
match result.0 { match result {
Err(e) => println!("Failed to copy dotfile: {:?}", e), Err(e) => println!("Failed to copy dotfile: {:?}", e),
_ => (), _ => (),
} }
@@ -60,6 +75,8 @@ pub fn run(args: ArgMatches, config: cfg::Config) -> Result<(), ManagerError> {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum ManagerError { pub enum ManagerError {
DotfileCopyError(dot::DotfileError), DotfileCopyError(dot::DotfileError),

View File

@@ -4,7 +4,10 @@ use dotfiles_manager::args::parse;
use dotfiles_manager::config::cfg; use dotfiles_manager::config::cfg;
fn main() -> Result<(), dotfiles_manager::ManagerError> { fn main() -> Result<(), dotfiles_manager::ManagerError> {
let args = parse::parse_args(); let args = parse::parse_args();
let program_config = cfg::Config::parse(PathBuf::from("/home/eesim/.config/dotfiles/config"))?; 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) dotfiles_manager::run(args, program_config)
} }