jexpr 0.0.1

A general-purpose, extensible evaluator for JSON-based prefix expressions, inspired by Mapbox GL expressions.
Documentation
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
use rustc_hash::FxHashMap;
use serde_json::Number;
use std::{collections::HashMap, f64, hash::BuildHasher, sync::Arc};

pub use serde_json::Value;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("filter expression is not a valid JSON: {0}")]
    ParseError(String),

    #[error("Evaluation error: {0}")]
    EvaluationError(String),

    #[error("Invalid number")]
    InvalidNumber,

    #[error("Function evaluation error: {0}")]
    FunctionEvaluationError(Box<dyn std::error::Error + Send + Sync>),
}

impl From<Box<dyn std::error::Error + Send + Sync>> for Error {
    fn from(value: Box<dyn std::error::Error + Send + Sync>) -> Self {
        Self::FunctionEvaluationError(value)
    }
}

pub type Function<'a, C = FxHashMap<String, String>> =
    Box<dyn Fn(&C, Value) -> Result<Value, Error> + Send + Sync + 'a>;

pub trait Tags {
    fn get(&self, key: &str) -> Option<String>;
    fn contains_key(&self, key: &str) -> bool;
}

impl<S: BuildHasher> Tags for HashMap<String, String, S> {
    fn get(&self, key: &str) -> Option<String> {
        self.get(key).cloned()
    }

    fn contains_key(&self, key: &str) -> bool {
        self.contains_key(key)
    }
}

pub type Expression<'a, C> = Box<dyn Fn(&C) -> Result<Value, Error> + Send + Sync + 'a>;

pub type ParseResult<'a, C> = Result<Expression<'a, C>, Error>;

/// # Errors
///
/// Parse `MapLibre` Expression (incomplete implementation)
/// and return function that evaluates the expression with the provided context
pub fn parse<'a, C: Tags + 'a>(expression: &str) -> ParseResult<'a, C> {
    compile(
        Arc::new(HashMap::new()),
        serde_json::from_str(expression).map_err(|e| Error::ParseError(e.to_string()))?,
    )
}

/// # Errors
///
/// Parse `MapLibre` Expression (incomplete implementation)
/// and return function that evaluates the expression with the provided context
pub fn parse_with_functions<'a, C: Tags + 'a, S: BuildHasher + Sync + Send + Default + 'a>(
    expression: &str,
    functions: HashMap<String, Function<'a, C>, S>,
) -> ParseResult<'a, C> {
    compile(
        Arc::new(functions),
        serde_json::from_str(expression).map_err(|e| Error::ParseError(e.to_string()))?,
    )
}

