Trait rocket_dyn_templates::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