prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//! Expression evaluator for executing compiled expressions

use super::ast::{Expression, NullHandling, SortDirection, SortKey};
use anyhow::Result;
use regex::Regex;
use serde_json::Value;
use std::cmp::Ordering;

/// String collation options for locale-specific sorting
#[derive(Debug, Clone, PartialEq, Default)]
pub enum Collation {
    /// Default binary/lexicographic comparison
    #[default]
    Default,
    /// Case-insensitive comparison
    CaseInsensitive,
    /// Numeric-aware comparison (e.g., "item2" < "item10")
    Numeric,
    /// Case-insensitive and numeric-aware
    CaseInsensitiveNumeric,
}

/// Compiled filter ready for execution
#[derive(Clone)]
pub struct CompiledFilter {
    expression: Expression,
    evaluator: ExpressionEvaluator,
}

impl CompiledFilter {
    /// Create a new compiled filter
    pub fn new(expression: Expression, evaluator: ExpressionEvaluator) -> Self {
        Self {
            expression,
            evaluator,
        }
    }

    /// Evaluate the filter against an item
    pub fn evaluate(&self, item: &Value) -> Result<bool> {
        self.evaluator.evaluate_bool(&self.expression, item)
    }
}

/// Compiled sort specification ready for execution
pub struct CompiledSort {
    sort_keys: Vec<SortKey>,
    collation: Collation,
}

impl CompiledSort {
    /// Create a new compiled sort
    pub fn new(sort_keys: Vec<SortKey>) -> Self {
        Self {
            sort_keys,
            collation: Collation::Default,
        }
    }

    /// Create a new compiled sort with custom collation
    pub fn with_collation(sort_keys: Vec<SortKey>, collation: Collation) -> Self {
        Self {
            sort_keys,
            collation,
        }
    }

    /// Apply the sort to a vector of items
    #[allow(clippy::ptr_arg)]
    pub fn apply(&self, items: &mut Vec<Value>) -> Result<()> {
        let evaluator = ExpressionEvaluator::new();
        items.sort_by(|a, b| self.compare_items(a, b, &evaluator));
        Ok(())
    }

    /// Compare two items according to the sort keys
    fn compare_items(&self, a: &Value, b: &Value, evaluator: &ExpressionEvaluator) -> Ordering {
        for key in &self.sort_keys {
            let a_value = evaluator.evaluate(&key.expression, a).ok();
            let b_value = evaluator.evaluate(&key.expression, b).ok();

            let ordering =
                self.compare_values(a_value.as_ref(), b_value.as_ref(), &key.null_handling);

            let ordering = match key.direction {
                SortDirection::Ascending => ordering,
                SortDirection::Descending => ordering.reverse(),
            };

            if ordering != Ordering::Equal {
                return ordering;
            }
        }

        Ordering::Equal
    }

    /// Compare two values with null handling
    fn compare_values(
        &self,
        a: Option<&Value>,
        b: Option<&Value>,
        null_handling: &NullHandling,
    ) -> Ordering {
        match (a, b) {
            (None, None) | (Some(Value::Null), Some(Value::Null)) => Ordering::Equal,
            (None, Some(v)) | (Some(Value::Null), Some(v)) if !v.is_null() => match null_handling {
                NullHandling::First => Ordering::Less,
                NullHandling::Last => Ordering::Greater,
            },
            (Some(v), None) | (Some(v), Some(Value::Null)) if !v.is_null() => match null_handling {
                NullHandling::First => Ordering::Greater,
                NullHandling::Last => Ordering::Less,
            },
            (Some(a), Some(b)) => self.compare_json_values(a, b),
            _ => Ordering::Equal,
        }
    }