#[allow(clippy::needless_pass_by_value)]
#[allow(clippy::too_many_lines)] // TODO
fn compile<'a, C: Tags + 'a, S: BuildHasher + Sync + Send + 'a>(
    functions: Arc<HashMap<String, Function<'a, C>, S>>,
    value: Value,
) -> ParseResult<'a, C> {
    let Value::Array(array) = value else {
        // literals

        return Ok(Box::new(move |_ctx| Ok(value.clone())));
    };

    let mut iter = array.into_iter();

    let operator = iter.next();

    let Some(Value::String(str)) = operator else {
        return Err(Error::EvaluationError(
            "operator must be a string literal".into(),
        ));
    };

    let mut get_next_arg = || {
        let next = iter
            .next()
            .ok_or_else(|| Error::EvaluationError("missing argument".into()))?;

        compile(Arc::clone(&functions), next)
    };

    match str.as_str() {
        "!" => {
            let arg = get_next_arg()?;

            Ok(Box::new(move |ctx| match arg(ctx)? {
                Value::Bool(bool) => Ok(Value::Bool(!bool)),
                _ => Err(Error::EvaluationError("not a bool".into())),
            }))
        }

        "to-string" => {
            let arg = get_next_arg()?;

            Ok(Box::new(move |ctx| {
                Ok(Value::String(match arg(ctx)? {
                    Value::Bool(bool) => (if bool { "true" } else { "false" }).to_owned(),
                    Value::Null => String::new(),
                    Value::String(s) => s,
                    Value::Number(n) => n.to_string(),
                    _ => Err(Error::EvaluationError("can't convert to string".into()))?,
                }))
            }))
        }

        "to-boolean" => {
            let arg = get_next_arg()?;

            Ok(Box::new(move |ctx| {
                Ok(Value::Bool(match arg(ctx)? {
                    Value::Bool(bool) => bool,
                    Value::String(s) => !s.is_empty(),
                    Value::Number(number) => {
                        let number = number
                            .as_f64()
                            .ok_or(Error::EvaluationError("error converting to f64".into()))?;
                        !number.is_nan() && number != 0.
                    }
                    _ => true,
                }))
            }))
        }

        "to-number" => {
            let arg = get_next_arg()?;

            Ok(Box::new(move |ctx| {
                Ok(Value::Number(match arg(ctx)? {
                    Value::Null => Number::from_f64(0.).unwrap(),
                    Value::Number(n) => n,
                    Value::Bool(bool) => Number::from_f64(if bool { 1. } else { 0. }).unwrap(),
                    Value::String(s) => {
                        Number::from_f64(s.parse::<f64>().map_err(|_| Error::InvalidNumber)?)
                            .ok_or_else(|| Error::EvaluationError("unsupported number".into()))?
                    }
                    _ => Err(Error::EvaluationError("can't convert to number".into()))?,
                }))
            }))
        }

        "length" => {
            let arg = get_next_arg()?;

            Ok(Box::new(move |ctx| {
                Ok(Value::Number(match arg(ctx)? {
                    Value::Array(a) => Number::from(a.len()),
                    Value::String(s) => Number::from(s.len()),
                    _ => Err(Error::EvaluationError(
                        "`length` works only on arrays or strings".into(),
                    ))?,
                }))
            }))
        }

        "==" => {
            let (arg_1, arg_2) = (get_next_arg()?, get_next_arg()?);

            Ok(Box::new(move |ctx| {
                Ok(Value::Bool(arg_1(ctx)? == arg_2(ctx)?))
            }))
        }

        "!=" => {
            let (arg_1, arg_2) = (get_next_arg()?, get_next_arg()?);

            Ok(Box::new(move |ctx| {
                Ok(Value::Bool(arg_1(ctx)? != arg_2(ctx)?))
            }))
        }

        ">" | "<" | ">=" | "<=" => {
            let (arg_1, arg_2) = (get_next_arg()?, get_next_arg()?);

            Ok(Box::new(move |ctx| {
                let v1 = arg_1(ctx)?;
                let v2 = arg_2(ctx)?;

                Ok(Value::Bool(match (v1, v2) {
                    (Value::String(s1), Value::String(s2)) => match str.as_str() {
                        ">" => s1 > s2,
                        "<" => s1 < s2,
                        ">=" => s1 >= s2,
                        "<=" => s1 <= s2,
                        _ => unreachable!(),
                    },
                    (Value::Number(n1), Value::Number(n2)) => {
                        let n1 = n1
                            .as_f64()
                            .ok_or(Error::EvaluationError("error converting from f64".into()))?;
                        let n2 = n2
                            .as_f64()
                            .ok_or(Error::EvaluationError("error converting from f64".into()))?;

                        match str.as_str() {
                            ">" => n1 > n2,
                            "<" => n1 < n2,
                            ">=" => n1 >= n2,
                            "<=" => n1 <= n2,
                            _ => unreachable!(),
                        }
                    }
                    _ => Err(Error::EvaluationError("exprected number or string".into()))?,
                }))
            }))
        }

        "abs" | "acos" | "asin" | "atan" | "ceil" | "cos" | "floor" | "ln" | "log10" | "log2"
        | "round" | "sin" | "sqrt" | "tan" => {
            let arg = get_next_arg()?;

            Ok(Box::new(move |ctx| {
                let value = arg(ctx)?;

                match value {
                    Value::Number(number) => {
                        let number = number
                            .as_f64()
                            .ok_or(Error::EvaluationError("error converting from f64".into()))?;

                        let value = match str.as_str() {
                            "abs" => number.abs(),
                            "acos" => number.acos(),
                            "asin" => number.asin(),
                            "atan" => number.atan(),
                            "ceil" => number.ceil(),
                            "cos" => number.cos(),
                            "floor" => number.floor(),
                            "ln" => number.ln(),
                            "log10" => number.log10(),
                            "log2" => number.log2(),
                            "round" => number.round(),
                            "sin" => number.sin(),
                            "sqrt" => number.sqrt(),
                            "tan" => number.tan(),
                            _ => unreachable!(),
                        };

                        Ok(Value::Number(Number::from_f64(value).ok_or(
                            Error::EvaluationError("error converting to f64".into()),
                        )?))
                    }
                    _ => Err(Error::EvaluationError("expected number".into()))?,
                }
            }))
        }

        "e" | "pi" | "ln2" | "ln10" => Ok(Box::new(move |_| {
            let value = match str.as_str() {
                "e" => f64::consts::E,
                "pi" => f64::consts::PI,
                "ln2" => f64::consts::LN_2,
                "ln10" => f64::consts::LN_10,
                _ => unreachable!(),
            };

            Ok(Value::Number(Number::from_f64(value).ok_or(
                Error::EvaluationError("error converting to f64".into()),
            )?))
        })),

        "+" | "*" | "min" | "max" => {
            let mut args = Vec::new();

            for arg in iter {
                args.push(compile(Arc::clone(&functions), arg)?);
            }

            Ok(Box::new(move |ctx| {
                let values = args
                    .iter()
                    .map(|arg| {
                        arg(ctx).and_then(|value| match value {
                            Value::Number(num) => num
                                .as_f64()
                                .ok_or(Error::EvaluationError("error converting to f64".into())),
                            _ => Err(Error::EvaluationError("expected number".into())),
                        })
                    })
                    .collect::<Result<Vec<_>, _>>()?;

                let result = match str.as_str() {
                    "+" => values.into_iter().fold(0.0, |acc, v| acc + v),
                    "*" => values.into_iter().fold(1.0, |acc, v| acc * v),
                    "min" => values.into_iter().fold(f64::INFINITY, f64::min),
                    "max" => values.into_iter().fold(f64::NEG_INFINITY, f64::max),
                    _ => unreachable!(),
                };

                Ok(Value::Number(Number::from_f64(result).ok_or(
                    Error::EvaluationError("error converting to f64".into()),
                )?))
            }))
        }

        "-" | "/" | "%" | "random" => {
            let (arg_1, arg_2) = (get_next_arg()?, get_next_arg()?);

            Ok(Box::new(move |ctx| {
                let v1 = arg_1(ctx)?;
                let v2 = arg_2(ctx)?;

                Ok(match (v1, v2) {
                    (Value::Number(n1), Value::Number(n2)) => {
                        let n1 = n1
                            .as_f64()
                            .ok_or(Error::EvaluationError("error converting from f64".into()))?;

                        let n2 = n2
                            .as_f64()
                            .ok_or(Error::EvaluationError("error converting from f64".into()))?;

                        // match str.as_str()
                        Value::Number(
                            Number::from_f64(match str.as_str() {
                                "-" => n1 - n2,
                                "/" => n1 / n2,
                                "%" => n1 % n2,
                                "random" => rand::random_range(n1..n2), // TODO seed is not supported yet
                                _ => unreachable!(),
                            })
                            .ok_or(Error::EvaluationError("error converting to f64".into()))?,
                        )
                    }
                    _ => return Err(Error::EvaluationError("expected number".into())),
                })
            }))
        }

        "upcase" | "downcase" => {
            let arg = get_next_arg()?;

            Ok(Box::new(move |ctx| {
                let value = arg(ctx)?;

                match value {
                    Value::String(string) => Ok(Value::String(match str.as_str() {
                        "upcase" => string.to_uppercase(),
                        "downcase" => string.to_lowercase(),
                        _ => unreachable!(),
                    })),
                    _ => Err(Error::EvaluationError("expected string".into())),
                }
            }))
        }

        "get" => {
            let arg1 = get_next_arg()?;

            let arg2 = iter
                .next()
                .map(|arg2| compile(Arc::clone(&functions), arg2))
                .transpose()?;

            Ok(Box::new(move |ctx| match arg1(ctx)? {
                Value::String(key) => {
                    if let Some(arg2) = &arg2 {
                        match arg2(ctx)? {
                            Value::Object(arg2) => {
                                Ok(arg2.get(&key).map_or(Value::Null, ToOwned::to_owned))
                            }
                            _ => Err(Error::EvaluationError(
                                "`has` second argument must be an object".into(),
                            )),
                        }
                    } else {
                        Ok(ctx.get(&key).map_or(Value::Null, Value::String))
                    }
                }
                _ => Err(Error::EvaluationError(
                    "`get` argument must be a string".into(),
                ))?,
            }))
        }

        "has" => {
            let arg1 = get_next_arg()?;

            let arg2 = iter
                .next()
                .map(|arg2| compile(Arc::clone(&functions), arg2))
                .transpose()?;

            Ok(Box::new(move |ctx| match arg1(ctx)? {
                Value::String(key) => {
                    if let Some(arg2) = &arg2 {
                        match arg2(ctx)? {
                            Value::Object(arg2) => Ok(Value::Bool(arg2.contains_key(&key))),
                            _ => Err(Error::EvaluationError(
                                "`has` second argument must be an object".into(),
                            )),
                        }
                    } else {
                        Ok(Value::Bool(ctx.contains_key(&key)))
                    }
                }
                _ => Err(Error::EvaluationError(
                    "`has` first argument must be a string".into(),
                ))?,
            }))
        }

        "at" => {
            let arg1 = get_next_arg()?;

            let arg2 = get_next_arg()?;

            Ok(Box::new(move |ctx| match arg1(ctx)? {
                Value::Number(index) => {
                    let index = index
                        .as_u64()
                        .ok_or_else(|| Error::EvaluationError("can't convert to u64".into()))?;

                    let index = usize::try_from(index).unwrap();

                    match arg2(ctx)? {
                        Value::Array(arg2) => {
                            Ok(arg2.get(index).map_or(Value::Null, ToOwned::to_owned))
                        }
                        _ => Err(Error::EvaluationError(
                            "`has` second argument must be an array".into(),
                        )),
                    }
                }
                _ => Err(Error::EvaluationError(
                    "`at` first argument must be a number".into(),
                ))?,
            }))
        }

        "typeof" => {
            let arg = get_next_arg()?;

            Ok(Box::new(move |ctx| {
                Ok(Value::String(
                    (match arg(ctx)? {
                        Value::Null => "null",
                        Value::Bool(_) => "boolean",
                        Value::Number(_) => "number",
                        Value::String(_) => "string",
                        Value::Array(_) => "array",
                        Value::Object(_) => "object",
                    })
                    .to_string(),
                ))
            }))
        }

        "any" => {
            let mut args = Vec::new();

            for arg in iter {
                args.push(compile(Arc::clone(&functions), arg)?);
            }

            Ok(Box::new(move |ctx| {
                for arg in &args {
                    let value = arg(ctx)?;

                    match value {
                        Value::Bool(bool) => {
                            if bool {
                                return Ok(value);
                            }
                        }
                        _ => return Err(Error::EvaluationError("not a bool".into())),
                    }
                }

                Ok(Value::Bool(false))
            }))
        }

        "all" => {
            let mut args = Vec::new();

            for arg in iter {
                args.push(compile(Arc::clone(&functions), arg)?);
            }

            Ok(Box::new(move |ctx| {
                for arg in &args {
                    match arg(ctx)? {
                        Value::Bool(bool) => {
                            if !bool {
                                return Ok(Value::Bool(false));
                            }
                        }
                        _ => return Err(Error::EvaluationError("not a bool".into())),
                    }
                }

                Ok(Value::Bool(true))
            }))
        }

        "coalesce" => {
            let mut args = Vec::new();

            for arg in iter {
                args.push(compile(Arc::clone(&functions), arg)?);
            }

            Ok(Box::new(move |ctx| {
                for arg in &args {
                    let value = arg(ctx)?;

                    if !value.is_null() {
                        return Ok(value);
                    }
                }

                Ok(Value::Null)
            }))
        }

        "case" => {
            let mut args = Vec::new();

            for arg in iter {
                args.push(compile(Arc::clone(&functions), arg)?);
            }

            Ok(Box::new(move |ctx| {
                let mut iter = args.iter();

                loop {
                    let cond = match iter.next() {
                        None => {
                            return Err(Error::EvaluationError("missing default value".into()));
                        }
                        Some(arg) => arg(ctx)?,
                    };

                    let value = match iter.next() {
                        None => return Ok(cond),
                        Some(arg) => arg(ctx)?,
                    };

                    if let Value::Bool(bool) = cond {
                        if bool {
                            return Ok(value);
                        }
                    } else {
                        return Err(Error::EvaluationError(
                            "case condition is not a bool".into(),
                        ));
                    }
                }
            }))
        }

        "concat" => {
            let mut args = Vec::new();

            for arg in iter {
                args.push(compile(Arc::clone(&functions), arg)?);
            }

            Ok(Box::new(move |ctx| {
                args.iter()
                    .map(|arg| {
                        arg(ctx).and_then(|value| match value {
                            Value::String(string) => Ok(string),
                            _ => Err(Error::EvaluationError("expected string".into())),
                        })
                    })
                    .collect::<Result<Vec<_>, _>>()
                    .map(|strings| Value::String(strings.concat()))
            }))
        }

        // TODO add more operators
        fn_name => {
            if !functions.contains_key(fn_name) {
                return Err(Error::EvaluationError(format!(
                    "no such operator or function: {str}"
                )));
            };

            let mut args = Vec::new();

            for arg in iter {
                args.push(compile(Arc::clone(&functions), arg)?);
            }

            let functions = Arc::clone(&functions);

            let fn_name = fn_name.to_string();

            Ok(Box::new(move |ctx| {
                let function = functions.get(&fn_name).unwrap();

                let mut values = Vec::new();

                for arg in &args {
                    let value = arg(ctx)?;

                    values.push(value);
                }

                function(ctx, Value::Array(values))
                    .map_err(|e| Error::FunctionEvaluationError(e.into()))
            }))
        }
    }
}

