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
/// Expects a function typically used for JsonLogic operators in the scope this macro is called in:
///
/// ```ignore
/// fn compute(args: &[Expression], data: &Data) -> Value
/// ```
///
/// Calls the `compute` function with the given arguments wrapped in `Expression::Constant` and an
/// empty data object created with `Data::empty()`.
#[macro_export]
macro_rules! compute_const {
($($args:expr),*) => {{
#[allow(unused_mut)]
let mut args_vec = vec![];
$(
args_vec.push($args);
)*
let expressions: Vec<Expression> = args_vec
.iter()
.map(|arg| Expression::Constant(&arg))
.collect();
compute(&expressions, &Data::empty())
}}
}
/// Expects a function typically used for JsonLogic operators in the scope this macro is called in:
///
/// ```ignore
/// fn compute(args: &[Expression], data: &Data) -> Value
/// ```
///
/// Expects two arguments. First an slice containing `Value`'s and second a `Data` instance.
/// Calls the `compute` function with the given `Value` slice where every element is wrapped in
/// `Expression::Constant` and the given data instance.
#[macro_export]
macro_rules! compute_const_with_data {
($args:expr, $data:expr) => {{
// Avoid "temporary value dropped while borrowed" errors.
let args: &[Value] = $args;
let expressions: Vec<Expression> = args
.iter()
.map(|ref arg| Expression::Constant(&arg))
.collect();
compute(&expressions, $data)
}};
}