From aea5b048998da3ea5e0a96ca23bd3609494107c5 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Sat, 27 May 2023 21:21:39 -0500 Subject: [PATCH] refactor: moved parallel dir functions into dir module --- src/lib.rs | 2 - src/metafile/dir.rs | 104 +++-------------------------- src/metafile/dir/node.rs | 98 +++++++++++++++++++++++++++ src/{ => metafile/dir}/parallel.rs | 0 4 files changed, 106 insertions(+), 98 deletions(-) create mode 100644 src/metafile/dir/node.rs rename src/{ => metafile/dir}/parallel.rs (100%) diff --git a/src/lib.rs b/src/lib.rs index d4d4da4..1f34105 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,6 @@ extern crate pest_derive; mod error; mod metafile; mod options; -mod parallel; mod parser; #[cfg(test)] @@ -14,7 +13,6 @@ mod tests; pub use error::*; pub use metafile::*; pub use options::*; -pub use parallel::*; pub use parser::*; use clap::Parser; diff --git a/src/metafile/dir.rs b/src/metafile/dir.rs index fb034bc..197d964 100644 --- a/src/metafile/dir.rs +++ b/src/metafile/dir.rs @@ -1,6 +1,11 @@ -use crate::{error::*, Options}; -use eyre::Result; -use std::{fs, path::PathBuf}; +mod node; +mod parallel; + +pub use node::*; +pub use parallel::*; + +use crate::Options; +use std::path::PathBuf; use super::*; @@ -12,96 +17,3 @@ pub struct DirNode<'a> { pub files: Vec>, pub dirs: Vec>, } - -impl<'a> DirNode<'a> { - pub fn build(path: PathBuf, opts: &'a Options) -> Result { - assert!(path.is_dir() && path.exists()); - - // copy over directory structure from source dir - let build_dir = opts.build.join(path.strip_prefix(&opts.source)?); - if !build_dir.exists() { - fs::create_dir_all(build_dir)?; - } - - let files: Vec = Vec::new(); - let dirs: Vec = Vec::new(); - let global = MetaFile::new(opts); - - Ok(Self { - path, - opts, - global, - files, - dirs, - }) - } - - // parses all contained files and directories and pushes - // parsed structures into the files and directories vectors - pub fn map(&mut self, global: &'a MetaFile) -> Result<()> { - if self.path.join("default.meta").exists() { - if let Some(mut new_global) = check_ignore(MetaFile::build( - self.path.clone().join("default.meta"), - self.opts, - ))? { - new_global.merge(global); - self.global = new_global; - } - } - - for f in fs::read_dir(&self.path)? { - let file = f?.path(); - - if file.is_dir() { - let dir = DirNode::build(file, self.opts)?; - self.dirs.push(dir); - } else if file.file_name().and_then(|f| f.to_str()) == Some("default.meta") { - continue; - } else if file.extension().and_then(|f| f.to_str()) == Some("meta") { - if let Some(file) = check_ignore(MetaFile::build(file, self.opts))? { - self.files.push(file) - } - } - } - - Ok(()) - } - - pub fn build_files(&mut self) -> Result<()> { - for file in self.files.iter_mut() { - file.merge(&self.global); - match file.construct() { - Ok(str) => { - fs::write(file.dest()?, str)?; - } - Err(e) => { - // print a line to stderr about failure but continue with other files - if self.opts.force { - eprintln!("ignoring {}: {}", file.path.display(), e); - continue; - } else { - match *e { - MetaError::Ignored => continue, - e => { - eprintln!("{}", file.path.display()); - return Err(e.into()); - } - } - } - } - } - } - Ok(()) - } - - pub fn build_dir(&'a mut self) -> Result<()> { - self.build_files()?; - - for dir in self.dirs.iter_mut() { - dir.map(&self.global)?; - dir.build_dir()?; - } - - Ok(()) - } -} diff --git a/src/metafile/dir/node.rs b/src/metafile/dir/node.rs new file mode 100644 index 0000000..e57630e --- /dev/null +++ b/src/metafile/dir/node.rs @@ -0,0 +1,98 @@ +use crate::{error::*, Options}; +use eyre::Result; +use std::{fs, path::PathBuf}; + +use super::*; + +impl<'a> DirNode<'a> { + pub fn build(path: PathBuf, opts: &'a Options) -> Result { + assert!(path.is_dir() && path.exists()); + + // copy over directory structure from source dir + let build_dir = opts.build.join(path.strip_prefix(&opts.source)?); + if !build_dir.exists() { + fs::create_dir_all(build_dir)?; + } + + let files: Vec = Vec::new(); + let dirs: Vec = Vec::new(); + let global = MetaFile::new(opts); + + Ok(Self { + path, + opts, + global, + files, + dirs, + }) + } + + // parses all contained files and directories and pushes + // parsed structures into the files and directories vectors + pub fn map(&mut self, global: &'a MetaFile) -> Result<()> { + if self.path.join("default.meta").exists() { + if let Some(mut new_global) = check_ignore(MetaFile::build( + self.path.clone().join("default.meta"), + self.opts, + ))? { + new_global.merge(global); + self.global = new_global; + } + } + + for f in fs::read_dir(&self.path)? { + let file = f?.path(); + + if file.is_dir() { + let dir = DirNode::build(file, self.opts)?; + self.dirs.push(dir); + } else if file.file_name().and_then(|f| f.to_str()) == Some("default.meta") { + continue; + } else if file.extension().and_then(|f| f.to_str()) == Some("meta") { + if let Some(file) = check_ignore(MetaFile::build(file, self.opts))? { + self.files.push(file) + } + } + } + + Ok(()) + } + + pub fn build_files(&mut self) -> Result<()> { + for file in self.files.iter_mut() { + file.merge(&self.global); + match file.construct() { + Ok(str) => { + fs::write(file.dest()?, str)?; + } + Err(e) => { + // print a line to stderr about failure but continue with other files + if self.opts.force { + eprintln!("ignoring {}: {}", file.path.display(), e); + continue; + } else { + match *e { + MetaError::Ignored => continue, + e => { + eprintln!("{}", file.path.display()); + return Err(e.into()); + } + } + } + } + } + } + Ok(()) + } + + pub fn build_dir(&'a mut self) -> Result<()> { + self.build_files()?; + + for dir in self.dirs.iter_mut() { + dir.map(&self.global)?; + dir.build_dir()?; + } + + Ok(()) + } +} diff --git a/src/parallel.rs b/src/metafile/dir/parallel.rs similarity index 100% rename from src/parallel.rs rename to src/metafile/dir/parallel.rs -- 2.45.2