pub struct Template {
pub source: String,
pub segments: Vec<Segment>,
}Expand description
A parsed template ready for rendering.
Fields§
§source: String§segments: Vec<Segment>Implementations§
Source§impl Template
impl Template
Sourcepub fn parse(source: &str) -> Result<Self, ProsaicError>
pub fn parse(source: &str) -> Result<Self, ProsaicError>
Parse a template string into segments.
Syntax:
{key}— substitute value from context{key|pipe}— apply a pipe transform{key|pipe:arg}— pipe with an argument{key|pipe1|pipe2:arg}— chained pipes{?key}...{/?}— conditional section (renders only ifkeyis truthy)
Sourcepub fn literal_tokens(&self) -> Vec<&str>
pub fn literal_tokens(&self) -> Vec<&str>
Return the text of every literal segment in this template.
Walks the segment tree recursively, collecting text from literal nodes at every nesting depth (including inside conditional sections). Partial-inclusion nodes are treated as opaque — their literals are only reachable after the engine expands them at render time. Callers that need partial content to contribute to faithfulness scoring should pre-expand partials or disable the gate for partial-heavy templates.
Used by faithfulness scoring to include template boilerplate in the entailment source set alongside context values.
Sourcepub fn slot_keys(&self) -> Vec<String>
pub fn slot_keys(&self) -> Vec<String>
Every slot key referenced by this template, including condition keys
from conditional sections ({?key}...{/?}).
Walks the segment tree recursively. Partial nodes are skipped — their
slot keys are only reachable after the engine expands them at render time.
The returned list may contain duplicates (e.g. when a key appears in both
a conditional guard and its body). Used by the prosaic_template! proc macro
for compile-time slot validation.
Sourcepub fn pipe_names(&self) -> Vec<String>
pub fn pipe_names(&self) -> Vec<String>
Every pipe name referenced by any slot in this template.
Walks the segment tree recursively. Returns the pipe name only (not any
argument, e.g. "pluralize" for {count|pluralize:item}). May contain
duplicates if the same pipe appears more than once. Used by the
prosaic_template! proc macro for compile-time pipe validation.
Sourcepub fn partial_names(&self) -> Vec<String>
pub fn partial_names(&self) -> Vec<String>
Every partial name referenced by this template via {>name}.
Walks the segment tree recursively. Used by the engine at
register_partial time to detect direct and indirect cycles
before they can produce a stack overflow at render time.
Sourcepub fn infer_types(&self) -> Result<Vec<(String, ValueType)>, String>
pub fn infer_types(&self) -> Result<Vec<(String, ValueType)>, String>
Infer the ValueType required for each slot, based on pipe-chain flow.
Walks every slot (including condition keys in {?key}...{/?} and
slots nested inside conditionals). For each slot:
- If it is used bare, its inferred type is
Any. - If its first pipe has input type
T, the slot type isT. - Every downstream pipe’s input must match the previous pipe’s output
(using
types_compatible); mismatch returns anErr. - When a slot appears multiple times, the inferred types are unified
by intersection:
Any ∩ T → T,T ∩ T → T, and two distinct concrete types produce anErr.
Unknown pipe names produce an Err. Slots inside {>partial} are
skipped (partials are opaque at parse time).
Returns a Vec<(slot_name, inferred_type)> in unspecified order on
success, or a human-readable error string on conflict.
Sourcepub fn as_bare_slots(&self) -> Option<Vec<BareSegment<'_>>>
pub fn as_bare_slots(&self) -> Option<Vec<BareSegment<'_>>>
Decompose this template into bare segments (literal text and bare slot
references with no pipes), returning None if the template contains
any pipes, conditional sections, or partial inclusions.
Used by the prosaic_template_compiled! proc macro for compile-time
code generation. Not intended for general use.