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
//! Built-in functions, and the [`Function`] trait for defining your own.
//!
//! Filters may call functions using the `name(args...)` syntax. The crate ships
//! a small base set (see [`base_functions`]) — currently [`Trim`] and, with the
//! `chrono` feature, [`Now`] — but you can add your own by implementing
//! [`Function`] and passing instances to
//! [`Filter::with_functions`](crate::Filter::with_functions).
use Cow;
use ;
use crateFilterValue;
pub use Trim;
pub use Now;
/// A function which can be called from within a filter expression using the
/// familiar `name(args...)` syntax.
///
/// The crate provides a small set of built-in functions, but you can define
/// your own by implementing this trait and registering instances with
/// [`Filter::with_functions`](crate::Filter::with_functions). A function
/// reports its [`name`](Function::name) and [`arity`](Function::arity) so the
/// parser can resolve and validate calls up-front, and evaluates via
/// [`call`](Function::call).
///
/// Implementations must be `Send + Sync` so that a parsed [`Filter`](crate::Filter)
/// can be shared across threads.
///
/// ```
/// use std::borrow::Cow;
/// use filt_rs::{FilterValue, Function};
///
/// /// A `len(string)` function returning the number of characters in its argument.
/// struct Len;
///
/// impl Function for Len {
/// fn name(&self) -> &str {
/// "len"
/// }
///
/// fn arity(&self) -> usize {
/// 1
/// }
///
/// fn call<'a>(&self, args: &[Cow<'a, FilterValue<'a>>]) -> Cow<'a, FilterValue<'a>> {
/// match args[0].as_ref() {
/// FilterValue::String(s) => Cow::Owned(FilterValue::Number(s.chars().count() as f64)),
/// _ => Cow::Owned(FilterValue::Null),
/// }
/// }
/// }
///
/// assert_eq!(Len.name(), "len");
/// assert_eq!(Len.arity(), 1);
/// ```
/// Returns the shared set of built-in functions available in every filter,
/// regardless of how it is constructed.
///
/// The set is built once and shared (reference-counted) thereafter, so cloning
/// the returned handle is cheap.
pub