pub trait Filter: Send + Sync + Debug + Display {
    fn evaluate(
        &self,
        input: &dyn ValueView,
        runtime: &dyn Runtime
    ) -> Result<Value>; }
Expand description

A trait that holds a filter, ready to evaluate.

Deriving

You cannot derive Filter, as it would go against the very point of creating your own filter. You can, however, derive some other traits that are necessary in order to implement it.

In order to implement this trait, the struct must also implement Debug and Display, as well as either Default or From<T> (where T is the FilterParameters struct), respectively, in a filter without or with parameters.

For Debug and Default, one may use rust’s #[derive(Debug)] and #[derive(Default)] macros. For Display and From<T>, one may use liquid-derive’s #[derive(Display_filter)] and #[derive(FromFilterParameters)].

Note, however, that you may need helper attributes like #[name = "..."] and #[parameters] for using liquid-derive's macros. See documentation on liquid-derive` for more information.

Examples

Filter for filter with no arguments:

#[derive(Debug, Default, Display_filter)]
#[name = "abs"] // The name of the filter, for `Display_filter`.
struct AbsFilter; // There are no parameters, so implements `Default`.

impl Filter for AbsFilter {
    fn evaluate(&self, input: &dyn  ValueView, _runtime: &dyn Runtime) -> Result<Value> {
        // Implementation of the filter here
    }
}

Filter for filter with arguments:

#[derive(Debug, FromFilterParameters, Display_filter)]
#[name = "at_least"] // The name of the filter for derives
struct AtLeastFilter { // There are parameters, so derives `FromFilterParameters`.
    #[parameters] // Mark the FilterParameters struct for derives
    args: AtLeastArgs, // A struct that implements `FilterParameters`
}

impl Filter for AtLeastFilter {
    fn evaluate(&self, input: &ValueViwe, runtime: &dyn Runtime) -> Result<Value> {
        // Evaluate the `FilterParameters`
        let args = self.args.evaluate(runtime)?;

        // Implementation of the filter here
    }
}

Filter for a configurable filter:

#[derive(Debug, Display_filter)]
#[name = "example"] // The name of the filter for `Display`
// Because construction happens manually (without derive) in `FilterParser`
// no need to derive neither `Default` nor `FromFilterParameters`.
struct ExampleFilter {
    #[parameters] // Mark the FilterParameters struct for `Display`
    args: ExampleArgs, // A struct that implements `FilterParameters`
    state: i32, // See `ParseFilter` example for runtime
}

impl Filter for AtLeastFilter {
    fn evaluate(&self, input: &dyn ValueView, runtime: &dyn Runtime) -> Result<Value> {
        // Evaluate the `FilterParameters`
        let args = self.args.evaluate(runtime)?;

        // Implementation of the filter here
    }
}

Required methods

Implementors