    /// Compare two JSON values
    fn compare_json_values(&self, a: &Value, b: &Value) -> Ordering {
        match (a, b) {
            (Value::Null, Value::Null) => Ordering::Equal,
            (Value::Bool(a), Value::Bool(b)) => a.cmp(b),
            (Value::Number(a), Value::Number(b)) => {
                let a_f64 = a.as_f64().unwrap_or(0.0);
                let b_f64 = b.as_f64().unwrap_or(0.0);
                a_f64.partial_cmp(&b_f64).unwrap_or(Ordering::Equal)
            }
            (Value::String(a), Value::String(b)) => self.compare_strings(a, b),
            (Value::Array(a), Value::Array(b)) => a.len().cmp(&b.len()),
            (Value::Object(a), Value::Object(b)) => a.len().cmp(&b.len()),
            // Different types - use type ordering
            _ => {
                let type_order = |v: &Value| match v {
                    Value::Null => 0,
                    Value::Bool(_) => 1,
                    Value::Number(_) => 2,
                    Value::String(_) => 3,
                    Value::Array(_) => 4,
                    Value::Object(_) => 5,
                };
                type_order(a).cmp(&type_order(b))
            }
        }
    }

    /// Compare strings according to the configured collation
    fn compare_strings(&self, a: &str, b: &str) -> Ordering {
        match &self.collation {
            Collation::Default => a.cmp(b),
            Collation::CaseInsensitive => a.to_lowercase().cmp(&b.to_lowercase()),
            Collation::Numeric => self.natural_compare(a, b),
            Collation::CaseInsensitiveNumeric => {
                self.natural_compare(&a.to_lowercase(), &b.to_lowercase())
            }
        }
    }

    /// Natural/numeric-aware string comparison
    /// Handles strings like "item2" < "item10" correctly
    fn natural_compare(&self, a: &str, b: &str) -> Ordering {
        let a_parts = self.split_numeric_parts(a);
        let b_parts = self.split_numeric_parts(b);

        for (a_part, b_part) in a_parts.iter().zip(b_parts.iter()) {
            let ord = match (a_part, b_part) {
                (NumericPart::Text(a_text), NumericPart::Text(b_text)) => a_text.cmp(b_text),
                (NumericPart::Number(a_num), NumericPart::Number(b_num)) => a_num.cmp(b_num),
                (NumericPart::Text(_), NumericPart::Number(_)) => Ordering::Less,
                (NumericPart::Number(_), NumericPart::Text(_)) => Ordering::Greater,
            };
            if ord != Ordering::Equal {
                return ord;
            }
        }

        a_parts.len().cmp(&b_parts.len())
    }

    /// Split a string into text and numeric parts for natural comparison
    fn split_numeric_parts(&self, s: &str) -> Vec<NumericPart> {
        let mut parts = Vec::new();
        let mut current_text = String::new();
        let mut current_num = String::new();
        let mut in_number = false;

        for ch in s.chars() {
            if ch.is_ascii_digit() {
                if !in_number {
                    if !current_text.is_empty() {
                        parts.push(NumericPart::Text(current_text.clone()));
                        current_text.clear();
                    }
                    in_number = true;
                }
                current_num.push(ch);
            } else {
                if in_number {
                    if let Ok(num) = current_num.parse::<u64>() {
                        parts.push(NumericPart::Number(num));
                    } else {
                        parts.push(NumericPart::Text(current_num.clone()));
                    }
                    current_num.clear();
                    in_number = false;
                }
                current_text.push(ch);
            }
        }

        // Handle remaining parts
        if in_number && !current_num.is_empty() {
            if let Ok(num) = current_num.parse::<u64>() {
                parts.push(NumericPart::Number(num));
            } else {
                parts.push(NumericPart::Text(current_num));
            }
        } else if !current_text.is_empty() {
            parts.push(NumericPart::Text(current_text));
        }

        if parts.is_empty() {
            parts.push(NumericPart::Text(s.to_string()));
        }

        parts
    }
}

/// Part of a string for natural sorting
#[derive(Debug)]
enum NumericPart {
    Text(String),
    Number(u64),
}

/// Evaluation context for special variables
#[derive(Clone, Debug, Default)]
pub struct EvaluationContext {
    pub index: Option<usize>,
    pub key: Option<String>,
    pub value: Option<Value>,
}

/// Expression evaluator
#[derive(Clone)]
pub struct ExpressionEvaluator {
    context: EvaluationContext,
}

impl ExpressionEvaluator {
    /// Create a new evaluator
    pub fn new() -> Self {
        Self {
            context: EvaluationContext::default(),
        }
    }

    /// Create an evaluator with context
    pub fn with_context(context: EvaluationContext) -> Self {
        Self { context }
    }

