Struct upon::Engine

source ·
pub struct Engine<'engine> { /* private fields */ }
Expand description

The compilation and rendering engine.

Implementations§

source§

impl<'engine> Engine<'engine>

source

pub fn new() -> Self

Construct a new engine.

source

pub fn with_syntax(syntax: Syntax<'engine>) -> Self

Construct a new engine with custom syntax.

§Examples
use upon::{Engine, Syntax};

let syntax = Syntax::builder().expr("<{", "}>").block("<[", "]>").build();
let engine = Engine::with_syntax(syntax);
source

pub fn set_max_include_depth(&mut self, depth: usize)

Set the maximum length of the template render stack.

This is the maximum number of nested {% include ... %} statements that are allowed during rendering, as counted from the root template.

Defaults to 64.

source

pub fn set_default_formatter<F>(&mut self, f: &'engine F)
where F: Fn(&mut Formatter<'_>, &Value) -> Result + Sync + Send + 'static,

Set the default formatter.

The default formatter defines how values are formatted in the rendered template. If not configured this defaults to fmt::default which does not perform any escaping. See the fmt module documentation for more information on value formatters.

source

pub fn add_formatter<N, F>(&mut self, name: N, f: F) -> Option<EngineFn>
where N: Into<Cow<'engine, str>>, F: Fn(&mut Formatter<'_>, &Value) -> Result + Sync + Send + 'static,

Add a new value formatter to the engine.

See the fmt module documentation for more information on formatters.

§Note

Formatters and filters share the same namespace. If a filter or formatter with the same name already exists in the engine, it is replaced and Some(_) with the type of function that was replaced is returned, else None is returned.

source

pub fn add_filter<N, F, R, A>(&mut self, name: N, f: F) -> Option<EngineFn>
where N: Into<Cow<'engine, str>>, F: Filter<R, A> + Send + Sync + 'static, R: FilterReturn, A: FilterArgs,

Available on crate feature filters only.

Add a new filter to the engine.

See the filters module documentation for more information on filters.

§Note

Formatters and filters share the same namespace. If a filter or formatter with the same name already exists in the engine, it is replaced and Some(_) with the type of function that was replaced is returned, else None is returned.

source

pub fn remove_function(&mut self, name: &str) -> Option<EngineFn>

Remove a formatter or filter by name.

§Note

Formatters and filters share the same namespace. If a filter or formatter with name existed in the engine, it is replaced and Some(_) with the type of function that was replaced is returned, else None is returned.

source

pub fn add_template<N, S>(&mut self, name: N, source: S) -> Result<()>
where N: Into<Cow<'engine, str>>, S: Into<Cow<'engine, str>>,

Add a template to the engine.

The template will be compiled and stored under the given name.

You can either pass a borrowed template (&str) or owned template (String) to this function. When passing a borrowed template, the lifetime needs to be at least as long as the engine lifetime. For shorter template lifetimes use .compile(..).

source

pub fn template(&self, name: &str) -> TemplateRef<'_>

Lookup a template by name.

§Panics

If the template with the given name does not exist.

source

pub fn get_template(&self, name: &str) -> Option<TemplateRef<'_>>

Lookup a template by name, returning None if it does not exist.

source

pub fn remove_template(&mut self, name: &str) -> bool

Remove a template by name.

Returns true if a template was removed, false if there was no template of that name.

source

pub fn compile<'source, S>(&self, source: S) -> Result<Template<'source>>
where S: Into<Cow<'source, str>>,

Compile a template.

The template will not be stored in the engine. The advantage over using .add_template(..) here is that if the template source is borrowed, it does not need to outlive the engine.

Trait Implementations§

source§

impl Debug for Engine<'_>

source§

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

Formats the value using the given formatter. Read more
source§

impl<'engine> Default for Engine<'engine>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'engine> !RefUnwindSafe for Engine<'engine>

§

impl<'engine> Send for Engine<'engine>

§

impl<'engine> Sync for Engine<'engine>

§

impl<'engine> Unpin for Engine<'engine>

§

impl<'engine> !UnwindSafe for Engine<'engine>

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

§

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

§

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.