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};
pub fn parse_args() -> ArgMatches {
let matches = Command::new("dotfiles")

View File

@@ -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 {}

View File

@@ -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),

View File

@@ -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::<Vec<_>>();
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")
}
}

View File

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

View File

@@ -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),

View File

@@ -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)
}