-use crate::{MetaError, Options};
+use crate::{error::*, Options};
use eyre::Result;
use std::{fs, path::PathBuf};
// 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();
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)?;
} else {
match *e {
MetaError::Ignored => continue,
- e => return Err(e.into()),
+ e => {
+ eprintln!("{}", file.path.display());
+ return Err(e.into());
+ }
}
}
}
}
}
- 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(_) => {
.into())
}
};
- let mut metafile = parse_string(str, opts)?;
+
+ let mut metafile = parse_string(str, opts).map_err(MetaError::from)?;
metafile.path = path;
Ok(metafile)
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();
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!(