qcl 0.1.5

A Query Check Language written in Rust
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
use alloc::{
    boxed::Box,
    format,
    string::{String, ToString},
    sync::Arc,
    vec::Vec,
};
use core::fmt::{Debug, Display};

#[cfg(feature = "std")]
use std::{
    hash::{DefaultHasher, Hash, Hasher},
    sync::{
        RwLock,
        atomic::{AtomicU64, Ordering},
    },
};

use crate::{
    ast::Parser,
    error::{Error, Result},
    op::{BinOp, UnaryOp, err_op},
    token::Tokenizer,
    val::Val,
};
use hashbrown::HashMap;
use hashbrown::HashSet as HbHashSet;
#[cfg(feature = "std")]
use once_cell::sync::Lazy;

#[cfg(feature = "std")]
const MAX_PARSE_CACHE_ENTRIES: usize = 4096;
#[cfg(feature = "std")]
const MAX_CACHED_EXPR_LEN: usize = 4096;
#[cfg(feature = "std")]
const PARSE_CACHE_SHARDS: usize = 16;

/// Grammar:
/// exp      ::= ternary
/// ternary  ::= coalesce {'?' expr ':' expr}
/// coalesce ::= or {'??' or}
/// or       ::= and {'||' and}
/// and      ::= cmp {'&&' cmp}
/// cmp      ::= addsub {('<' | '>' | '<=' | '>=' | '!=' | '==') addsub}
/// addsub   ::= muldiv {('+' | '-') muldiv}
/// muldiv   ::= unary {('*' | '/' | '%') unary}
/// unary    ::= {'!' | '-'} postfix
/// postfix  ::= primary {'.' field}
/// primary  ::= nil | false | true | int | float | string | at | list | map
/// at       ::= '@' field {'.' field}
/// field    ::= id | int
/// list     ::= '[' [expr {',' expr}] ']'
/// map      ::= '{' [expr ':' expr {',' expr ':' expr}] '}'
///
///
/// Details:
/// - @expr
///   + All accessible objects of `@` expr are maps actually.
///   + You can use `.` to access the fields of the map.
///   + @req: Request object, `@req.user` is the user object.
///   + @record: Record object, `@record` is the record object.
///   + All valid objects are defined in the [Context]
/// - int / float are considered as `i64 / f64`.
/// - bool can be `true` or `false`.
/// - String
///   + can be wrapped with `""` or `''`.
///   + max length is 64.
/// - nil
///   + ONLY [Option::None] and [Result::Err] are `nil`.
///   + zero value of all types are NOT `nil`.
/// - list literals: `[1, 2, "hello"]`
/// - map literals: `{"key": "value", "count": 42}`
/// - Access literals: `[1, 2, 3].1`, `{"name": "Alice"}.name`
///
/// Examples:
/// - `@req.user.age >= 18`
/// - `@req.user.name == "Alice" && @record.status == "active"`
/// - `[1, 2, 3]`
/// - `{"name": "John", "age": 30}`
/// - `[1, 2, 3].1`
/// - `{"name": "Alice"}.name`
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
    /// expr == expr
    Bin(Box<Expr>, BinOp, Box<Expr>),
    /// !expr
    Unary(UnaryOp, Box<Expr>),
    /// expr && expr
    And(Box<Expr>, Box<Expr>),
    /// expr || expr
    Or(Box<Expr>, Box<Expr>),
    /// expr ?? expr (returns left if non-nil, else right)
    Coalesce(Box<Expr>, Box<Expr>),
    /// expr ? expr : expr (ternary conditional)
    Ternary(Box<Expr>, Box<Expr>, Box<Expr>),
    /// @field.field...
    /// field can be string or int
    At(Vec<Box<Expr>>),
    /// expr.field
    Access(Box<Expr>, Box<Expr>),
    // (expr)
    Paren(Box<Expr>),
    /// [expr, expr, ...]
    List(Vec<Box<Expr>>),
    /// {expr: expr, expr: expr, ...}
    Map(Vec<(Box<Expr>, Box<Expr>)>),
    Val(Val),
}

#[cfg(feature = "std")]
struct ParseCacheShard {
    state: RwLock<ParseCacheState>,
}

#[cfg(feature = "std")]
struct ParseCacheState {
    entries: HashMap<String, (Arc<Expr>, u64)>,
}

