Skip to main content

Template

Struct Template 

Source
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

Source

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 if key is truthy)
Source

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.

Source

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.

Source

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.

Source

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.

Source

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 is T.
  • Every downstream pipe’s input must match the previous pipe’s output (using types_compatible); mismatch returns an Err.
  • 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 an Err.

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Template

Source§

fn clone(&self) -> Template

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Template

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.