    /// Evaluate an expression and return a boolean result
    pub fn evaluate_bool(&self, expr: &Expression, item: &Value) -> Result<bool> {
        match self.evaluate(expr, item)? {
            Value::Bool(b) => Ok(b),
            Value::Null => Ok(false),
            _ => Ok(true), // Non-null values are truthy
        }
    }

    /// Evaluate an expression
    pub fn evaluate(&self, expr: &Expression, item: &Value) -> Result<Value> {
        match expr {
            // Literals - delegated to pure function
            Expression::Number(n) => Ok(Self::evaluate_literal_number(*n)),
            Expression::String(s) => Ok(Value::String(s.clone())),
            Expression::Boolean(b) => Ok(Value::Bool(*b)),
            Expression::Null => Ok(Value::Null),

            // Field access
            Expression::Field(path) => Ok(self.get_field_value(item, path).unwrap_or(Value::Null)),
            Expression::Index(expr, index) => self.evaluate_index_access(expr, index, item),

            // Special variables
            Expression::Variable(name) => self.get_variable_value(name, item),

            // Comparison operators - already extracted as helpers
            Expression::Equal(left, right) => {
                self.evaluate_binary_comparison(left, right, item, |l, r| l == r)
            }
            Expression::NotEqual(left, right) => {
                self.evaluate_binary_comparison(left, right, item, |l, r| l != r)
            }
            Expression::GreaterThan(left, right) => self.evaluate_comparison_gt(left, right, item),
            Expression::LessThan(left, right) => self.evaluate_comparison_lt(left, right, item),
            Expression::GreaterEqual(left, right) => {
                self.evaluate_comparison_gte(left, right, item)
            }
            Expression::LessEqual(left, right) => self.evaluate_comparison_lte(left, right, item),

            // Logical operators - already extracted
            Expression::And(left, right) => self.evaluate_logical_and(left, right, item),
            Expression::Or(left, right) => self.evaluate_logical_or(left, right, item),
            Expression::Not(expr) => self.evaluate_logical_not(expr, item),

            // String functions - already extracted as helpers
            Expression::Contains(str_expr, pattern) => {
                self.evaluate_string_operation(str_expr, pattern, item, |s, p| s.contains(p))
            }
            Expression::StartsWith(str_expr, prefix) => {
                self.evaluate_string_operation(str_expr, prefix, item, |s, p| s.starts_with(p))
            }
            Expression::EndsWith(str_expr, suffix) => {
                self.evaluate_string_operation(str_expr, suffix, item, |s, p| s.ends_with(p))
            }
            Expression::Matches(str_expr, pattern) => {
                self.evaluate_string_operation(str_expr, pattern, item, |s, p| {
                    Regex::new(p).is_ok_and(|re| re.is_match(s))
                })
            }

            // Type checking - already extracted as helpers
            Expression::IsNull(expr) => self.evaluate_type_check(expr, item, |v| v.is_null()),
            Expression::IsNotNull(expr) => self.evaluate_type_check(expr, item, |v| !v.is_null()),
            Expression::IsNumber(expr) => {
                self.evaluate_type_check(expr, item, |v| matches!(v, Value::Number(_)))
            }
            Expression::IsString(expr) => {
                self.evaluate_type_check(expr, item, |v| matches!(v, Value::String(_)))
            }
            Expression::IsBool(expr) => {
                self.evaluate_type_check(expr, item, |v| matches!(v, Value::Bool(_)))
            }
            Expression::IsArray(expr) => {
                self.evaluate_type_check(expr, item, |v| matches!(v, Value::Array(_)))
            }
            Expression::IsObject(expr) => {
                self.evaluate_type_check(expr, item, |v| matches!(v, Value::Object(_)))
            }

            // Aggregate functions - extracted to separate methods
            Expression::Length(expr) => self.evaluate_length(expr, item),
            Expression::Sum(expr) => self.evaluate_sum(expr, item),
            Expression::Count(expr) => self.evaluate_count(expr, item),
            Expression::Min(expr) => self.evaluate_min(expr, item),
            Expression::Max(expr) => self.evaluate_max(expr, item),
            Expression::Avg(expr) => self.evaluate_avg(expr, item),

            // Array operations - extracted to separate methods
            Expression::In(expr, values) => self.evaluate_in_operation(expr, values, item),
            Expression::ArrayWildcard(base_expr, path) => {
                self.evaluate_array_wildcard(base_expr, path, item)
            }
        }
    }

