]> git.huck.website - metaforge.git/commitdiff
refactored: added check_ignore() for easier error handling
authorHuck Boles <huck@huck.website>
Wed, 24 May 2023 01:39:21 +0000 (20:39 -0500)
committerHuck Boles <huck@huck.website>
Wed, 24 May 2023 01:39:21 +0000 (20:39 -0500)
src/error.rs
src/metafile/dir.rs
src/metafile/file.rs
src/tests.rs

index 164d9af7d2ebc50012e0266a1ee66d07aec44a29..c11f25b5ef202d5884ed8caf4ee3462256c1dad0 100644 (file)
@@ -34,3 +34,13 @@ pub enum MetaError {
     #[error(transparent)]
     Other(#[from] eyre::Error),
 }
+
+pub fn check_ignore<T>(result: Result<T, MetaError>) -> Result<Option<T>, MetaError> {
+    match result {
+        Ok(f) => Ok(Some(f)),
+        Err(e) => match e {
+            MetaError::Ignored => Ok(None),
+            e => Err(e.into()),
+        },
+    }
+}
index 4b80825b0724ed79d25a8c9626a90019003e393c..fb034bcbd63a14dd5b1bb0099832f6755d22b50d 100644 (file)
@@ -1,4 +1,4 @@
-use crate::{MetaError, Options};
+use crate::{error::*, Options};
 use eyre::Result;
 use std::{fs, path::PathBuf};
 
@@ -39,6 +39,16 @@ 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.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();
 
@@ -46,24 +56,20 @@ impl<'a> DirNode<'a> {
                 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;
+                continue;
             } else if file.extension().and_then(|f| f.to_str()) == Some("meta") {
-                let file = MetaFile::build(file, self.opts)?;
-                self.files.push(file)
+                if let Some(file) = check_ignore(MetaFile::build(file, self.opts))? {
+                    self.files.push(file)
+                }
             }
         }
 
-        eprintln!("{:#?}", self);
-
         Ok(())
     }
 
     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)?;
@@ -76,7 +82,10 @@ impl<'a> DirNode<'a> {
                     } else {
                         match *e {
                             MetaError::Ignored => continue,
-                            e => return Err(e.into()),
+                            e => {
+                                eprintln!("{}", file.path.display());
+                                return Err(e.into());
+                            }
                         }
                     }
                 }
index 37a822365117a50f9eed396f13afaa90cf167aca..7f8e70a6c17a0d0f61acae781cfe179233057c2e 100644 (file)
@@ -34,7 +34,7 @@ impl<'a> MetaFile<'a> {
         }
     }
 
-    pub fn build(path: PathBuf, opts: &'a Options) -> Result<Self> {
+    pub fn build(path: PathBuf, opts: &'a Options) -> Result<Self, MetaError> {
         let str = match std::fs::read_to_string(&path) {
             Ok(str) => str,
             Err(_) => {
@@ -44,7 +44,8 @@ impl<'a> MetaFile<'a> {
                 .into())
             }
         };
-        let mut metafile = parse_string(str, opts)?;
+
+        let mut metafile = parse_string(str, opts).map_err(MetaError::from)?;
 
         metafile.path = path;
         Ok(metafile)
index 272d9d9c6dcf56578aabf624ee5ee16ed0ec92d4..66da952e02d85f60a1fe2dd50c4686e396a8d6e3 100644 (file)
@@ -123,23 +123,17 @@ fn test_filetype_header() -> Result<()> {
     let path = opts.source.join("unit_tests/header/filetype.meta");
     let file = MetaFile::build(path, &opts)?;
 
-    if file.dest()? == opts.build.join("unit_tests/header/filetype.rss") {
-        Ok(())
-    } else {
-        let err = eyre::eyre!("filetype - failed");
-        eprintln!("{:?}", err);
-        eprintln!(
-            "\nTEST:\n{}\nOUTPUT:\n{}",
-            opts.build.join("unit_tests/header/filetype.rss").display(),
-            file.dest()?.display()
-        );
-        Err(err)
-    }
+    assert_eq!(
+        file.dest()?,
+        opts.build.join("unit_tests/header/filetype.rss")
+    );
+
+    Ok(())
 }
 
 #[test]
 fn test_global() -> Result<()> {
-    let dir = PathBuf::from("files/test_site/").canonicalize()?;
+    let dir = PathBuf::from("files/test_site").canonicalize()?;
 
     let mut opts = Options::new();
     opts.root = dir.clone();
@@ -148,8 +142,11 @@ fn test_global() -> Result<()> {
     opts.pattern = dir.join("pattern");
 
     let mut dir_node = crate::DirNode::build(dir.join("source/unit_tests/global"), &opts)?;
+
     let global = MetaFile::build(dir.join("source/default.meta"), &opts)?;
+
     dir_node.map(&global)?;
+
     dir_node.build_dir()?;
 
     assert_eq!(