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 the error still flows through `Result` rather than a panic site,
9/// so the anti-pattern hook stays clean and the caller's stage label reaches
10/// the user as `.with_context(...)?`.
11pub fn parse_static(name: &str, raw: &str) -> Result<tera::Tera> {
12 let mut tera = tera::Tera::default();
13 // Empty suffix list = escape nothing; the element type must be named for
14 // 2.0's generic `IntoIterator` signature to infer.
15 tera.autoescape_on(Vec::<&str>::new());
16 super::base_tera::register_ruby_escape(&mut tera);
17 // Same raw-string-literal restoration the dynamic render path applies:
18 // the embedded templates were authored against 1.x's raw literals.
19 let raw = super::engine_adapter::double_string_literal_backslashes(raw);
20 tera.add_raw_template(name, raw.as_ref())
21 .with_context(|| format!("parse static template '{}'", name))?;
22 Ok(tera)
23}
24
25/// Render a previously-registered Tera template with `ctx`, returning the
26/// rendered string. Stage label is included in the error context so a render
27/// failure surfaces as `<stage>: render '<name>': <tera-msg>` rather than a
28/// panic.
29pub fn render_static(
30 tera: &tera::Tera,
31 name: &str,
32 ctx: &tera::Context,
33 stage: &str,
34) -> Result<String> {
35 tera.render(name, ctx)
36 .with_context(|| format!("{}: render '{}'", stage, name))
37}