#[cfg(feature = "std")]
static CACHE_CLOCK: AtomicU64 = AtomicU64::new(0);

#[cfg(feature = "std")]
impl ParseCacheState {
    fn new() -> Self {
        Self {
            entries: HashMap::new(),
        }
    }

    fn get(&mut self, expression: &str) -> Option<Arc<Expr>> {
        let entry = self.entries.get_mut(expression)?;
        entry.1 = CACHE_CLOCK.fetch_add(1, Ordering::Relaxed);
        Some(entry.0.clone())
    }

    fn insert(&mut self, expression: String, expr: Arc<Expr>) {
        if self.entries.contains_key(expression.as_str()) {
            return;
        }

        if self.entries.len() >= parse_cache_entries_per_shard() {
            self.evict_oldest();
        }

        let ts = CACHE_CLOCK.fetch_add(1, Ordering::Relaxed);
        self.entries.insert(expression, (expr, ts));
    }

    fn evict_oldest(&mut self) {
        if let Some(oldest_key) = self
            .entries
            .iter()
            .min_by_key(|(_, (_, ts))| *ts)
            .map(|(k, _)| k.clone())
        {
            self.entries.remove(&oldest_key);
        }
    }
}

#[cfg(feature = "std")]
static PARSE_CACHE: Lazy<Box<[ParseCacheShard]>> = Lazy::new(|| {
    (0..PARSE_CACHE_SHARDS)
        .map(|_| ParseCacheShard {
            state: RwLock::new(ParseCacheState::new()),
        })
        .collect::<Vec<_>>()
        .into_boxed_slice()
});

#[cfg(feature = "std")]
#[inline]
const fn parse_cache_entries_per_shard() -> usize {
    MAX_PARSE_CACHE_ENTRIES.div_ceil(PARSE_CACHE_SHARDS)
}

#[cfg(feature = "std")]
#[inline]
fn parse_cache_shard_idx(expression: &str) -> usize {
    let mut hasher = DefaultHasher::new();
    expression.hash(&mut hasher);
    (hasher.finish() as usize) % PARSE_CACHE_SHARDS
}

#[cfg(feature = "std")]
#[inline]
fn parse_cache_shard(expression: &str) -> &'static ParseCacheShard {
    &PARSE_CACHE[parse_cache_shard_idx(expression)]
}

impl Expr {
    pub fn eval(&self, ctx: &Val) -> Result<Val> {
        match self {
            Expr::Bin(l, op, r) => op.eval(l, r, ctx),
            Expr::Unary(op, expr) => op.eval(expr, ctx),
            Expr::And(e1, e2) => {
                let l = e1.eval(ctx)?;
                // Short-circuit evaluation to improve performance
                if let Val::Bool(false) = l {
                    return Ok(Val::Bool(false));
                }
                let r = e2.eval(ctx)?;
                match (&l, &r) {
                    (Val::Bool(true), Val::Bool(true)) => Ok(Val::Bool(true)),
                    (Val::Bool(_), Val::Bool(_)) => Ok(Val::Bool(false)),
                    _ => err_op(&l, "&&", &r),
                }
            }
            Expr::Or(e1, e2) => {
                let l = e1.eval(ctx)?;
                // Short-circuit evaluation to improve performance
                if let Val::Bool(true) = l {
                    return Ok(Val::Bool(true));
                }
                let r = e2.eval(ctx)?;
                match (&l, &r) {
                    (Val::Bool(_), Val::Bool(true)) => Ok(Val::Bool(true)),
                    (Val::Bool(_), Val::Bool(_)) => Ok(Val::Bool(false)),
                    _ => err_op(&l, "||", &r),
                }
            }
            Expr::Coalesce(l, r) => {
                let left = l.eval(ctx)?;
                if left == Val::Nil { r.eval(ctx) } else { Ok(left) }
            }
            Expr::Ternary(cond, t, f) => {
                let cond_val = cond.eval(ctx)?;
                match cond_val {
                    Val::Bool(true) => t.eval(ctx),
                    Val::Bool(false) => f.eval(ctx),
                    _ => Err(Error::Eval(format!("Ternary condition must be bool, got: {cond_val}"))),
                }
            }
            Expr::At(paths) => {
                if paths.is_empty() {
                    return Ok(Val::Nil);
                }

                let mut val = ctx;
                for path in paths {
                    let tmp;
                    let key = match &**path {
                        Expr::Val(v) => v,
                        _ => {
                            tmp = path.eval(ctx)?;
                            &tmp
                        }
                    };

                    val = match val.access(key) {
                        Some(v) => v,
                        None => return Ok(Val::Nil),
                    };
                }
                // Return a clone only at the end of evaluation to reduce allocations
                Ok(val.clone())
            }
            Expr::Access(expr, field) => {
                let val = expr.eval(ctx)?;
                let tmp;
                let field_val = match &**field {
                    Expr::Val(v) => v,
                    _ => {
                        tmp = field.eval(ctx)?;
                        &tmp
                    }
                };

                match val.access(field_val) {
                    Some(v) => Ok(v.clone()),
                    None => Ok(Val::Nil),
                }
            }
            Expr::List(exprs) => {
                let mut values = Vec::with_capacity(exprs.len());
                for expr in exprs {
                    values.push(expr.eval(ctx)?);
                }
                Ok(Val::List(Arc::new(values)))
            }
            Expr::Map(pairs) => {
                let mut map = hashbrown::HashMap::with_capacity(pairs.len());
                for (key_expr, value_expr) in pairs {
                    let key_val = key_expr.eval(ctx)?;
                    let value_val = value_expr.eval(ctx)?;

                    let Some(key_str) = primitive_map_key_to_string(&key_val) else {
                        return Err(Error::Eval(format!(
                            "Map key must be a primitive type, got: {:?}",
                            key_val
                        )));
                    };

                    map.insert(key_str, value_val);
                }
                Ok(Val::Map(Arc::new(map)))
            }
            Expr::Paren(expr) => expr.eval(ctx),
            Expr::Val(val) => Ok(val.clone()), // Clone necessary as eval returns owned Val
        }
    }

