commit 69b7a05e48565b89af0fb854839d2384e4bd544f Author: kitsunecafe <10284516+kitsunecafe@users.noreply.github.com> Date: Mon Jan 29 05:41:13 2024 -0500 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f67777b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "roxy_tera_parser" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +roxy_core = { git = "https://github.com/kitsunecafe/roxy-core.git" } +tera = "1.19.1" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..bcf2460 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,37 @@ +use roxy_core::roxy::Parse; + +#[derive(Debug, Default)] +pub struct TeraParser { + pub tera: tera::Tera, + context: tera::Context, +} + +impl TeraParser { + pub fn new(tera: tera::Tera, context: tera::Context) -> Self { + Self { tera, context } + } +} + +impl Parse for TeraParser { + fn parse(&mut self, path: &str, src: &[u8], dst: &mut Vec) -> std::io::Result<()> { + // TODO: This error is a hack + let err = |e: tera::Error| { + println!("{e:?}"); + std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()) + }; + let template = String::from_utf8_lossy(src).to_string(); + + self.tera + .add_raw_template(path, template.as_str()) + .map_err(err)?; + + self.tera.render_to(path, &self.context, dst).map_err(err) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + } +}