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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! An abstraction over any filter function or closure.
//!
//! The [`Filter`] trait is used by the
//! [`Engine::add_filter`][crate::Engine::add_filter] method to abstract over a
//! variety of function and closure types. This includes filters with variable
//! argument types, return types and arity. The first argument to a filter
//! function will always receive the piped value or expression. It can then have
//! up to four more arguments. The renderer will check the number of arguments
//! and the type of arguments when the filter is used. Generally you should not
//! try to implement any of the traits in this module yourself, instead you
//! should define functions or closures that adhere to the generic
//! implementation provided.
//!
//! ## Types
//!
//! [`Filter`] is implemented for functions and closures that take any owned
//! argument implementing [`FilterArg`] and any return type implementing
//! [`FilterReturn`].
//!
//! Additionally, the _first_ argument to the filter (i.e. the piped expression)
//! can also be specified using the following reference types. This is preferred
//! in most cases because the renderer won't have to clone the value before
//! passing it to the filter.
//! - [`&str`][str]
//! - [`&[Value]`][slice]
//! - [`&BTreeMap<String, Value>`][std::collections::BTreeMap]
//! - [`&Value`][Value]
//!
//! Other arguments can also use [`&str`][str] but only if the passed parameter
//! is always a literal string.
//!
//! # Examples
//!
//! ## Using existing functions
//!
//! A lot of standard library functions can be used as filters, as long as they
//! have the supported argument and return types.
//!
//! ```
//! let mut engine = upon::Engine::new();
//! engine.add_filter("lower", str::to_lowercase);
//! engine.add_filter("abs", i64::abs);
//! ```
//!
//! ## Closures
//!
//! Closures are perfectly valid filters.
//!
//! ```
//! let mut engine = upon::Engine::new();
//! engine.add_filter("add", |a: i64, b: i64| a + b);
//! ```
//!
//! This could be use like this
//!
//! ```text
//! {{ user.age | add: 10 }}
//! ```
//!
//! ## Owned vs reference arguments
//!
//! Consider the following template.
//!
//! ```text
//! {{ users | last }}
//! ```
//!
//! Where the `last` filter retrieves the final element in a list. We could
//! implement this filter taking an owned argument.
//!
//! ```rust
//! # use upon::Value;
//! fn last(mut list: Vec<Value>) -> Option<Value> {
//!     list.pop()
//! }
//! ```
//!
//! But it would be more efficient to implement it such that it takes a slice,
//! because then only the last element is cloned, as opposed to all the elements
//! in the list being cloned.
//!
//! ```
//! # use upon::Value;
//! fn last(list: &[Value]) -> Option<Value> {
//!     list.last().map(Clone::clone)
//! }
//! ```

mod args;
mod impls;

use crate::render::{FilterState, Stack};
use crate::types::ast::BaseExpr;
use crate::types::span::Span;
use crate::value::ValueCow;
use crate::{Error, Result, Value};

pub(crate) type FilterFn = dyn Fn(FilterState<'_>) -> Result<Value> + Send + Sync + 'static;

pub(crate) fn new<F, R, A>(f: F) -> Box<FilterFn>
where
    F: Filter<R, A> + Send + Sync + 'static,
    R: FilterReturn,
    A: FilterArgs,
{
    Box::new(move |state: FilterState<'_>| -> Result<Value> {
        let args = A::from_state(state)?;
        let result = Filter::filter(&f, args);
        FilterReturn::to_value(result)
    })
}

/// Any filter function.
///
/// *See the [module][crate::filters] documentation for more information.*
#[cfg_attr(docsrs, doc(cfg(feature = "filters")))]
pub trait Filter<R, A>
where
    A: FilterArgs,
{
    #[doc(hidden)]
    fn filter(&self, args: <A as FilterArgs>::Output<'_>) -> R;
}

/// The set of arguments to a filter.
///
/// *See the [module][crate::filters] documentation for more information.*
#[cfg_attr(docsrs, doc(cfg(feature = "filters")))]
pub trait FilterArgs {
    #[doc(hidden)]
    type Output<'a>;
    #[doc(hidden)]
    fn from_state(state: FilterState<'_>) -> Result<Self::Output<'_>>;
}

/// An argument to a filter.
///
/// *See the [module][crate::filters] documentation for more information.*
#[cfg_attr(docsrs, doc(cfg(feature = "filters")))]
pub trait FilterArg {
    #[doc(hidden)]
    type Output<'a>;
    #[doc(hidden)]
    fn from_value<'a>(v: Value) -> args::Result<Self::Output<'a>>;
    #[doc(hidden)]
    fn from_value_ref(v: &Value) -> args::Result<Self::Output<'_>>;
    #[doc(hidden)]
    fn from_cow_mut<'a>(v: &'a mut ValueCow<'a>) -> args::Result<Self::Output<'a>>;
}

/// A return value from a filter.
///
/// This trait is implemented for many types by utilizing the [`From`]
/// implementations for [`Value`].
///
/// - `R` where `R` implements `Into<Value>`
/// - `Result<R, E>` where `R` implements `Into<Value>` and `E` implements
///   [`FilterError`].
///
/// *See the [module][crate::filters] documentation for more information.*
#[cfg_attr(docsrs, doc(cfg(feature = "filters")))]
pub trait FilterReturn {
    #[doc(hidden)]
    fn to_value(self) -> Result<Value>;
}

/// A value returned from a filter.
///
/// *See the [module][crate::filters] documentation for more information.*
pub trait FilterError {
    #[doc(hidden)]
    fn to_error(self) -> Error;
}

////////////////////////////////////////////////////////////////////////////////
// Filter
////////////////////////////////////////////////////////////////////////////////

impl<Func, R, V> Filter<R, (V,)> for Func
where
    Func: Fn(V) -> R,
    R: FilterReturn,

    V: for<'a> FilterArg<Output<'a> = V>,

    (V,): for<'a> FilterArgs<Output<'a> = (V,)>,
{
    #[doc(hidden)]
    fn filter<'a>(&self, (v,): (V,)) -> R {
        self(v)
    }
}

impl<Func, R, V, A> Filter<R, (V, A)> for Func
where
    Func: Fn(V, A) -> R,
    R: FilterReturn,

    V: for<'a> FilterArg<Output<'a> = V>,
    A: for<'a> FilterArg<Output<'a> = A>,

    (V, A): for<'a> FilterArgs<Output<'a> = (V, A)>,
{
    #[doc(hidden)]
    fn filter<'a>(&self, (v, a): (V, A)) -> R {
        self(v, a)
    }
}

