Skip to main content

jpx_core/
ast.rs

1//! JMESPath Abstract Syntax Tree.
2
3use serde_json::Value;
4
5/// Represents a JMESPath AST node.
6#[derive(Clone, Debug, PartialEq)]
7pub enum Ast {
8    /// Compares two nodes using a comparator (e.g., `foo == bar`).
9    Comparison {
10        offset: usize,
11        comparator: Comparator,
12        lhs: Box<Ast>,
13        rhs: Box<Ast>,
14    },
15    /// Returns the RHS if the predicate yields a truthy value.
16    Condition {
17        offset: usize,
18        predicate: Box<Ast>,
19        then: Box<Ast>,
20    },
21    /// Returns the current node.
22    Identity { offset: usize },
23    /// Expression reference (used by `sort_by`, `max_by`, etc.).
24    Expref { offset: usize, ast: Box<Ast> },
25    /// Flattens nested arrays.
26    Flatten { offset: usize, node: Box<Ast> },
27    /// Calls a function with the given arguments.
28    Function {
29        offset: usize,
30        name: String,
31        args: Vec<Ast>,
32    },
33    /// Extracts a field from an object.
34    Field { offset: usize, name: String },
35    /// Accesses an element by index.
36    Index { offset: usize, idx: i32 },
37    /// A literal JSON value.
38    Literal { offset: usize, value: Value },
39    /// Multi-select list (e.g., `[foo, bar]`).
40    MultiList { offset: usize, elements: Vec<Ast> },
41    /// Multi-select hash (e.g., `{foo: bar, baz: qux}`).
42    MultiHash {
43        offset: usize,
44        elements: Vec<KeyValuePair>,
45    },
46    /// Negation (e.g., `!expr`).
47    Not { offset: usize, node: Box<Ast> },
48    /// Projects the RHS over each element of the LHS array.
49    Projection {
50        offset: usize,
51        lhs: Box<Ast>,
52        rhs: Box<Ast>,
53    },
54    /// Converts an object into an array of its values.
55    ObjectValues { offset: usize, node: Box<Ast> },
56    /// Logical AND.
57    And {
58        offset: usize,
59        lhs: Box<Ast>,
60        rhs: Box<Ast>,
61    },
62    /// Logical OR.
63    Or {
64        offset: usize,
65        lhs: Box<Ast>,
66        rhs: Box<Ast>,
67    },
68    /// Slices an array (e.g., `[0:5:2]`).
69    Slice {
70        offset: usize,
71        start: Option<i32>,
72        stop: Option<i32>,
73        step: i32,
74    },
75    /// Sub-expression (e.g., `foo.bar`).
76    Subexpr {
77        offset: usize,
78        lhs: Box<Ast>,
79        rhs: Box<Ast>,
80    },
81    /// JEP-18: Variable reference (e.g., `$name`).
82    #[cfg(feature = "let-expr")]
83    VariableRef { offset: usize, name: String },
84    /// JEP-18: Let expression (e.g., `let $x = expr in body`).
85    #[cfg(feature = "let-expr")]
86    Let {
87        offset: usize,
88        bindings: Vec<(String, Ast)>,
89        expr: Box<Ast>,
90    },
91}
92
93/// A key-value pair used in multi-select hash expressions.
94#[derive(Clone, Debug, PartialEq)]
95pub struct KeyValuePair {
96    pub key: String,
97    pub value: Ast,
98}
99
100/// Comparison operators used in JMESPath expressions.
101#[derive(Clone, Debug, PartialEq, Eq)]
102pub enum Comparator {
103    Equal,
104    NotEqual,
105    LessThan,
106    LessThanEqual,
107    GreaterThan,
108    GreaterThanEqual,
109}