From: Huck Boles Date: Tue, 23 May 2023 23:02:30 +0000 (-0500) Subject: cleanup: extraneous dir.rs X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=10c6adaceed2594b9e6ba81f5222fff0fd505636;p=metaforge.git cleanup: extraneous dir.rs --- diff --git a/dir.rs b/dir.rs deleted file mode 100644 index f69a73e..0000000 --- a/dir.rs +++ /dev/null @@ -1,31 +0,0 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; - -pub fn build_dir(c: &mut Criterion) { - let dir = std::path::PathBuf::from("files/bench_site") - .canonicalize() - .unwrap(); - - let mut opts = metaforge::Options::new(); - opts.root = dir.clone(); - opts.source = dir.join("source"); - opts.build = dir.join("build"); - opts.pattern = dir.join("pattern"); - opts.clean = true; - - c.bench_function("build dir", |b| { - if opts.build.exists() { - std::fs::remove_dir_all(&opts.build).expect("clean build dir"); - } - - std::fs::create_dir(&opts.build).expect("create build dir"); - b.iter(|| metaforge::build_site(black_box(&opts)).unwrap()) - }); -} - -criterion_group! { - name = benches; - config = Criterion::default().sample_size(10).measurement_time(core::time::Duration::from_secs(140)); - targets = build_dir -} - -criterion_main!(benches); diff --git a/files/test_site/source/unit_tests/ignore.meta b/files/test_site/source/unit_tests/ignore.meta new file mode 100644 index 0000000..0c70043 --- /dev/null +++ b/files/test_site/source/unit_tests/ignore.meta @@ -0,0 +1,3 @@ +#{ ignore = true } + +This file shouldn't be passed on diff --git a/src/dir.rs b/src/dir.rs deleted file mode 100644 index f807902..0000000 --- a/src/dir.rs +++ /dev/null @@ -1,101 +0,0 @@ -use crate::{MetaError, Options}; -use eyre::Result; -use rayon::prelude::*; -use std::{fs, path::PathBuf}; - -use super::*; - -#[derive(Debug, Clone)] -pub struct DirNode<'a> { - path: PathBuf, - opts: &'a Options, - global: MetaFile<'a>, - files: Vec>, - 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.opts.parallel {} - - 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") { - let mut new_global = MetaFile::build(file, self.opts)?; - new_global.merge(global); - self.global = new_global; - } else if file.extension().and_then(|f| f.to_str()) == Some("meta") { - let file = 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 { - // we raise an ignored error to quickly abort any file parsing - if let MetaError::Ignored = *e { - continue; - // anything else gets wrapped up and passed up the calling chain - } else { - 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/error.rs b/src/error.rs index 171b849..164d9af 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,7 +5,7 @@ use thiserror::Error; pub enum MetaError { #[error("unknown internal error")] Unknown, - #[error("internal break switch")] + #[error("file ignored")] Ignored, #[error("internal filetype error")] Filetype, diff --git a/src/metafile/dir.rs b/src/metafile/dir.rs index c24654d..447c46b 100644 --- a/src/metafile/dir.rs +++ b/src/metafile/dir.rs @@ -39,6 +39,8 @@ impl<'a> DirNode<'a> { // 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.opts.parallel {} + for f in fs::read_dir(&self.path)? { let file = f?.path(); @@ -61,6 +63,7 @@ impl<'a> DirNode<'a> { pub fn build_files(&mut self) -> Result<()> { for file in self.files.iter_mut() { file.merge(&self.global); + println!(":constructing {:?}", &file); match file.construct() { Ok(str) => { fs::write(file.dest()?, str)?; @@ -71,12 +74,9 @@ impl<'a> DirNode<'a> { eprintln!("ignoring {}: {}", file.path.display(), e); continue; } else { - // we raise an ignored error to quickly abort any file parsing - if let MetaError::Ignored = *e { - continue; - // anything else gets wrapped up and passed up the calling chain - } else { - return Err(e.into()); + match *e { + MetaError::Ignored => continue, + e => return Err(e.into()), } } } diff --git a/src/tests.rs b/src/tests.rs index 7c5174f..272d9d9 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -24,6 +24,28 @@ macro_rules! unit_test ( }; ); +macro_rules! panic_test ( + ($name:ident, $file:expr,$test:literal) => { + #[test] + #[should_panic] + fn $name() { + let dir = PathBuf::from("files/test_site").canonicalize().unwrap_or_default(); + + let mut opts = Options::new(); + opts.root = dir.clone(); + opts.source = dir.join("source"); + opts.build = dir.join("build"); + opts.pattern = dir.join("pattern"); + + let test_dir = opts.source.join("unit_tests"); + let mut path = test_dir.join($file); + path.set_extension("meta"); + let file = MetaFile::build(path, &opts).unwrap(); + assert_eq!(file.construct().unwrap(), $test); + } + }; +); + unit_test!(blank_pattern, "blank/blank_pattern", ""); unit_test!(blank_variable, "blank/blank_variable", "\n\n"); unit_test!(blank_array, "blank/blank_array", "\n\n"); @@ -87,6 +109,8 @@ unit_test!( "\n

GOOD

\n\n" ); +panic_test!(ignore, "ignore.meta", ""); + #[test] fn test_filetype_header() -> Result<()> { let dir = PathBuf::from("files/test_site").canonicalize()?;