Jinja templating meets Rust code
use finja::finja;
#[finja(
welcomer => "println!",
greeting => "Hello, {}",
name => "guest2"
)]
fn main() {
let guest1 = "Universe";
let guest2 = "World";
{{ welcomer }}("{{ greeting }}", {{ name }});
}Which renders to:
fn main()
{
let guest1 = "Universe";
let guest2 = "World";
println!("Hello, {}", guest2);
}Move error-related code outside of the function body to make it less noisy.
use finja::finja;
#[finja(
OpenError => "|e| MyError::OpenError(path.to_path_buf(), e)",
ReadError => "|e| MyError::ReadError(path.to_path_buf(), e)",
FooError => "Err(MyError::ContentError(path.to_path_buf(), 42.to_string()))",
)]
fn process(path: impl AsRef<Path>) -> Result<(), MyError> {
let path = path.as_ref();
let mut file = File::open(path).map_err({{ OpenError }})?;
let mut contents = Vec::new();
file.read_to_end(&mut contents).map_err({{ ReadError }})?;
if !contents.windows(2).any(|w| w == b"42") {
return {{ FooError }};
}
Ok(())
}Add this to your Cargo.toml
[dependencies]
finja = { git = "https://github.com/synek317/finja" }And you are ready to use it. Currently the examples above are the only documentation available.