    /// Get the requested context names from the expression.
    pub fn requested_ctx(&self) -> alloc::collections::BTreeSet<String> {
        let mut names = HbHashSet::new();
        self.collect_ctx_names(&mut names);
        names.into_iter().collect()
    }

    /// Returns true when the expression can be evaluated without consulting `ctx`.
    pub fn is_ctx_independent(&self) -> bool {
        match self {
            Expr::At(_) => false,
            Expr::Bin(l, _, r) | Expr::And(l, r) | Expr::Or(l, r) | Expr::Access(l, r) | Expr::Coalesce(l, r) => {
                l.is_ctx_independent() && r.is_ctx_independent()
            }
            Expr::Unary(_, expr) | Expr::Paren(expr) => expr.is_ctx_independent(),
            Expr::Ternary(cond, t, f) => cond.is_ctx_independent() && t.is_ctx_independent() && f.is_ctx_independent(),
            Expr::List(exprs) => exprs.iter().all(|expr| expr.is_ctx_independent()),
            Expr::Map(pairs) => pairs
                .iter()
                .all(|(key, value)| key.is_ctx_independent() && value.is_ctx_independent()),
            Expr::Val(_) => true,
        }
    }

    /// Helper method to collect context names recursively
    ///
    /// eg.: `@user.props.(@req.service).value && @list` => `["user", "req", "list"]`
    fn collect_ctx_names(&self, names: &mut HbHashSet<String>) {
        match self {
            Expr::At(paths) => {
                if !paths.is_empty() {
                    // The first path element is the context name
                    if let Expr::Val(Val::Str(name)) = &*paths[0] {
                        names.insert(name.as_ref().to_string());
                    } else {
                        // If the first element is a complex expression, process it
                        paths[0].collect_ctx_names(names);
                    }

                    // For other path elements, only process them if they might contain contexts
                    for path in &paths[1..] {
                        match &**path {
                            // Skip [Val]s that are just field names
                            Expr::Val(_) => {}
                            // Process other values normally
                            _ => path.collect_ctx_names(names),
                        }
                    }
                }
            }
            Expr::Access(expr, field) => {
                expr.collect_ctx_names(names);
                field.collect_ctx_names(names);
            }
            Expr::Bin(l, _, r) => {
                l.collect_ctx_names(names);
                r.collect_ctx_names(names);
            }
            Expr::Unary(_, expr) => {
                expr.collect_ctx_names(names);
            }
            Expr::And(l, r) | Expr::Or(l, r) | Expr::Coalesce(l, r) => {
                l.collect_ctx_names(names);
                r.collect_ctx_names(names);
            }
            Expr::Ternary(cond, t, f) => {
                cond.collect_ctx_names(names);
                t.collect_ctx_names(names);
                f.collect_ctx_names(names);
            }
            Expr::List(exprs) => {
                for expr in exprs {
                    expr.collect_ctx_names(names);
                }
            }
            Expr::Map(pairs) => {
                for (key, value) in pairs {
                    key.collect_ctx_names(names);
                    value.collect_ctx_names(names);
                }
            }
            Expr::Paren(expr) => {
                expr.collect_ctx_names(names);
            }
            // Only collect string values when they are actual context names, not field names
            Expr::Val(_) => {}
        }
    }

