jsonpath-rust 1.0.4

The library provides the basic functionality to find the set of the data according to the filtering query.
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
use crate::parser::errors::JsonPathError;
use crate::parser::Parsed;
use std::fmt::{Display, Formatter};

/// Represents a JSONPath query with a list of segments.
#[derive(Debug, Clone, PartialEq)]
pub struct JpQuery {
    pub segments: Vec<Segment>,
}

impl JpQuery {
    pub fn new(segments: Vec<Segment>) -> Self {
        JpQuery { segments }
    }
}

impl Display for JpQuery {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "${}",
            self.segments
                .iter()
                .map(|s| s.to_string())
                .collect::<String>()
        )
    }
}
/// Enum representing different types of segments in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum Segment {
    /// Represents a descendant segment.
    Descendant(Box<Segment>),
    /// Represents a selector segment.
    Selector(Selector),
    /// Represents multiple selectors.
    Selectors(Vec<Selector>),
}

impl Segment {
    pub fn name(name: &str) -> Self {
        Segment::Selector(Selector::Name(name.to_string()))
    }
}

impl Display for Segment {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Segment::Descendant(s) => write!(f, "..{}", s),
            Segment::Selector(selector) => write!(f, "{}", selector),
            Segment::Selectors(selectors) => write!(
                f,
                "{}",
                selectors.iter().map(|s| s.to_string()).collect::<String>()
            ),
        }
    }
}
/// Enum representing different types of selectors in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum Selector {
    /// Represents a name selector.
    Name(String),
    /// Represents a wildcard selector.
    Wildcard,
    /// Represents an index selector.
    Index(i64),
    /// Represents a slice selector.
    Slice(Option<i64>, Option<i64>, Option<i64>),
    /// Represents a filter selector.
    Filter(Filter),
}

pub fn slice_from((start, end, step): (Option<i64>, Option<i64>, Option<i64>)) -> Selector {
    Selector::Slice(start, end, step)
}

impl Display for Selector {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Selector::Name(name) => write!(f, "{}", name),
            Selector::Wildcard => write!(f, "*"),
            Selector::Index(index) => write!(f, "{}", index),
            Selector::Slice(start, end, step) => write!(
                f,
                "{}:{}:{}",
                start.unwrap_or(0),
                end.unwrap_or(0),
                step.unwrap_or(1)
            ),
            Selector::Filter(filter) => write!(f, "[?{}]", filter),
        }
    }
}
/// Enum representing different types of filters in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum Filter {
    /// Represents a logical OR filter.
    Or(Vec<Filter>),
    /// Represents a logical AND filter.
    And(Vec<Filter>),
    /// Represents an atomic filter.
    Atom(FilterAtom),
}

impl Display for Filter {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let items_to_str = |items: &Vec<Filter>, sep: &str| {
            items
                .iter()
                .map(|f| f.to_string())
                .collect::<Vec<_>>()
                .join(sep)
        };

        match self {
            Filter::Or(filters) => write!(f, "{}", items_to_str(filters, " || ")),
            Filter::And(filters) => write!(f, "{}", items_to_str(filters, " && ")),
            Filter::Atom(atom) => write!(f, "{}", atom),
        }
    }
}

/// Enum representing different types of atomic filters in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum FilterAtom {
    /// Represents a nested filter with an optional NOT flag.
    Filter { expr: Box<Filter>, not: bool },
    /// Represents a test filter with an optional NOT flag.
    Test { expr: Box<Test>, not: bool },
    /// Represents a comparison filter.
    Comparison(Box<Comparison>),
}

impl FilterAtom {
    pub fn filter(expr: Filter, not: bool) -> Self {
        FilterAtom::Filter {
            expr: Box::new(expr),
            not,
        }
    }

    pub fn test(expr: Test, not: bool) -> Self {
        FilterAtom::Test {
            expr: Box::new(expr),
            not,
        }
    }

    pub fn cmp(cmp: Box<Comparison>) -> Self {
        FilterAtom::Comparison(cmp)
    }
}

