Trait minijinja::tests::Test

source ·
pub trait Test<Rv, Args>: Send + Sync + 'static { }
Expand description

A utility trait that represents test functions.

This trait is used by the add_test method to abstract over different types of functions that implement tests. Tests are similar to filters but they always return boolean values and use a slightly different syntax to filters. Like filters they accept the State by reference as first parameter and the value that that the test is applied to as second. Additionally up to 4 further parameters are supported.

A test function can return any of the following types:

  • bool
  • Result<bool, Error>

Tests accept one mandatory parameter which is the value the filter is applied to and up to 4 extra parameters. The extra parameters can be marked optional by using Option<T>. The last argument can also use Rest<T> to capture the remaining arguments. All types are supported for which ArgType is implemented.

For a list of built-in tests see tests.

§Basic Example

use minijinja::State;

fn is_lowercase(value: String) -> bool {
    value.chars().all(|x| x.is_lowercase())
}

env.add_test("lowercase", is_lowercase);
{{ "foo" is lowercase }} -> true

§Arguments and Optional Arguments

use minijinja::State;

fn is_containing(value: String, other: String) -> bool {
    value.contains(&other)
}

env.add_test("containing", is_containing);
{{ "foo" is containing("o") }} -> true

Implementors§

source§

impl<Func, Rv> Test<Rv, ()> for Func
where Func: Fn() -> Rv + Send + Sync + 'static, Rv: TestResult,

source§

impl<Func, Rv, A> Test<Rv, (A,)> for Func
where Func: Fn(A) -> Rv + Send + Sync + 'static, Rv: TestResult, A: for<'a> ArgType<'a>,

source§

impl<Func, Rv, A, B> Test<Rv, (A, B)> for Func
where Func: Fn(A, B) -> Rv + Send + Sync + 'static, Rv: TestResult, A: for<'a> ArgType<'a>, B: for<'a> ArgType<'a>,

source§

impl<Func, Rv, A, B, C> Test<Rv, (A, B, C)> for Func
where Func: Fn(A, B, C) -> Rv + Send + Sync + 'static, Rv: TestResult, A: for<'a> ArgType<'a>, B: for<'a> ArgType<'a>, C: for<'a> ArgType<'a>,

source§

impl<Func, Rv, A, B, C, D> Test<Rv, (A, B, C, D)> for Func
where Func: Fn(A, B, C, D) -> Rv + Send + Sync + 'static, Rv: TestResult, A: for<'a> ArgType<'a>, B: for<'a> ArgType<'a>, C: for<'a> ArgType<'a>, D: for<'a> ArgType<'a>,

source§

impl<Func, Rv, A, B, C, D, E> Test<Rv, (A, B, C, D, E)> for Func
where Func: Fn(A, B, C, D, E) -> Rv + Send + Sync + 'static, Rv: TestResult, A: for<'a> ArgType<'a>, B: for<'a> ArgType<'a>, C: for<'a> ArgType<'a>, D: for<'a> ArgType<'a>, E: for<'a> ArgType<'a>,