[][src]Trait liquid_compiler::ParseFilter

pub trait ParseFilter: Send + Sync + ParseFilterClone {
    fn parse(&self, arguments: FilterArguments) -> Result<Box<dyn Filter>>;
fn reflection(&self) -> &dyn FilterReflection; }

A trait to register a new filter in the liquid::Parser.

To implement this trait, the structure must also implement FilterReflection, thus giving meta information about the filter (namely it's name).

Every time a filter with that name is encountered, it is parsed with the ParseFilter::parse method, yielding a new Filter.

Deriving

In order to implement this trait, the struct must also implement FilterReflection and Clone.

Clone may be derived with rust's #[derive(Clone)]. FilterReflection may be derived with liquid-derive's #[derive(FilterReflection)]. ParseFilter may be derived with #[derive(FilterReflection)].

In order to use liquid-derive's macros, however, it is necessary to use the #[filter(...)] helper attribute. See documentation on liquid-derive for more information.

Examples

ParseFilter for filter with no arguments:

This example is not tested
#[derive(Clone, ParseFilter, FilterReflection)]
#[filter(
    name = "abs",
    description = "Returns the absolute value of a number.",
    parsed(AbsFilter) // A struct that implements `Filter` (must implement `Default`)
)]
pub struct Abs;

ParseFilter for filter with arguments:

This example is not tested
#[derive(Clone, ParseFilter, FilterReflection)]
#[filter(
    name = "slice",
    description = "Takes a slice of a given string or array.",
    parameters(SliceArgs), // A struct that implements `FilterParameters`
    parsed(SliceFilter) // A struct that implements `Filter` (must implement `From<SliceArgs>`)
)]
pub struct Slice;

ParseFilter for a configurable filter:

This example is not tested
#[derive(Clone, FilterReflection)]
#[filter(
    name = "example",
    description = "This filter exists for example purposes.",
    parameters(ExampleArgs) // A struct that implements `FilterParameters`
)]
pub struct ExampleParser {
    // You can add as many fields as you find necessary to configure the filter
    // before registering it.
    pub mode: i32,
}

// For configurable filters, there is no default implementation of `ParseFilter`
impl ParseFilter for ExampleParser {
    fn parse(&self, arguments: FilterArguments) -> Result<Box<Filter>> {
        // Create the `FilterParameters` struct from the given `arguments`
        let args = ExampleArgs::from_args(arguments)?;
        // Use the configuration state of the `ParseFilter`
        let state = self.state;

        // Create the `Filter` struct and return it, passing the information
        // about the arguments and the configuration of the `ParseFilter`.
        Ok(Box::new(ExampleFilter { args, state }))
    }
}

Required methods

fn parse(&self, arguments: FilterArguments) -> Result<Box<dyn Filter>>

Filter input based on arguments.

fn reflection(&self) -> &dyn FilterReflection

Loading content...

Implementors

Loading content...