Skip to main content

CompiledTemplate

Struct CompiledTemplate 

Source
pub struct CompiledTemplate { /* private fields */ }
Expand description

A parsed template that can be evaluated multiple times without re-parsing.

Use this when the same template source will be evaluated against different contexts or at different points in time.

use weaver_lang::{CompiledTemplate, SimpleContext, Registry};

let template = CompiledTemplate::compile("HP: {{global:hp}}").unwrap();
let registry = Registry::new();

let mut ctx = SimpleContext::new();
ctx.set("global", "hp", 100i64);
assert_eq!(template.evaluate(&mut ctx, &registry).unwrap(), "HP: 100");

ctx.set("global", "hp", 75i64);
assert_eq!(template.evaluate(&mut ctx, &registry).unwrap(), "HP: 75");

Implementations§

Source§

impl CompiledTemplate

Source

pub fn compile(source: &str) -> Result<Self, Vec<ParseError>>

Parse source text into a compiled template.

Returns parse errors if the source contains invalid syntax.

Source

pub fn evaluate( &self, ctx: &mut impl EvalContext, registry: &Registry, ) -> Result<String, EvalError>

Evaluate this template against the given context and registry.

Source

pub fn evaluate_with_options( &self, ctx: &mut impl EvalContext, registry: &Registry, options: EvalOptions, ) -> Result<String, EvalError>

Evaluate this template with custom options.

Source

pub fn ast(&self) -> &Template

Access the underlying AST for inspection or analysis.

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> 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, 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.