From ac41f55d22fbb94accc4ef8773a4266a57563058 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Wed, 10 May 2023 18:40:14 -0500 Subject: [PATCH] semi-working main() --- .gitignore | 2 +- src/filetype/builder.rs | 60 +++++++++++++------ src/main.rs | 32 +++++++++- src/tests/test_metafile.rs | 2 +- test_site/source/sub_dir/sub_source.meta | 33 ---------- .../source => tests/files}/test_source.meta | 0 6 files changed, 73 insertions(+), 56 deletions(-) delete mode 100644 test_site/source/sub_dir/sub_source.meta rename {test_site/source => tests/files}/test_source.meta (100%) diff --git a/.gitignore b/.gitignore index 17e283c..c688828 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ /target -/test_site/site +/test_site/build diff --git a/src/filetype/builder.rs b/src/filetype/builder.rs index 55a7405..adea616 100644 --- a/src/filetype/builder.rs +++ b/src/filetype/builder.rs @@ -1,5 +1,9 @@ use crate::{parse_file, MetaFile, Options, Source, Substitution}; -use color_eyre::{eyre::bail, Result}; +use color_eyre::{ + eyre::bail, + eyre::{eyre, WrapErr}, + Result, +}; use pandoc::{InputFormat, InputKind, OutputFormat, OutputKind, Pandoc, PandocOutput}; use std::{ collections::HashMap, @@ -8,13 +12,16 @@ use std::{ }; pub fn build_metafile(path: &Path, dirs: &Options) -> Result<()> { - let file = fs::read_to_string(path)?; - let file = parse_file(&file)?; + let file = fs::read_to_string(path) + .wrap_err_with(|| eyre!("failed to read: {}\n", path.to_string_lossy()))?; + let file = parse_file(&file) + .wrap_err_with(|| eyre!("failed to parse: {}\n", path.to_string_lossy()))?; - let html = get_source_html(&file, dirs)?; + let html = get_source_html(&file, dirs) + .wrap_err_with(|| eyre!("failed converting to html: {}\n", path.to_string_lossy()))?; - let pattern = get_pattern("base", &file, dirs)?; - let mut base = parse_file(&pattern)?; + let pattern = get_pattern("base", &file, dirs).wrap_err("failed to get base pattern\n")?; + let mut base = parse_file(&pattern).wrap_err("failed to parse base pattern\n")?; base.variables = file.variables; base.arrays = file.arrays; @@ -22,10 +29,19 @@ pub fn build_metafile(path: &Path, dirs: &Options) -> Result<()> { base.patterns.insert("SOURCE", &html); - let output = metafile_to_string(&base, dirs, Some("base"))?; + let output = metafile_to_string(&base, dirs, Some("base")) + .wrap_err_with(|| eyre!("failed to build: {}\n", path.to_string_lossy()))?; + + let dest = find_dest(path, dirs).wrap_err_with(|| { + format!( + "could not find destination file: {}\n", + path.to_string_lossy() + ) + })?; // want newline to end file - fs::write(find_dest(path, dirs)?, output + "\n")?; + fs::write(&dest, output + "\n") + .wrap_err_with(|| eyre!("could not write to: {}\n", dest.to_string_lossy()))?; Ok(()) } @@ -48,7 +64,8 @@ pub fn metafile_to_string(file: &MetaFile, dirs: &Options, name: Option<&str>) - .filter(|val| *val != "BLANK" && *val != "DEFAULT") .map(|val| val.to_string()) .unwrap_or_default(), - Substitution::Pattern(key) => get_pattern(key, file, dirs)?, + Substitution::Pattern(key) => get_pattern(key, file, dirs) + .wrap_err_with(|| eyre!("could not find pattern for: {}\n", key))?, // comments have already been removed at this point, // so we use them to mark keys for array substitution Substitution::Array(key) => { @@ -69,7 +86,7 @@ pub fn metafile_to_string(file: &MetaFile, dirs: &Options, name: Option<&str>) - } fn get_source_html(file: &MetaFile, dirs: &Options) -> Result { - let file = metafile_to_string(file, dirs, Some("SOURCE"))?; + let file = metafile_to_string(file, dirs, Some("SOURCE")).wrap_err("failed building source")?; let mut pandoc = Pandoc::new(); pandoc @@ -104,7 +121,9 @@ fn get_pattern(key: &str, file: &MetaFile, dirs: &Options) -> Result { let mut path = dirs.pattern.join(pattern_path); path.set_extension("meta"); - return Ok(fs::read_to_string(path.to_str().unwrap_or_default())?); + let base = fs::read_to_string(&path) + .wrap_err_with(|| eyre!("could not read: {}\n", path.to_string_lossy()))?; + return Ok(base); } // BLANK returns nothing, so no more processing needs to be done @@ -120,8 +139,10 @@ fn get_pattern(key: &str, file: &MetaFile, dirs: &Options) -> Result { let mut path = dirs.pattern.join(pattern_path); path.set_extension("meta"); - let pattern = &fs::read_to_string(path.to_str().unwrap_or_default())?; - let mut pattern = parse_file(pattern)?; + let pattern = &fs::read_to_string(&path) + .wrap_err_with(|| eyre!("could not read: {}\n", path.to_string_lossy()))?; + let mut pattern = parse_file(pattern) + .wrap_err_with(|| eyre!("could not parse: {}\n", path.to_string_lossy()))?; // copy over maps for expanding contained variables pattern.variables = file.variables.clone(); @@ -132,11 +153,14 @@ fn get_pattern(key: &str, file: &MetaFile, dirs: &Options) -> Result { } fn find_dest(path: &Path, dirs: &Options) -> Result { - let source = dirs.source.to_string_lossy().to_string(); - let build = dirs.build.to_string_lossy().to_string(); - - let path = path.canonicalize()?.to_string_lossy().to_string(); - let path = path.replace(&source, &build); + let source = dirs.source.to_string_lossy(); + let build = dirs.build.to_string_lossy(); + + let path = path + .canonicalize() + .wrap_err_with(|| eyre!("could not get absolute path: {}\n", path.to_string_lossy()))?; + let path = path.to_string_lossy(); + let path = path.replace(&*source, &*build); let mut path = PathBuf::from(path); path.set_extension("html"); diff --git a/src/main.rs b/src/main.rs index ce82dbe..2146e62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,34 @@ -use color_eyre::Result; -use metaforge::parse_opts; +use color_eyre::{eyre::bail, Result}; +use metaforge::{build_metafile, parse_opts}; +use std::path::PathBuf; +use walkdir::WalkDir; fn main() -> Result<()> { + color_eyre::install()?; + let opts = parse_opts()?; - println!("{:?}", opts); + + let files: Vec = WalkDir::new(&opts.source) + .into_iter() + .filter_map(|file| file.ok()) + .filter(|file| file.file_type().is_file()) + .map(|file| file.into_path()) + .collect(); + + for file in files.iter() { + match build_metafile(file, &opts) { + Ok(_) => continue, + Err(e) => { + if opts.force { + // print a line to stderr about failure but continue with other files + eprintln!("error in {}: {}", file.to_string_lossy(), e); + continue; + } else { + bail!("error in {}: {}", file.to_string_lossy(), e); + } + } + } + } + Ok(()) } diff --git a/src/tests/test_metafile.rs b/src/tests/test_metafile.rs index 8c1cbff..f2f7c22 100644 --- a/src/tests/test_metafile.rs +++ b/src/tests/test_metafile.rs @@ -2,7 +2,7 @@ use crate::{parse_file, source, Source, Substitution}; use color_eyre::Result; use pretty_assertions::assert_eq; -static SOURCE: &str = include_str!("../../test_site/source/test_source.meta"); +static SOURCE: &str = include_str!("../../tests/files/test_source.meta"); static PATTERN: &str = include_str!("../../test_site/pattern/test/pattern.meta"); #[test] diff --git a/test_site/source/sub_dir/sub_source.meta b/test_site/source/sub_dir/sub_source.meta deleted file mode 100644 index 2ddc53b..0000000 --- a/test_site/source/sub_dir/sub_source.meta +++ /dev/null @@ -1,33 +0,0 @@ -${ - overwrite = 'GOOD' - new = 'GOOD' - blank = BLANK -} - -@{ - overwrite = ['GOOD'] - new = ['GOOD'] -} - -&{ - test.overwrite = 'overwrite' - test.new = 'new' - test.blank = BLANK - test.default = DEFAULT -} - -This is a test for a file in a sub_directory to test -overwriting variables and patterns. - -var [OVERWRITE]: ${overwrite} -var [PARENT]: ${parent} -var [NEW]: ${new} -var [BLANK]: ${blank} - -arr [OVERWRITE]: @{overwrite} -arr [NEW]: @{new} - -pat [OVERWRITE]: &{test.overwrite} -pat [NEW]: &{test.new} -pat [BLANK]: &{test.blank} -pat [DEFAULT]: &{test.default} diff --git a/test_site/source/test_source.meta b/tests/files/test_source.meta similarity index 100% rename from test_site/source/test_source.meta rename to tests/files/test_source.meta -- 2.45.2