From: Huck Boles Date: Wed, 24 May 2023 01:39:21 +0000 (-0500) Subject: refactored: added check_ignore() for easier error handling X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=807347a217a0b8785bb02f972b94be895936e809;p=metaforge.git refactored: added check_ignore() for easier error handling --- diff --git a/src/error.rs b/src/error.rs index 164d9af..c11f25b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -34,3 +34,13 @@ pub enum MetaError { #[error(transparent)] Other(#[from] eyre::Error), } + +pub fn check_ignore(result: Result) -> Result, MetaError> { + match result { + Ok(f) => Ok(Some(f)), + Err(e) => match e { + MetaError::Ignored => Ok(None), + e => Err(e.into()), + }, + } +} diff --git a/src/metafile/dir.rs b/src/metafile/dir.rs index 4b80825..fb034bc 100644 --- a/src/metafile/dir.rs +++ b/src/metafile/dir.rs @@ -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()); + } } } } diff --git a/src/metafile/file.rs b/src/metafile/file.rs index 37a8223..7f8e70a 100644 --- a/src/metafile/file.rs +++ b/src/metafile/file.rs @@ -34,7 +34,7 @@ impl<'a> MetaFile<'a> { } } - pub fn build(path: PathBuf, opts: &'a Options) -> Result { + pub fn build(path: PathBuf, opts: &'a Options) -> Result { 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) diff --git a/src/tests.rs b/src/tests.rs index 272d9d9..66da952 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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!(