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    tera.add_raw_template(name, raw)
15        .with_context(|| format!("parse static template '{}'", name))?;
16    Ok(tera)
17}
18
19/// Render a previously-registered Tera template with `ctx`, returning the
20/// rendered string. Stage label is included in the error context so a render
21/// failure surfaces as `<stage>: render '<name>': <tera-msg>` rather than a
22/// panic.
23pub fn render_static(
24    tera: &tera::Tera,
25    name: &str,
26    ctx: &tera::Context,
27    stage: &str,
28) -> Result<String> {
29    tera.render(name, ctx)
30        .with_context(|| format!("{}: render '{}'", stage, name))
31}