    /// Cached parsing: parse expression string to Expr with caching to avoid repeated parsing overhead
    #[cfg(feature = "std")]
    pub fn parse_cached(expression: &str) -> Result<Expr> {
        Ok((*Self::parse_cached_arc(expression)?).clone())
    }

    /// Cached parsing variant that avoids cloning the parsed AST on cache hits.
    #[cfg(feature = "std")]
    pub fn parse_cached_arc(expression: &str) -> Result<Arc<Expr>> {
        if expression.len() <= MAX_CACHED_EXPR_LEN {
            let shard = parse_cache_shard(expression);
            if let Some(cached) = shard.state.write().unwrap().get(expression) {
                return Ok(cached);
            }
        }

        let tokens = Tokenizer::new(expression)?;
        let expr = Parser::new(&tokens).parse()?; // Internal constant folding happens in parser
        let expr = Arc::new(expr);

        if expression.len() <= MAX_CACHED_EXPR_LEN {
            let shard = parse_cache_shard(expression);
            let mut state = shard.state.write().unwrap();

            if let Some(cached) = state.get(expression) {
                return Ok(cached);
            }

            state.insert(expression.to_string(), expr.clone());
        }

        Ok(expr)
    }

    /// Constant folding: calculate pure constant sub-expressions as Val constants
    pub(crate) fn fold_constants(self) -> Expr {
        match self {
            Expr::Val(_) => self, // Constant value, return directly
            Expr::Bin(l_box, op, r_box) => {
                // Recursively fold left and right sub-expressions
                let left = (*l_box).fold_constants();
                let right = (*r_box).fold_constants();
                // Try to calculate binary expression as constant
                if let (Expr::Val(lval), Expr::Val(rval)) = (&left, &right) {
                    if op.is_arith() {
                        // Arithmetic operation constant folding
                        let result = match op {
                            BinOp::Add => (lval as &Val) + (rval as &Val),
                            BinOp::Sub => (lval as &Val) - (rval as &Val),
                            BinOp::Mul => (lval as &Val) * (rval as &Val),
                            BinOp::Div => (lval as &Val) / (rval as &Val),
                            BinOp::Mod => (lval as &Val) % (rval as &Val),
                            _ => unreachable!(),
                        };
                        if let Ok(result_val) = result {
                            return Expr::Val(result_val);
                        }
                    } else if op.is_cmp() {
                        // Comparison/contains operation constant folding
                        if let Ok(res_bool) = op.cmp(lval, rval) {
                            return Expr::Val(Val::Bool(res_bool));
                        }
                    }
                    // Other cases (like type mismatch) don't fold, keep expression form
                }
                // Partial folding: left and right nodes already folded, but current node can't fold to constant
                Expr::Bin(Box::new(left), op, Box::new(right))
            }
            Expr::Unary(op, expr_box) => {
                let inner = (*expr_box).fold_constants();
                match (&op, &inner) {
                    (UnaryOp::Not, Expr::Val(Val::Bool(b))) => {
                        return Expr::Val(Val::Bool(!*b));
                    }
                    (UnaryOp::Neg, Expr::Val(Val::Int(i))) => {
                        if let Some(neg) = i.checked_neg() {
                            return Expr::Val(Val::Int(neg));
                        }
                    }
                    (UnaryOp::Neg, Expr::Val(Val::Float(f))) => {
                        return Expr::Val(Val::Float(-*f));
                    }
                    _ => {}
                }
                Expr::Unary(op, Box::new(inner))
            }
            Expr::And(e1_box, e2_box) => {
                let e1 = (*e1_box).fold_constants();
                // Short-circuit constant false: left side constant false, then entire AND is constant false
                if let Expr::Val(Val::Bool(false)) = e1 {
                    return Expr::Val(Val::Bool(false));
                }
                let e2 = (*e2_box).fold_constants();
                // Short-circuit constant true: left side constant true, then return right side expression result
                if let Expr::Val(Val::Bool(true)) = e1 {
                    return e2;
                }
                // Both folded, if both are boolean constants then can further fold
                if let (Expr::Val(Val::Bool(b1)), Expr::Val(Val::Bool(b2))) = (&e1, &e2) {
                    return Expr::Val(Val::Bool(*b1 && *b2));
                }
                Expr::And(Box::new(e1), Box::new(e2))
            }
            Expr::Or(e1_box, e2_box) => {
                let e1 = (*e1_box).fold_constants();
                if let Expr::Val(Val::Bool(true)) = e1 {
                    // Left side constant true, OR expression is constant true
                    return Expr::Val(Val::Bool(true));
                }
                let e2 = (*e2_box).fold_constants();
                if let Expr::Val(Val::Bool(false)) = e1 {
                    // Left side constant false, OR result depends on right side
                    return e2;
                }
                if let (Expr::Val(Val::Bool(b1)), Expr::Val(Val::Bool(b2))) = (&e1, &e2) {
                    return Expr::Val(Val::Bool(*b1 || *b2));
                }
                Expr::Or(Box::new(e1), Box::new(e2))
            }
            Expr::At(paths) => {
                // @path expressions depend on context, don't fold
                let folded_paths = paths.into_iter().map(|p| Box::new(p.fold_constants())).collect();
                Expr::At(folded_paths)
            }
            Expr::Access(base_box, field_box) => {
                let base = (*base_box).fold_constants();
                let field = (*field_box).fold_constants();
                if let (Expr::Val(base_val), Expr::Val(field_val)) = (&base, &field) {
                    // Direct access to constant structure, e.g. [1,2,3].1 or {"k":10}.k
                    if let Some(res_val) = base_val.access(field_val) {
                        return Expr::Val(res_val.clone());
                    } else {
                        return Expr::Val(Val::Nil);
                    }
                }
                Expr::Access(Box::new(base), Box::new(field))
            }
            Expr::List(exprs) => {
                // Fold once and keep the constant payloads as we go to avoid a second pass.
                let mut folded_elems = Vec::with_capacity(exprs.len());
                let mut const_vals = Vec::with_capacity(exprs.len());
                let mut all_const = true;

                for expr in exprs {
                    let folded = expr.fold_constants();
                    if let Expr::Val(v) = &folded {
                        if all_const {
                            const_vals.push(v.clone());
                        }
                    } else {
                        all_const = false;
                    }
                    folded_elems.push(folded);
                }

                if all_const {
                    return Expr::Val(Val::List(Arc::new(const_vals)));
                }
                Expr::List(folded_elems.into_iter().map(Box::new).collect())
            }
            Expr::Map(pairs) => {
                // Fold once and build the constant map incrementally when possible.
                let mut folded_pairs = Vec::with_capacity(pairs.len());
                let mut const_map = HashMap::with_capacity(pairs.len());
                let mut all_const = true;

                for (k, v) in pairs {
                    let key = k.fold_constants();
                    let value = v.fold_constants();

                    if let (Expr::Val(k_val), Expr::Val(v_val)) = (&key, &value) {
                        if all_const {
                            if let Some(key_str) = primitive_map_key_to_string(k_val) {
                                const_map.insert(key_str, v_val.clone());
                            } else {
                                all_const = false;
                            }
                        }
                    } else {
                        all_const = false;
                    }

                    folded_pairs.push((Box::new(key), Box::new(value)));
                }

                if all_const {
                    return Expr::Val(Val::Map(Arc::new(const_map)));
                }

                Expr::Map(folded_pairs)
            }
            Expr::Paren(expr_box) => {
                // Keep parentheses structure, but fold internal expression
                Expr::Paren(Box::new((*expr_box).fold_constants()))
            }
            Expr::Coalesce(l_box, r_box) => {
                let left = (*l_box).fold_constants();
                match &left {
                    Expr::Val(Val::Nil) => return (*r_box).fold_constants(),
                    Expr::Val(_) => return left,
                    _ => {}
                }
                let right = (*r_box).fold_constants();
                Expr::Coalesce(Box::new(left), Box::new(right))
            }
            Expr::Ternary(cond_box, t_box, f_box) => {
                let cond = (*cond_box).fold_constants();
                match &cond {
                    Expr::Val(Val::Bool(true)) => return (*t_box).fold_constants(),
                    Expr::Val(Val::Bool(false)) => return (*f_box).fold_constants(),
                    _ => {}
                }
                let t = (*t_box).fold_constants();
                let f = (*f_box).fold_constants();
                Expr::Ternary(Box::new(cond), Box::new(t), Box::new(f))
            }
        }
    }
}