    /// Pure function: Evaluate a literal number
    fn evaluate_literal_number(n: f64) -> Value {
        Value::Number(
            serde_json::Number::from_f64(n).unwrap_or_else(|| serde_json::Number::from(0)),
        )
    }

    /// Evaluate index access operation
    fn evaluate_index_access(
        &self,
        expr: &Expression,
        index: &Expression,
        item: &Value,
    ) -> Result<Value> {
        let base = self.evaluate(expr, item)?;
        let idx = self.evaluate(index, item)?;
        Ok(self.index_value(&base, &idx).unwrap_or(Value::Null))
    }

    /// Evaluate greater-than comparison
    fn evaluate_comparison_gt(
        &self,
        left: &Expression,
        right: &Expression,
        item: &Value,
    ) -> Result<Value> {
        self.evaluate_binary_comparison(left, right, item, |l, r| {
            self.compare_values(l, r) == Ordering::Greater
        })
    }

    /// Evaluate less-than comparison
    fn evaluate_comparison_lt(
        &self,
        left: &Expression,
        right: &Expression,
        item: &Value,
    ) -> Result<Value> {
        self.evaluate_binary_comparison(left, right, item, |l, r| {
            self.compare_values(l, r) == Ordering::Less
        })
    }

    /// Evaluate greater-than-or-equal comparison
    fn evaluate_comparison_gte(
        &self,
        left: &Expression,
        right: &Expression,
        item: &Value,
    ) -> Result<Value> {
        self.evaluate_binary_comparison(left, right, item, |l, r| {
            let ord = self.compare_values(l, r);
            ord == Ordering::Greater || ord == Ordering::Equal
        })
    }

    /// Evaluate less-than-or-equal comparison
    fn evaluate_comparison_lte(
        &self,
        left: &Expression,
        right: &Expression,
        item: &Value,
    ) -> Result<Value> {
        self.evaluate_binary_comparison(left, right, item, |l, r| {
            let ord = self.compare_values(l, r);
            ord == Ordering::Less || ord == Ordering::Equal
        })
    }

    /// Evaluate logical AND with short-circuit evaluation
    fn evaluate_logical_and(
        &self,
        left: &Expression,
        right: &Expression,
        item: &Value,
    ) -> Result<Value> {
        let l = self.evaluate_bool(left, item)?;
        if !l {
            return Ok(Value::Bool(false)); // Short-circuit
        }
        let r = self.evaluate_bool(right, item)?;
        Ok(Value::Bool(r))
    }

    /// Evaluate logical OR with short-circuit evaluation
    fn evaluate_logical_or(
        &self,
        left: &Expression,
        right: &Expression,
        item: &Value,
    ) -> Result<Value> {
        let l = self.evaluate_bool(left, item)?;
        if l {
            return Ok(Value::Bool(true)); // Short-circuit
        }
        let r = self.evaluate_bool(right, item)?;
        Ok(Value::Bool(r))
    }

    /// Evaluate logical NOT
    fn evaluate_logical_not(&self, expr: &Expression, item: &Value) -> Result<Value> {
        let v = self.evaluate_bool(expr, item)?;
        Ok(Value::Bool(!v))
    }

    /// Evaluate length aggregate function
    fn evaluate_length(&self, expr: &Expression, item: &Value) -> Result<Value> {
        let v = self.evaluate(expr, item)?;
        let len = Self::compute_length(&v);
        Ok(Value::Number(serde_json::Number::from(len as u64)))
    }

    /// Pure function: Compute length of a value
    fn compute_length(v: &Value) -> usize {
        match v {
            Value::String(s) => s.len(),
            Value::Array(arr) => arr.len(),
            Value::Object(obj) => obj.len(),
            _ => 0,
        }
    }

