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, ®istry).unwrap(), "HP: 100");
ctx.set("global", "hp", 75i64);
assert_eq!(template.evaluate(&mut ctx, ®istry).unwrap(), "HP: 75");Implementations§
Source§impl CompiledTemplate
impl CompiledTemplate
Sourcepub fn compile(source: &str) -> Result<Self, Vec<ParseError>>
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.
Sourcepub fn evaluate(
&self,
ctx: &mut impl EvalContext,
registry: &Registry,
) -> Result<String, EvalError>
pub fn evaluate( &self, ctx: &mut impl EvalContext, registry: &Registry, ) -> Result<String, EvalError>
Evaluate this template against the given context and registry.
Sourcepub fn evaluate_with_options(
&self,
ctx: &mut impl EvalContext,
registry: &Registry,
options: EvalOptions,
) -> Result<String, EvalError>
pub fn evaluate_with_options( &self, ctx: &mut impl EvalContext, registry: &Registry, options: EvalOptions, ) -> Result<String, EvalError>
Evaluate this template with custom options.
Auto Trait Implementations§
impl Freeze for CompiledTemplate
impl RefUnwindSafe for CompiledTemplate
impl Send for CompiledTemplate
impl Sync for CompiledTemplate
impl Unpin for CompiledTemplate
impl UnsafeUnpin for CompiledTemplate
impl UnwindSafe for CompiledTemplate
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more