1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use std::fmt::{Debug, Display};

use liquid_error::Result;
use liquid_interpreter::{Context, Expression};
use liquid_value::Value;

/// A structure that holds the information of a single parameter in a filter.
/// This includes its name, description and whether it is optional or required.
///
/// This is the return type in some `FilterReflection` functions.
pub struct ParameterReflection {
    pub name: &'static str,
    pub description: &'static str,
    pub is_optional: bool,
}

/// A trait that holds the information of the parameters of a filter.
///
/// All structs that implement `FilterParameters` must implement this.
/// This is actually automatically implemented with `#[derive(FilterParameters)]`.
///
/// This trait allows `FilterReflection` macro to extract the parameters information
/// from a given `FilterParameters` structure.
pub trait FilterParametersReflection {
    fn positional_parameters() -> &'static [ParameterReflection];
    fn keyword_parameters() -> &'static [ParameterReflection];
}

/// A trait that holds the information of a filter about itself, such as
/// its name, description and parameters.
///
/// All structs that implement `ParseFilter` must implement this.
///
/// # Deriving
///
/// This trait may be derived with `liquid-derive`'s `#[derive(FilterReflection)]`. However,
/// it is necessary to use the `#[filter(...)]`  helper attribute. See documentation on
/// `liquid-derive` for more information.
pub trait FilterReflection {
    fn name(&self) -> &'static str;
    fn description(&self) -> &'static str;

    fn positional_parameters(&self) -> &'static [ParameterReflection];
    fn keyword_parameters(&self) -> &'static [ParameterReflection];
}

/// A trait that declares and holds the parameters of a filter.
///
/// Provides `from_args`, to construct itself from `FilterArguments` (parses the arguments)
/// and `evaluate`, to construct its evaluated counterpart (evaluates the arguments).
///
/// # Deriving
///
/// The whole point of this structure is to facilitate the process of deriving a filter.
/// Thus, this trait and all traits it requires may be derived with `#[derive(Debug, FilterParameters)]`.
///
/// See documentation for `FilterParameters` macro on `liquid-derive` for more information.
pub trait FilterParameters<'a>: Sized + FilterParametersReflection + Debug + Display {
    type EvaluatedFilterParameters;
    fn from_args(args: FilterArguments) -> Result<Self>;
    fn evaluate(&'a self, context: &'a Context) -> Result<Self::EvaluatedFilterParameters>;
}

/// Structure that holds the unparsed arguments of a filter, both positional and keyword.
pub struct FilterArguments<'a> {
    pub positional: Box<Iterator<Item = Expression>>,
    pub keyword: Box<Iterator<Item = (&'a str, Expression)> + 'a>,
}

/// 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:
/// ```ignore
/// #[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: &Value, _context: &Context) -> Result<Value> {
///         // Implementation of the filter here
///     }
/// }
/// ```
///
/// Filter for filter with arguments:
/// ```ignore
/// #[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: &Value, context: &Context) -> Result<Value> {
///         // Evaluate the `FilterParameters`
///         let args = self.args.evaluate(context)?;
///
///         // Implementation of the filter here
///     }
/// }
/// ```
///
/// Filter for a configurable filter:
/// ```ignore
/// #[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 context
/// }
///
/// impl Filter for AtLeastFilter {
///     fn evaluate(&self, input: &Value, context: &Context) -> Result<Value> {
///         // Evaluate the `FilterParameters`
///         let args = self.args.evaluate(context)?;
///
///         // Implementation of the filter here
///     }
/// }
/// ```
pub trait Filter: Send + Sync + Debug + Display {
    // This will evaluate the expressions and evaluate the filter.
    fn evaluate(&self, input: &Value, context: &Context) -> Result<Value>;
}

/// 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:
/// ```ignore
/// #[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:
/// ```ignore
/// #[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:
/// ```ignore
/// #[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 }))
///     }
/// }
/// ```
pub trait ParseFilter: Send + Sync + ParseFilterClone + FilterReflection {
    /// Filter `input` based on `arguments`.
    fn parse(&self, arguments: FilterArguments) -> Result<Box<Filter>>;
}

/// Support cloning of `Box<ParseFilter>`.
pub trait ParseFilterClone {
    /// Cloning of `dyn ParseFilter`.
    fn clone_box(&self) -> Box<ParseFilter>;
}

impl<T> ParseFilterClone for T
where
    T: 'static + ParseFilter + Clone,
{
    fn clone_box(&self) -> Box<ParseFilter> {
        Box::new(self.clone())
    }
}

impl Clone for Box<ParseFilter> {
    fn clone(&self) -> Box<ParseFilter> {
        self.clone_box()
    }
}

impl<T> From<T> for Box<ParseFilter>
where
    T: 'static + ParseFilter,
{
    fn from(filter: T) -> Self {
        Box::new(filter)
    }
}