impl TryInto<Val> for &Expr {
    type Error = crate::error::Error;

    fn try_into(self) -> Result<Val> {
        match self {
            Expr::Val(val) => Ok(val.clone()), // Clone necessary as eval returns owned Val
            _ => {
                let msg = format!("Can't convert Expr::{:?} to Val", self);
                Err(crate::error::Error::Eval(msg))
            }
        }
    }
}

fn primitive_map_key_to_string(value: &Val) -> Option<String> {
    match value {
        Val::Str(s) => Some(s.as_ref().to_string()),
        Val::Int(i) => Some(i.to_string()),
        Val::Float(f) => Some(f.to_string()),
        Val::Bool(b) => Some(b.to_string()),
        _ => None,
    }
}

fn into_expr<S: AsRef<str>>(s: S) -> Result<Expr> {
    let tokens = Tokenizer::new(s.as_ref())?;
    let expr = Parser::new(&tokens).parse()?;
    Ok(expr)
}

impl TryFrom<&str> for Expr {
    type Error = crate::error::Error;

    fn try_from(value: &str) -> core::result::Result<Self, Self::Error> {
        into_expr(value)
    }
}

impl TryFrom<String> for Expr {
    type Error = crate::error::Error;

    fn try_from(value: String) -> core::result::Result<Self, Self::Error> {
        into_expr(value)
    }
}