impl Display for FilterAtom {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            FilterAtom::Filter { expr, not } => {
                if *not {
                    write!(f, "!{}", expr)
                } else {
                    write!(f, "{}", expr)
                }
            }
            FilterAtom::Test { expr, not } => {
                if *not {
                    write!(f, "!{}", expr)
                } else {
                    write!(f, "{}", expr)
                }
            }
            FilterAtom::Comparison(cmp) => write!(f, "{}", cmp),
        }
    }
}
/// Enum representing different types of comparisons in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum Comparison {
    /// Represents an equality comparison.
    Eq(Comparable, Comparable),
    /// Represents a non-equality comparison.
    Ne(Comparable, Comparable),
    /// Represents a greater-than comparison.
    Gt(Comparable, Comparable),
    /// Represents a greater-than-or-equal-to comparison.
    Gte(Comparable, Comparable),
    /// Represents a less-than comparison.
    Lt(Comparable, Comparable),
    /// Represents a less-than-or-equal-to comparison.
    Lte(Comparable, Comparable),
}

impl Comparison {
    pub fn try_new(op: &str, left: Comparable, right: Comparable) -> Parsed<Self> {
        match op {
            "==" => Ok(Comparison::Eq(left, right)),
            "!=" => Ok(Comparison::Ne(left, right)),
            ">" => Ok(Comparison::Gt(left, right)),
            ">=" => Ok(Comparison::Gte(left, right)),
            "<" => Ok(Comparison::Lt(left, right)),
            "<=" => Ok(Comparison::Lte(left, right)),
            _ => Err(JsonPathError::InvalidJsonPath(format!(
                "Invalid comparison operator: {}",
                op
            ))),
        }
    }

    pub fn vals(&self) -> (&Comparable, &Comparable) {
        match self {
            Comparison::Eq(left, right) => (left, right),
            Comparison::Ne(left, right) => (left, right),
            Comparison::Gt(left, right) => (left, right),
            Comparison::Gte(left, right) => (left, right),
            Comparison::Lt(left, right) => (left, right),
            Comparison::Lte(left, right) => (left, right),
        }
    }
}

impl Display for Comparison {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Comparison::Eq(left, right) => write!(f, "{} == {}", left, right),
            Comparison::Ne(left, right) => write!(f, "{} != {}", left, right),
            Comparison::Gt(left, right) => write!(f, "{} > {}", left, right),
            Comparison::Gte(left, right) => write!(f, "{} >= {}", left, right),
            Comparison::Lt(left, right) => write!(f, "{} < {}", left, right),
            Comparison::Lte(left, right) => write!(f, "{} <= {}", left, right),
        }
    }
}

/// Enum representing different types of comparable values in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum Comparable {
    /// Represents a literal value.
    Literal(Literal),
    /// Represents a function.
    Function(TestFunction),
    /// Represents a singular query.
    SingularQuery(SingularQuery),
}

impl Display for Comparable {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Comparable::Literal(literal) => write!(f, "{}", literal),
            Comparable::Function(func) => write!(f, "{}", func),
            Comparable::SingularQuery(query) => write!(f, "{}", query),
        }
    }
}

/// Enum representing different types of singular queries in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum SingularQuery {
    /// Represents a current node query.
    Current(Vec<SingularQuerySegment>),
    /// Represents a root node query.
    Root(Vec<SingularQuerySegment>),
}

impl Display for SingularQuery {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            SingularQuery::Current(segments) => write!(
                f,
                "@.{}",
                segments.iter().map(|s| s.to_string()).collect::<String>()
            ),
            SingularQuery::Root(segments) => write!(
                f,
                "$.{}",
                segments.iter().map(|s| s.to_string()).collect::<String>()
            ),
        }
    }
}

/// Enum representing different types of singular query segments in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum SingularQuerySegment {
    /// Represents an index segment.
    Index(i64),
    /// Represents a name segment.
    Name(String),
}

impl Display for SingularQuerySegment {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            SingularQuerySegment::Index(index) => write!(f, "{}", index),
            SingularQuerySegment::Name(name) => write!(f, "{}", name),
        }
    }
}

/// Enum representing different types of tests in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum Test {
    /// Represents a relative query.
    RelQuery(Vec<Segment>),
    /// Represents an absolute query.
    AbsQuery(JpQuery),
    /// Represents a function test.
    Function(Box<TestFunction>),
}

impl Test {
    pub fn is_res_bool(&self) -> bool {
        match self {
            Test::RelQuery(_) => false,
            Test::AbsQuery(_) => false,
            Test::Function(func) => match **func {
                TestFunction::Length(_) => false,
                TestFunction::Value(_) => false,
                TestFunction::Count(_) => false,
                TestFunction::Custom(_, _) => true,
                TestFunction::Search(_, _) => true,
                TestFunction::Match(_, _) => true,
            },
        }
    }
}

