Skip to main content

anodizer_core/template/
static_render.rs

1use anyhow::{Context as _, Result};
2
3/// Parse a static (compile-time) Tera template, returning a `tera::Tera`
4/// instance with the template registered under `name`.
5///
6/// Use this for "trusted" templates baked into the binary (PKGBUILD body,
7/// cask/formula skeletons, nuspec, etc.) where parse failure is a programmer
8/// bug, but we still want the error to flow through `Result` rather than a
9/// panic site so the anti-pattern hook stays clean and the caller's stage
10/// label reaches the user as `.with_context(...)?`.
11pub fn parse_static(name: &str, raw: &str) -> Result<tera::Tera> {
12    let mut tera = tera::Tera::default();
13    tera.autoescape_on(vec![]);
14    super::base_tera::register_ruby_escape(&mut tera);
15    tera.add_raw_template(name, raw)
16        .with_context(|| format!("parse static template '{}'", name))?;
17    Ok(tera)
18}
19
20/// Render a previously-registered Tera template with `ctx`, returning the
21/// rendered string. Stage label is included in the error context so a render
22/// failure surfaces as `<stage>: render '<name>': <tera-msg>` rather than a
23/// panic.
24pub fn render_static(
25    tera: &tera::Tera,
26    name: &str,
27    ctx: &tera::Context,
28    stage: &str,
29) -> Result<String> {
30    tera.render(name, ctx)
31        .with_context(|| format!("{}: render '{}'", stage, name))
32}