    /// Evaluate sum aggregate function
    fn evaluate_sum(&self, expr: &Expression, item: &Value) -> Result<Value> {
        let v = self.evaluate(expr, item)?;
        match v {
            Value::Array(arr) => {
                let sum = Self::sum_numeric_array(&arr);
                Ok(Self::to_number_value(sum))
            }
            _ => Ok(Value::Number(serde_json::Number::from(0))),
        }
    }

    /// Pure function: Sum numeric values in an array
    fn sum_numeric_array(arr: &[Value]) -> f64 {
        arr.iter()
            .filter_map(|v| v.as_f64())
            .filter(|f| !f.is_nan())
            .sum()
    }

    /// Evaluate count aggregate function
    fn evaluate_count(&self, expr: &Expression, item: &Value) -> Result<Value> {
        let v = self.evaluate(expr, item)?;
        match v {
            Value::Array(arr) => Ok(Value::Number(serde_json::Number::from(arr.len() as u64))),
            _ => Ok(Value::Number(serde_json::Number::from(0))),
        }
    }

    /// Evaluate min aggregate function
    fn evaluate_min(&self, expr: &Expression, item: &Value) -> Result<Value> {
        let v = self.evaluate(expr, item)?;
        match v {
            Value::Array(arr) => Ok(Self::find_min_in_array(&arr)),
            _ => Ok(Value::Null),
        }
    }