impl<Func, R, V, A, B> Filter<R, (V, A, B)> for Func
where
    Func: Fn(V, A, B) -> R,
    R: FilterReturn,

    V: for<'a> FilterArg<Output<'a> = V>,
    A: for<'a> FilterArg<Output<'a> = A>,
    B: for<'a> FilterArg<Output<'a> = B>,

    (V, A, B): for<'a> FilterArgs<Output<'a> = (V, A, B)>,
{
    #[doc(hidden)]
    fn filter<'a>(&self, (v, a, b): (V, A, B)) -> R {
        self(v, a, b)
    }
}

impl<Func, R, V, A, B, C> Filter<R, (V, A, B, C)> for Func
where
    Func: Fn(V, A, B, C) -> R,
    R: FilterReturn,

    V: for<'a> FilterArg<Output<'a> = V>,
    A: for<'a> FilterArg<Output<'a> = A>,
    B: for<'a> FilterArg<Output<'a> = B>,
    C: for<'a> FilterArg<Output<'a> = C>,

    (V, A, B, C): for<'a> FilterArgs<Output<'a> = (V, A, B, C)>,
{
    #[doc(hidden)]
    fn filter<'a>(&self, (v, a, b, c): (V, A, B, C)) -> R {
        self(v, a, b, c)
    }
}

impl<Func, R, V, A, B, C, D> Filter<R, (V, A, B, C, D)> for Func
where
    Func: Fn(V, A, B, C, D) -> R,
    R: FilterReturn,

    V: for<'a> FilterArg<Output<'a> = V>,
    A: for<'a> FilterArg<Output<'a> = A>,
    B: for<'a> FilterArg<Output<'a> = B>,
    C: for<'a> FilterArg<Output<'a> = C>,
    D: for<'a> FilterArg<Output<'a> = D>,

    (V, A, B, C, D): for<'a> FilterArgs<Output<'a> = (V, A, B, C, D)>,
{
    #[doc(hidden)]
    fn filter<'a>(&self, (v, a, b, c, d): (V, A, B, C, D)) -> R {
        self(v, a, b, c, d)
    }
}

////////////////////////////////////////////////////////////////////////////////
// FilterArgs
////////////////////////////////////////////////////////////////////////////////

impl<V> FilterArgs for (V,)
where
    V: FilterArg,
{
    type Output<'a> = (V::Output<'a>,);

    fn from_state(state: FilterState<'_>) -> Result<Self::Output<'_>> {
        check_args(&state, 0)?;
        let err = |e| err_expected_val(e, state.source, state.filter.span);
        let v = V::from_cow_mut(state.value).map_err(err)?;
        Ok((v,))
    }
}