#[cfg(test)]
mod tests {
    use rustc_hash::FxHashMap;

    use super::*;

    #[test]
    fn test() -> Result<(), Error> {
        assert_eq!(parse("2")?(&FxHashMap::default())?, 2);

        assert_eq!(parse(r#"["+", 2, 2]"#)?(&FxHashMap::default())?, 4.);

        assert_eq!(parse(r#"["+", "2", "2"]"#)?(&FxHashMap::default())?, "22");

        assert_eq!(parse(r#"[">", 3, -2]"#)?(&FxHashMap::default())?, true);

        assert_eq!(
            parse(r#"["all", true, false]"#)?(&FxHashMap::default())?,
            false
        );

        assert_eq!(
            parse(r#"["all", true, true]"#)?(&FxHashMap::default())?,
            true
        );

        assert_eq!(
            parse(r#"["any", true, false]"#)?(&FxHashMap::default())?,
            true
        );

        assert_eq!(
            parse(r#"["any", false, false]"#)?(&FxHashMap::default())?,
            false
        );

        assert_eq!(
            parse(r#"["coalesce", null, "foo", 42]"#)?(&FxHashMap::default())?,
            "foo"
        );

        assert_eq!(
            parse(r#"["case", false, "foo", true, "bar", "baz"]"#)?(&FxHashMap::default())?,
            "bar"
        );

        let mut ctx = FxHashMap::default();

        ctx.insert(String::from("name"), String::from("martin"));

        assert_eq!(parse(r#"["==", ["get", "name"], "martin"]"#)?(&ctx)?, true);

        Ok(())
    }
}