impl Display for Test {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Test::RelQuery(segments) => write!(
                f,
                "{}",
                segments.iter().map(|s| s.to_string()).collect::<String>()
            ),
            Test::AbsQuery(query) => write!(f, "{}", query),
            Test::Function(func) => write!(f, "{}", func),
        }
    }
}

/// Enum representing different types of test functions in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum TestFunction {
    /// Represents a custom function.
    Custom(String, Vec<FnArg>),
    /// Represents a length function.
    Length(Box<FnArg>),
    /// Represents a value function.
    Value(FnArg),
    /// Represents a count function.
    Count(FnArg),
    /// Represents a search function.
    Search(FnArg, FnArg),
    /// Represents a match function.
    Match(FnArg, FnArg),
}

impl TestFunction {
    pub fn try_new(name: &str, args: Vec<FnArg>) -> Parsed<Self> {
        fn with_node_type_validation<'a>(
            a: &'a FnArg,
            name: &str,
        ) -> Result<&'a FnArg, JsonPathError> {
            if a.is_lit() {
                Err(JsonPathError::InvalidJsonPath(format!(
                    "Invalid argument for the function `{}`: expected a node, got a literal",
                    name
                )))
            } else if a.is_filter() {
                Err(JsonPathError::InvalidJsonPath(format!(
                    "Invalid argument for the function `{}`: expected a node, got a filter",
                    name
                )))
            } else {
                Ok(a)
            }
        }

        match (name, args.as_slice()) {
            ("length", [a]) => Ok(TestFunction::Length(Box::new(a.clone()))),
            ("value", [a]) => Ok(TestFunction::Value(a.clone())),
            ("count", [a]) => Ok(TestFunction::Count(
                with_node_type_validation(a, name)?.clone(),
            )),
            ("search", [a, b]) => Ok(TestFunction::Search(a.clone(), b.clone())),
            ("match", [a, b]) => Ok(TestFunction::Match(a.clone(), b.clone())),
            ("length" | "value" | "count" | "match" | "search", args) => {
                Err(JsonPathError::InvalidJsonPath(format!(
                    "Invalid number of arguments for the function `{}`: got {}",
                    name,
                    args.len()
                )))
            }
            (custom, _) => Ok(TestFunction::Custom(custom.to_string(), args)),
        }
    }

    pub fn is_comparable(&self) -> bool {
        match self {
            TestFunction::Length(_) => true,
            TestFunction::Value(_) => true,
            TestFunction::Count(_) => true,
            TestFunction::Custom(_, _) => false,
            TestFunction::Search(_, _) => false,
            TestFunction::Match(_, _) => false,
        }
    }
}

impl Display for TestFunction {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            TestFunction::Custom(name, args) => write!(
                f,
                "{}({})",
                name,
                args.iter().map(|a| a.to_string()).collect::<String>()
            ),
            TestFunction::Length(arg) => write!(f, "length({})", arg),
            TestFunction::Value(arg) => write!(f, "value({})", arg),
            TestFunction::Count(arg) => write!(f, "count({})", arg),
            TestFunction::Search(arg1, arg2) => write!(f, "search({}, {})", arg1, arg2),
            TestFunction::Match(arg1, arg2) => write!(f, "match({}, {})", arg1, arg2),
        }
    }
}

/// Enum representing different types of function arguments in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum FnArg {
    /// Represents a literal argument.
    Literal(Literal),
    /// Represents a test argument.
    Test(Box<Test>),
    /// Represents a filter argument.
    Filter(Filter),
}

impl FnArg {
    pub fn is_lit(&self) -> bool {
        matches!(self, FnArg::Literal(_))
    }
    pub fn is_filter(&self) -> bool {
        matches!(self, FnArg::Filter(_))
    }
}

impl Display for FnArg {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            FnArg::Literal(literal) => write!(f, "{}", literal),
            FnArg::Test(test) => write!(f, "{}", test),
            FnArg::Filter(filter) => write!(f, "{}", filter),
        }
    }
}

/// Enum representing different types of literal values in a JSONPath query.
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
    /// Represents an integer literal.
    Int(i64),
    /// Represents a floating-point literal.
    Float(f64),
    /// Represents a string literal.
    String(String),
    /// Represents a boolean literal.
    Bool(bool),
    /// Represents a null literal.
    Null,
}

impl Display for Literal {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Literal::Int(val) => write!(f, "{}", val),
            Literal::Float(val) => write!(f, "{}", val),
            Literal::String(val) => write!(f, "\"{}\"", val),
            Literal::Bool(val) => write!(f, "{}", val),
            Literal::Null => write!(f, "null"),
        }
    }
}