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
use super::*;
use indexmap::map::Entry;
use std::hash::Hash;
impl private::Sealed for Body {}
impl Evaluate for Body {
type Output = Self;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
self.iter()
.map(|structure| structure.evaluate(ctx))
.collect()
}
}
impl private::Sealed for Structure {}
impl Evaluate for Structure {
type Output = Self;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
match self {
Structure::Attribute(attr) => attr.evaluate(ctx).map(Structure::Attribute),
Structure::Block(block) => block.evaluate(ctx).map(Structure::Block),
}
}
}
impl private::Sealed for Attribute {}
impl Evaluate for Attribute {
type Output = Self;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
Ok(Attribute {
key: self.key.clone(),
expr: self.expr.evaluate(ctx).map(Into::into)?,
})
}
}
impl private::Sealed for Block {}
impl Evaluate for Block {
type Output = Self;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
Ok(Block {
identifier: self.identifier.clone(),
labels: self.labels.clone(),
body: self.body.evaluate(ctx)?,
})
}
}
impl private::Sealed for Expression {}
impl Evaluate for Expression {
type Output = Value;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
let ctx = &ctx.child_with_expr(self);
match self {
Expression::Array(array) => array.evaluate(ctx).map(Value::Array),
Expression::Object(object) => object.evaluate(ctx).map(Value::Object),
Expression::TemplateExpr(expr) => expr.evaluate(ctx),
Expression::Variable(ident) => ctx.lookup_var(ident).cloned(),
Expression::Traversal(traversal) => traversal.evaluate(ctx),
Expression::FuncCall(func_call) => func_call.evaluate(ctx),
Expression::Parenthesis(expr) => expr.evaluate(ctx),
Expression::Conditional(cond) => cond.evaluate(ctx),
Expression::Operation(op) => op.evaluate(ctx),
Expression::ForExpr(expr) => expr.evaluate(ctx),
Expression::Raw(_) => Err(ctx.error(ErrorKind::RawExpression)),
other => Ok(Value::from(other.clone())),
}
}
}
impl<T> private::Sealed for Vec<T> where T: Evaluate {}
impl<T> Evaluate for Vec<T>
where
T: Evaluate,
{
type Output = Vec<T::Output>;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
self.iter().map(|expr| expr.evaluate(ctx)).collect()
}
}
impl<K, V> private::Sealed for Object<K, V>
where
K: Evaluate,
V: Evaluate,
{
}
impl<K, V> Evaluate for Object<K, V>
where
K: Evaluate,
K::Output: Hash + Eq,
V: Evaluate,
{
type Output = Map<K::Output, V::Output>;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
self.iter()
.map(|(key, expr)| Ok((key.evaluate(ctx)?, expr.evaluate(ctx)?)))
.collect()
}
}
impl private::Sealed for ObjectKey {}
impl Evaluate for ObjectKey {
type Output = String;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
match self {
ObjectKey::Expression(expr) => expr::evaluate_object_key(expr, ctx),
ident => Ok(ident.to_string()),
}
}
}
impl private::Sealed for TemplateExpr {}
impl Evaluate for TemplateExpr {
type Output = Value;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
let template = Template::from_expr(self)?;
let elements = template.elements();
match elements.get(0) {
Some(Element::Interpolation(interp)) if elements.len() == 1 => {
interp.expr.evaluate(ctx)
}
_ => template.evaluate(ctx).map(Value::String),
}
}
}
impl private::Sealed for Template {}
impl Evaluate for Template {
type Output = String;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
let mut result = String::new();
template::evaluate_template(&mut result, self, ctx, StripMode::None)?;
Ok(result)
}
}
impl private::Sealed for Traversal {}
impl Evaluate for Traversal {
type Output = Value;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
let value = self.expr.evaluate(ctx)?;
let deque = self.operators.iter().collect();
expr::evaluate_traversal(value, deque, ctx)
}
}
impl private::Sealed for FuncCall {}
impl Evaluate for FuncCall {
type Output = Value;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
let name = &self.name;
let func = ctx.lookup_func(name)?;
let len = self.args.len();
let mut args = Vec::with_capacity(len);
for (index, arg) in self.args.iter().enumerate() {
if self.expand_final && index == len - 1 {
args.extend(expr::evaluate_array(arg, ctx)?);
} else {
args.push(arg.evaluate(ctx)?);
}
}
func.call(args)
.map_err(|err| ctx.error(ErrorKind::FuncCall(name.clone(), err)))
}
}
impl private::Sealed for Conditional {}
impl Evaluate for Conditional {
type Output = Value;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
if expr::evaluate_bool(&self.cond_expr, ctx)? {
self.true_expr.evaluate(ctx)
} else {
self.false_expr.evaluate(ctx)
}
}
}
impl private::Sealed for Operation {}
impl Evaluate for Operation {
type Output = Value;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
match self {
Operation::Unary(unary) => unary.evaluate(ctx),
Operation::Binary(binary) => binary.evaluate(ctx),
}
}
}
impl private::Sealed for UnaryOp {}
impl Evaluate for UnaryOp {
type Output = Value;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
use {UnaryOperator::*, Value::*};
let value = self.expr.evaluate(ctx)?;
let value = match (self.operator, value) {
(Not, Bool(v)) => Bool(!v),
(Neg, Number(n)) => Number(-n),
(operator, value) => return Err(ctx.error(ErrorKind::UnaryOp(operator, value))),
};
Ok(value)
}
}
impl private::Sealed for BinaryOp {}
impl Evaluate for BinaryOp {
type Output = Value;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
use {BinaryOperator::*, Value::*};
let op = self.clone().normalize();
let lhs = op.lhs_expr.evaluate(ctx)?;
let rhs = op.rhs_expr.evaluate(ctx)?;
let value = match (lhs, op.operator, rhs) {
(lhs, Eq, rhs) => Bool(lhs == rhs),
(lhs, NotEq, rhs) => Bool(lhs != rhs),
(Bool(lhs), And, Bool(rhs)) => Bool(lhs && rhs),
(Bool(lhs), Or, Bool(rhs)) => Bool(lhs || rhs),
(Number(lhs), LessEq, Number(rhs)) => Bool(lhs <= rhs),
(Number(lhs), GreaterEq, Number(rhs)) => Bool(lhs >= rhs),
(Number(lhs), Less, Number(rhs)) => Bool(lhs < rhs),
(Number(lhs), Greater, Number(rhs)) => Bool(lhs > rhs),
(Number(lhs), Plus, Number(rhs)) => Number(lhs + rhs),
(Number(lhs), Minus, Number(rhs)) => Number(lhs - rhs),
(Number(lhs), Mul, Number(rhs)) => Number(lhs * rhs),
(Number(lhs), Div, Number(rhs)) => Number(lhs / rhs),
(Number(lhs), Mod, Number(rhs)) => Number(lhs % rhs),
(lhs, operator, rhs) => return Err(ctx.error(ErrorKind::BinaryOp(lhs, operator, rhs))),
};
Ok(value)
}
}
impl private::Sealed for ForExpr {}
impl Evaluate for ForExpr {
type Output = Value;
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
let collection = expr::Collection::from_for_expr(self, ctx)?;
match &self.key_expr {
Some(key_expr) => {
let mut result = Map::with_capacity(collection.len());
for ctx in collection {
let ctx = &ctx?;
let key = expr::evaluate_object_key(key_expr, ctx)?;
let value = self.value_expr.evaluate(ctx)?;
if self.grouping {
result
.entry(key)
.or_insert_with(|| Value::Array(Vec::new()))
.as_array_mut()
.unwrap()
.push(value);
} else {
match result.entry(key) {
Entry::Occupied(entry) => {
return Err(ctx.error(ErrorKind::KeyExists(entry.key().clone())))
}
Entry::Vacant(entry) => {
entry.insert(value);
}
}
}
}
Ok(Value::Object(result))
}
None => {
let result = collection
.into_iter()
.map(|ctx| self.value_expr.evaluate(&ctx?))
.collect::<EvalResult<_>>()?;
Ok(Value::Array(result))
}
}
}
}