From 81cb5a2252e58ff18ce57e90442548c6f6496a29 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Sun, 14 May 2023 18:29:53 -0500 Subject: [PATCH] names dont include file extensions --- Cargo.toml | 4 --- benches/build_metafile.rs | 9 ++----- benches/map_dir.rs | 27 ------------------- src/metafile.rs | 56 +++++++++++++++++++++++++++++++++------ 4 files changed, 50 insertions(+), 46 deletions(-) delete mode 100644 benches/map_dir.rs diff --git a/Cargo.toml b/Cargo.toml index 81a5157..631bd59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,3 @@ criterion = "0.4" [[bench]] name = "build_metafile" harness = false - -[[bench]] -name = "map_dir" -harness = false diff --git a/benches/build_metafile.rs b/benches/build_metafile.rs index 52dba97..1aa36b7 100644 --- a/benches/build_metafile.rs +++ b/benches/build_metafile.rs @@ -18,13 +18,8 @@ pub fn build_file_benchmark(c: &mut Criterion) { b.iter(|| { let string = std::fs::read_to_string(black_box(&source)).expect("read file"); let file = metaforge::parse_file(string, black_box(&opts)).expect("parse file"); - let mut path = opts.build.join( - source - .strip_prefix(black_box(&opts.source)) - .expect("strip path"), - ); - path.set_extension("html"); - std::fs::write(path, metaforge::build_metafile(&file).expect("build file")) + let string = metaforge::build_metafile(&file).expect("build file"); + metaforge::write_file(black_box(&source), string, black_box(&opts)) .expect("write file"); }) }); diff --git a/benches/map_dir.rs b/benches/map_dir.rs deleted file mode 100644 index c1434f7..0000000 --- a/benches/map_dir.rs +++ /dev/null @@ -1,27 +0,0 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; - -pub fn map_dir_benchmark(c: &mut Criterion) { - let dir = std::path::PathBuf::from("files/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 benchmark file", |b| { - b.iter(|| { - let mut dir = - metaforge::DirNode::build(black_box(opts.source.clone()), black_box(&opts)) - .unwrap(); - let tmp = metaforge::MetaFile::new(black_box(&opts)); - dir.map(&tmp).unwrap(); - }) - }); -} - -criterion_group!(benches, map_dir_benchmark); -criterion_main!(benches); diff --git a/src/metafile.rs b/src/metafile.rs index ce16a98..60dc3a0 100644 --- a/src/metafile.rs +++ b/src/metafile.rs @@ -36,20 +36,30 @@ impl<'a> MetaFile<'a> { pub fn name(&self) -> Result { if self.path.starts_with(&self.opts.source) { - // in source dir, we want the file name - let name = self.path.strip_prefix(&self.opts.source)?; - let name = name.to_string_lossy().to_string().replace('/', "."); + // in source dir, we want the file name without the '.meta' extension + let name: String = self + .path + .strip_prefix(&self.opts.source)? + .components() + .map(|x| { + x.as_os_str() + .to_string_lossy() + .to_string() + .replace(".meta", "") + }) + .collect::>() + .join("."); Ok(name) - } else { + } else if self.path.starts_with(&self.opts.pattern) { // in pattern dir, we want the parent dir let name = self.path.strip_prefix(&self.opts.pattern)?; let name = name .parent() - .unwrap() - .to_string_lossy() - .to_string() - .replace('/', "."); + .map(|s| s.to_string_lossy().to_string().replace('/', ".")) + .unwrap_or_default(); Ok(name) + } else { + color_eyre::eyre::bail!("could not get name from: {}", self.path.display()); } } @@ -193,3 +203,33 @@ impl<'a> DirNode<'a> { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_name() -> Result<()> { + let mut opts = Options::new(); + + opts.source = "/tmp/source".into(); + opts.build = "/tmp/build".into(); + opts.pattern = "/tmp/pattern".into(); + + let src_path = PathBuf::from("/tmp/source/test/file.meta"); + let pat1_path = PathBuf::from("/tmp/pattern/base/test.meta"); + let pat2_path = PathBuf::from("/tmp/pattern/test/class/file.meta"); + + let mut src = MetaFile::new(&opts); + src.path = src_path; + let mut pat1 = MetaFile::new(&opts); + pat1.path = pat1_path; + let mut pat2 = MetaFile::new(&opts); + pat2.path = pat2_path; + + assert_eq!(src.name()?, "test.file"); + assert_eq!(pat1.name()?, "base"); + assert_eq!(pat2.name()?, "test.class"); + + Ok(()) + } +} -- 2.45.2