]> git.huck.website - metaforge.git/commitdiff
cleanup: extraneous dir.rs
authorHuck Boles <huck@huck.website>
Tue, 23 May 2023 23:02:30 +0000 (18:02 -0500)
committerHuck Boles <huck@huck.website>
Tue, 23 May 2023 23:02:30 +0000 (18:02 -0500)
dir.rs [deleted file]
files/test_site/source/unit_tests/ignore.meta [new file with mode: 0644]
src/dir.rs [deleted file]
src/error.rs
src/metafile/dir.rs
src/tests.rs

diff --git a/dir.rs b/dir.rs
deleted file mode 100644 (file)
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 (file)
index 0000000..0c70043
--- /dev/null
@@ -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 (file)
index f807902..0000000
+++ /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<MetaFile<'a>>,
-    dirs: Vec<DirNode<'a>>,
-}
-
-impl<'a> DirNode<'a> {
-    pub fn build(path: PathBuf, opts: &'a Options) -> Result<Self> {
-        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<MetaFile> = Vec::new();
-        let dirs: Vec<DirNode> = 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(())
-    }
-}
index 171b84985ea8ca541d32c84908452a99319a7fa4..164d9af7d2ebc50012e0266a1ee66d07aec44a29 100644 (file)
@@ -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,
index c24654d1142e3dc5c40d8245350f94c3899ed350..447c46bd2c633b5e9f554f7a6de2ab77019b9f6c 100644 (file)
@@ -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()),
                         }
                     }
                 }
index 7c5174f0d908c54badba2fff128f8a4b33acc4f7..272d9d9c6dcf56578aabf624ee5ee16ed0ec92d4 100644 (file)
@@ -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", "<html>\n</html>\n");
 unit_test!(blank_array, "blank/blank_array", "<html>\n</html>\n");
@@ -87,6 +109,8 @@ unit_test!(
     "<html>\n<p>GOOD</p>\n</html>\n"
 );
 
+panic_test!(ignore, "ignore.meta", "");
+
 #[test]
 fn test_filetype_header() -> Result<()> {
     let dir = PathBuf::from("files/test_site").canonicalize()?;