    /// Pure function: Find minimum value in numeric array
    fn find_min_in_array(arr: &[Value]) -> Value {
        let min = arr
            .iter()
            .filter_map(|v| v.as_f64())
            .filter(|f| !f.is_nan())
            .min_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));

        min.map(Self::to_number_value).unwrap_or(Value::Null)
    }

    /// Evaluate max aggregate function
    fn evaluate_max(&self, expr: &Expression, item: &Value) -> Result<Value> {
        let v = self.evaluate(expr, item)?;
        match v {
            Value::Array(arr) => Ok(Self::find_max_in_array(&arr)),
            _ => Ok(Value::Null),
        }
    }

    /// Pure function: Find maximum value in numeric array
    fn find_max_in_array(arr: &[Value]) -> Value {
        let max = arr
            .iter()
            .filter_map(|v| v.as_f64())
            .filter(|f| !f.is_nan())
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));

        max.map(Self::to_number_value).unwrap_or(Value::Null)
    }

    /// Evaluate average aggregate function
    fn evaluate_avg(&self, expr: &Expression, item: &Value) -> Result<Value> {
        let v = self.evaluate(expr, item)?;
        match v {
            Value::Array(arr) => Ok(Self::compute_average(&arr)),
            _ => Ok(Value::Null),
        }
    }

    /// Pure function: Compute average of numeric array
    fn compute_average(arr: &[Value]) -> Value {
        let values: Vec<f64> = arr
            .iter()
            .filter_map(|v| v.as_f64())
            .filter(|f| !f.is_nan())
            .collect();

        if values.is_empty() {
            Value::Null
        } else {
            let avg = values.iter().sum::<f64>() / values.len() as f64;
            Self::to_number_value(avg)
        }
    }

    /// Pure function: Convert f64 to JSON Number Value
    fn to_number_value(n: f64) -> Value {
        Value::Number(
            serde_json::Number::from_f64(n).unwrap_or_else(|| serde_json::Number::from(0)),
        )
    }

    /// Evaluate 'in' operation
    fn evaluate_in_operation(
        &self,
        expr: &Expression,
        values: &[Value],
        item: &Value,
    ) -> Result<Value> {
        let v = self.evaluate(expr, item)?;
        Ok(Value::Bool(values.contains(&v)))
    }

    /// Evaluate array wildcard access
    fn evaluate_array_wildcard(
        &self,
        base_expr: &Expression,
        path: &[String],
        item: &Value,
    ) -> Result<Value> {
        let base = self.evaluate(base_expr, item)?;
        match base {
            Value::Array(arr) => Ok(Value::Array(self.collect_array_wildcard_values(&arr, path))),
            _ => Ok(Value::Null),
        }
    }

    /// Collect values from array wildcard access
    fn collect_array_wildcard_values(&self, arr: &[Value], path: &[String]) -> Vec<Value> {
        arr.iter()
            .filter_map(|item| {
                if path.is_empty() {
                    Some(item.clone())
                } else {
                    self.get_field_value(item, path)
                }
            })
            .collect()
    }

    /// Get a field value from an object using a path
    fn get_field_value(&self, item: &Value, path: &[String]) -> Option<Value> {
        let mut current = item.clone();

        for segment in path {
            // Handle array access notation in field names
            if segment.contains('[') && segment.contains(']') {
                let parts: Vec<&str> = segment.split('[').collect();
                let field = parts[0];
                let index_str = parts[1].trim_end_matches(']');

                // Get the field first
                current = current.get(field)?.clone();

                // Then apply the index
                if let Ok(index) = index_str.parse::<usize>() {
                    if let Value::Array(arr) = current {
                        current = arr.get(index)?.clone();
                    } else {
                        return None;
                    }
                }
            } else {
                current = current.get(segment)?.clone();
            }
        }

        Some(current)
    }

    /// Index into a value
    fn index_value(&self, base: &Value, index: &Value) -> Option<Value> {
        match (base, index) {
            (Value::Array(arr), Value::Number(n)) => {
                let idx = n.as_u64()? as usize;
                arr.get(idx).cloned()
            }
            (Value::Object(obj), Value::String(key)) => obj.get(key).cloned(),
            _ => None,
        }
    }

    /// Get a special variable value
    fn get_variable_value(&self, name: &str, _item: &Value) -> Result<Value> {
        match name {
            "_index" => match self.context.index {
                Some(idx) => Ok(Value::Number(serde_json::Number::from(idx as u64))),
                None => Ok(Value::Null),
            },
            "_key" => match &self.context.key {
                Some(key) => Ok(Value::String(key.clone())),
                None => Ok(Value::Null),
            },
            "_value" => match &self.context.value {
                Some(val) => Ok(val.clone()),
                None => Ok(Value::Null),
            },
            _ => Ok(Value::Null),
        }
    }

    /// Compare two values for ordering
    fn compare_values(&self, a: &Value, b: &Value) -> Ordering {
        match (a, b) {
            (Value::Number(a), Value::Number(b)) => {
                let a_f64 = a.as_f64().unwrap_or(0.0);
                let b_f64 = b.as_f64().unwrap_or(0.0);
                a_f64.partial_cmp(&b_f64).unwrap_or(Ordering::Equal)
            }
            (Value::String(a), Value::String(b)) => a.cmp(b),
            (Value::Bool(a), Value::Bool(b)) => a.cmp(b),
            _ => Ordering::Equal,
        }
    }

    /// Helper: Evaluate a binary comparison operation
    /// Takes left and right expressions and a comparison function
    fn evaluate_binary_comparison<F>(
        &self,
        left: &Expression,
        right: &Expression,
        item: &Value,
        comparator: F,
    ) -> Result<Value>
    where
        F: FnOnce(&Value, &Value) -> bool,
    {
        let left_val = self.evaluate(left, item)?;
        let right_val = self.evaluate(right, item)?;
        Ok(Value::Bool(comparator(&left_val, &right_val)))
    }

    /// Helper: Evaluate a string operation
    /// Takes string expression, pattern expression, and a string comparison function
    fn evaluate_string_operation<F>(
        &self,
        str_expr: &Expression,
        pattern_expr: &Expression,
        item: &Value,
        operation: F,
    ) -> Result<Value>
    where
        F: FnOnce(&str, &str) -> bool,
    {
        let str_val = self.evaluate(str_expr, item)?;
        let pattern_val = self.evaluate(pattern_expr, item)?;

        let result = match (str_val, pattern_val) {
            (Value::String(s), Value::String(p)) => operation(&s, &p),
            _ => false,
        };

        Ok(Value::Bool(result))
    }

    /// Helper: Evaluate a type checking operation
    /// Takes an expression and a type checking predicate
    fn evaluate_type_check<F>(
        &self,
        expr: &Expression,
        item: &Value,
        type_predicate: F,
    ) -> Result<Value>
    where
        F: FnOnce(&Value) -> bool,
    {
        let value = self.evaluate(expr, item)?;
        Ok(Value::Bool(type_predicate(&value)))
    }
}

impl Default for ExpressionEvaluator {
    fn default() -> Self {
        Self::new()
    }
}