impl<V, A> FilterArgs for (V, A)
where
    V: FilterArg,
    A: FilterArg,
{
    type Output<'a> = (V::Output<'a>, A::Output<'a>);

    fn from_state(state: FilterState<'_>) -> Result<Self::Output<'_>> {
        check_args(&state, 1)?;
        let err = |e| err_expected_val(e, state.source, state.filter.span);
        let v = V::from_cow_mut(state.value).map_err(err)?;
        let a = get_arg::<A>(state.source, state.stack, state.args, 0)?;
        Ok((v, a))
    }
}

impl<V, A, B> FilterArgs for (V, A, B)
where
    V: FilterArg,
    A: FilterArg,
    B: FilterArg,
{
    type Output<'a> = (V::Output<'a>, A::Output<'a>, B::Output<'a>);

    fn from_state(state: FilterState<'_>) -> Result<Self::Output<'_>> {
        check_args(&state, 2)?;
        let err = |e| err_expected_val(e, state.source, state.filter.span);
        let v = V::from_cow_mut(state.value).map_err(err)?;
        let a = get_arg::<A>(state.source, state.stack, state.args, 0)?;
        let b = get_arg::<B>(state.source, state.stack, state.args, 1)?;
        Ok((v, a, b))
    }
}

impl<V, A, B, C> FilterArgs for (V, A, B, C)
where
    V: FilterArg,
    A: FilterArg,
    B: FilterArg,
    C: FilterArg,
{
    type Output<'a> = (V::Output<'a>, A::Output<'a>, B::Output<'a>, C::Output<'a>);

    fn from_state(state: FilterState<'_>) -> Result<Self::Output<'_>> {
        check_args(&state, 3)?;
        let err = |e| err_expected_val(e, state.source, state.filter.span);
        let v = V::from_cow_mut(state.value).map_err(err)?;
        let a = get_arg::<A>(state.source, state.stack, state.args, 0)?;
        let b = get_arg::<B>(state.source, state.stack, state.args, 1)?;
        let c = get_arg::<C>(state.source, state.stack, state.args, 2)?;
        Ok((v, a, b, c))
    }
}

impl<V, A, B, C, D> FilterArgs for (V, A, B, C, D)
where
    V: FilterArg,
    A: FilterArg,
    B: FilterArg,
    C: FilterArg,
    D: FilterArg,
{
    type Output<'a> = (
        V::Output<'a>,
        A::Output<'a>,
        B::Output<'a>,
        C::Output<'a>,
        D::Output<'a>,
    );

    fn from_state(state: FilterState<'_>) -> Result<Self::Output<'_>> {
        check_args(&state, 4)?;
        let err = |e| err_expected_val(e, state.source, state.filter.span);
        let v = V::from_cow_mut(state.value).map_err(err)?;
        let a = get_arg::<A>(state.source, state.stack, state.args, 0)?;
        let b = get_arg::<B>(state.source, state.stack, state.args, 1)?;
        let c = get_arg::<C>(state.source, state.stack, state.args, 2)?;
        let d = get_arg::<D>(state.source, state.stack, state.args, 3)?;
        Ok((v, a, b, c, d))
    }
}

fn check_args(state: &FilterState<'_>, exp: usize) -> Result<()> {
    if state.args.len() == exp {
        Ok(())
    } else {
        Err(Error::render(
            format!("filter expected {exp} arguments"),
            state.source,
            state.filter.span,
        ))
    }
}

fn get_arg<'a, T>(
    source: &str,
    stack: &'a Stack<'a>,
    args: &'a [BaseExpr],
    i: usize,
) -> Result<T::Output<'a>>
where
    T: FilterArg,
{
    match &args[i] {
        BaseExpr::Var(var) => match stack.lookup_var(source, var)? {
            ValueCow::Borrowed(v) => {
                T::from_value_ref(v).map_err(|e| err_expected_arg(e, source, var.span()))
            }
            ValueCow::Owned(v) => {
                T::from_value(v).map_err(|e| err_expected_arg(e, source, var.span()))
            }
        },
        BaseExpr::Literal(lit) => {
            T::from_value_ref(&lit.value).map_err(|e| err_expected_arg(e, source, lit.span))
        }
    }
}

fn err_expected_arg(err: args::Error, source: &str, span: Span) -> Error {
    let msg = match err {
        args::Error::Type(exp, got) => {
            format!("filter expected {exp} argument, found {got}")
        }
        args::Error::Reference(got) => {
            format!("filter expected reference argument but this {got} can only be passed as owned",)
        }
        args::Error::TryFromInt(want, value) => {
            format!("filter expected {want} argument, but `{value}` is out of range",)
        }
    };
    Error::render(msg, source, span)
}

fn err_expected_val(err: args::Error, source: &str, span: Span) -> Error {
    let msg = match err {
        args::Error::Type(exp, got) => {
            format!("filter expected {exp} value, found {got}")
        }
        args::Error::Reference(_) => {
            unreachable!()
        }
        args::Error::TryFromInt(want, value) => {
            format!("filter expected {want} value, but `{value}` is out of range",)
        }
    };
    Error::render(msg, source, span)
}

////////////////////////////////////////////////////////////////////////////////
// FilterReturn
////////////////////////////////////////////////////////////////////////////////

impl<T> FilterReturn for T
where
    T: Into<Value>,
{
    fn to_value(self) -> Result<Value> {
        Ok(self.into())
    }
}

impl<T, E> FilterReturn for std::result::Result<T, E>
where
    T: Into<Value>,
    E: FilterError,
{
    fn to_value(self) -> Result<Value> {
        self.map(Into::into).map_err(FilterError::to_error)
    }
}

////////////////////////////////////////////////////////////////////////////////
// FilterError
////////////////////////////////////////////////////////////////////////////////

impl FilterError for String {
    fn to_error(self) -> Error {
        Error::filter(self)
    }
}

impl FilterError for &str {
    fn to_error(self) -> Error {
        Error::filter(self)
    }
}