impl Display for Expr {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Expr::Bin(left, op, right) => write!(f, "{left} {op:?} {right}"),
            Expr::Unary(op, expr) => write!(f, "{op:?}{expr}"),
            Expr::And(left, right) => write!(f, "{left} && {right}"),
            Expr::Or(left, right) => write!(f, "{left} || {right}"),
            Expr::Coalesce(left, right) => write!(f, "{left} ?? {right}"),
            Expr::Ternary(cond, t, fa) => write!(f, "{cond} ? {t} : {fa}"),
            Expr::At(paths) => {
                let paths: Vec<String> = paths.iter().map(|p| p.to_string()).collect();
                write!(f, "@{}", paths.join("."))
            }
            Expr::Access(expr, field) => write!(f, "{}.{}", expr, field),
            Expr::List(exprs) => {
                let exprs: Vec<String> = exprs.iter().map(|e| e.to_string()).collect();
                write!(f, "[{}]", exprs.join(", "))
            }
            Expr::Map(pairs) => {
                let pairs: Vec<String> = pairs.iter().map(|(k, v)| format!("{}: {}", k, v)).collect();
                write!(f, "{{{}}}", pairs.join(", "))
            }
            Expr::Paren(expr) => write!(f, "{expr}"),
            Expr::Val(val) => write!(f, "{}", val),
        }
    }
}

impl From<Val> for Expr {
    fn from(val: Val) -> Self {
        Expr::Val(val)
    }
}

#[cfg(test)]
pub(crate) fn reset_parse_cache_for_tests() {
    for shard in PARSE_CACHE.iter() {
        let mut state = shard.state.write().unwrap();
        state.entries.clear();
    }
}

#[cfg(test)]
pub(crate) fn parse_cache_shard_idx_for_tests(expression: &str) -> usize {
    parse_cache_shard_idx(expression)
}

#[cfg(test)]
pub(crate) fn parse_cache_entry_count_for_tests() -> usize {
    PARSE_CACHE
        .iter()
        .map(|shard| shard.state.read().unwrap().entries.len())
        .sum()
}

#[cfg(test)]
pub(crate) const fn parse_cache_entries_per_shard_for_tests() -> usize {
    parse_cache_entries_per_shard()
}

#[cfg(test)]
pub(crate) const fn parse_cache_shard_count_for_tests() -> usize {
    PARSE_CACHE_SHARDS
}