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
#![allow(unused_macros)]
//! Basic JSONPath generation and manipulation.  Note that this is *not* supposed to be a complete
//! implementation of RFC 8259...there is just enough JSONPath goodness within this module to
//! support what the SAX and DOM parsers do.
use std::borrow::Cow;
use std::collections::VecDeque;
use std::fmt::Display;
use std::ops::Add;

/// The default separator to use within generated paths
const PATH_SEPARATOR: char = '.';

/// An enumeration fo various different path components
#[derive(Debug, Clone, PartialEq)]
pub enum JsonPathComponent<'a> {
    /// The root element '$'
    Root,
    /// A name selector component
    NameSelector(Cow<'a, str>),
    /// Wildcard selector
    WildcardSelector,
    /// Index selector
    IndexSelector(usize),
    /// Range selector
    RangeSelector(usize, usize),
}

impl<'a> Display for JsonPathComponent<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Root => write!(f, "$"),
            Self::NameSelector(s) => write!(f, "['{}']", s),
            Self::WildcardSelector => write!(f, "[*]"),
            Self::IndexSelector(i) => write!(f, "[{}]", i),
            Self::RangeSelector(i, j) => write!(f, "[{}..{}]", i, j),
        }
    }
}

/// Macro to check whether a given [JsonPathComponent] is an index selector
macro_rules! is_index_selector {
    ($comp : expr) => {
        match $comp {
            JsonPathComponent::IndexSelector(_) => true,
            _ => false,
        }
    };
}

/// Macro to check whether a given [JsonPathComponent] is a root selector
macro_rules! is_root_selector {
    ($comp : expr) => {
        match $comp {
            JsonPathComponent::Root => true,
            _ => false,
        }
    };
}

/// Macro to check whether a given [JsonPathComponent] is a wildcard root selector
macro_rules! is_wildcard_selector {
    ($comp : expr) => {
        match $comp {
            JsonPathComponent::WildcardSelector => true,
            _ => false,
        }
    };
}

/// macro to check whether a given [JsonPathComponent] is a name selector
macro_rules! is_name_selector {
    ($comp : expr) => {
        match $comp {
            JsonPathComponent::NameSelector(_) => true,
            _ => false,
        }
    };
}

/// macro to check whether a given [JsonPathComponent] is a range selector
macro_rules! is_range_selector {
    ($comp : expr) => {
        match $comp {
            JsonPathComponent::RangeSelector(_, _) => true,
            _ => false,
        }
    };
}

/// Struct for creating and manipulating Json paths vaguely compatible with a subset of RFC 8259.
/// Each instance of [JsonPath] comprises of multiple [JsonPathComponent]s
#[derive(Debug)]
pub struct JsonPath<'a> {
    /// The path components
    components: Vec<JsonPathComponent<'a>>,
}

impl<'a> JsonPath<'a> {
    /// Create a new, partial (no root) [JsonPath] instance
    pub fn new_partial() -> Self {
        JsonPath { components: vec![] }
    }

    /// Create a new [JsonPath] instance with just a root component
    pub fn new() -> Self {
        JsonPath {
            components: vec![JsonPathComponent::Root],
        }
    }

    /// Is this an empty path?
    pub fn is_empty(&self) -> bool {
        self.components.is_empty()
    }

    /// A partial path doesn't have a [JsonPathComponent::Root] in the first position
    pub fn is_partial(&self) -> bool {
        if !self.is_empty() {
            self.components[0] != JsonPathComponent::Root
        } else {
            true
        }
    }

    /// Render a string ([Cow]) representation of the path
    pub fn as_string(&self) -> Cow<'a, str> {
        Cow::Owned(
            self.components
                .iter()
                .map(|s| s.to_string())
                .collect::<Vec<String>>()
                .join("."),
        )
    }

    /// Push a new [JsonPathComponent::NameSelector] based on a string slice.  Note that no
    /// validation is carried out (as of yet) to check whether the name is actually valid and not
    /// full of crap
    pub fn push_str_selector(&mut self, name: &str) {
        self.components
            .push(JsonPathComponent::NameSelector(Cow::Owned(String::from(
                name.replace("\"", ""),
            ))));
    }

    /// Push a new [JsonPathComponent::IndexSelector] based on a given index
    pub fn push_index_select(&mut self, index: usize) {
        self.components
            .push(JsonPathComponent::IndexSelector(index));
    }

    /// Push a new [JsonPathComponent::RangeSelector] based on a given start and end index
    pub fn push_range_selector(&mut self, start: usize, end: usize) {
        self.components
            .push(JsonPathComponent::RangeSelector(start, end));
    }

    /// Push a new [JsonPathComponent::WildcardSelector]
    pub fn push_wildcard_selector(&mut self) {
        self.components.push(JsonPathComponent::WildcardSelector);
    }

    /// Appends a new [JsonPathComponent] to the end of the path
    pub fn push(&mut self, component: JsonPathComponent<'a>) {
        self.components.push(component);
    }

    /// Pops the last [JsonPathComponent] from the end of the path (if it exists)
    pub fn pop(&mut self) -> Option<JsonPathComponent<'a>> {
        self.components.pop()
    }

    /// Checks whether a path points to an array element within the source JSON
    pub fn is_array_path(&self) -> bool {
        if self.is_empty() {
            return false;
        }
        is_index_selector!(self.components.last().unwrap())
    }

    /// Returns the number of [JsonPathComponent]s within the path
    pub fn len(&self) -> usize {
        self.components.len()
    }

    /// Determines whether a given [JsonPath] matches another given [JsonPath] strictly,
    /// in that wildcards do not match against ranges within the path, and each path must
    /// be identical to each other in the sense that all components match exactly
    pub fn matches_strict(&self, rhs: &JsonPath<'a>) -> bool {
        if self.is_empty() && rhs.is_empty() {
            return true;
        }
        if self.len() != rhs.len() {
            return false;
        };
        self.components
            .iter()
            .zip(rhs.components.iter())
            .fold(true, |acc, comps| acc && comps.0 == comps.1)
    }

    /// Determines whether a path matches a given path according to the following rules:
    /// - A wildcard selector should match against an index selector in the same position
    /// - Name selectors only match based on strict equality
    /// - Indexed selectors match a range selector iff the index lies within the range supplied
    /// - Range selectors only match strictly with other range selectors (hmmmm)
    pub fn matches(&self, rhs: &JsonPath<'a>) -> bool {
        if self.is_empty() && rhs.is_empty() {
            return true;
        }
        if self.len() != rhs.len() {
            return false;
        };
        self.components
            .iter()
            .zip(rhs.components.iter())
            .fold(true, |acc, comp| acc && Self::match_path_components(comp))
    }

    /// Pattern match on pairs of [JsonPathComponent]s, and perform "special" equality checks for
    /// things like [JsonPathComponent::IndexSelector] and [JsonPathComponent::RangeSelector]. We
    /// need to make sure that we take care of commutativity in the match arms
    fn match_path_components(pair: (&JsonPathComponent, &JsonPathComponent)) -> bool {
        match pair {
            (JsonPathComponent::IndexSelector(i), JsonPathComponent::IndexSelector(j)) => i == j,
            (JsonPathComponent::IndexSelector(i), JsonPathComponent::RangeSelector(j, k)) => {
                j <= i && i <= k
            }
            (JsonPathComponent::RangeSelector(j, k), JsonPathComponent::IndexSelector(i)) => {
                j <= i && i <= k
            }
            (JsonPathComponent::WildcardSelector, JsonPathComponent::IndexSelector(_)) => true,
            (JsonPathComponent::IndexSelector(_), JsonPathComponent::WildcardSelector) => true,
            (a, b) => a == b,
        }
    }
}

impl<'a> Display for JsonPath<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_string())
    }
}

impl<'a> Add<&JsonPath<'a>> for JsonPath<'a> {
    type Output = Self;

    /// Concatenate two [JsonPath] instances.  Does some basic checking to ensure that you don't
    /// try and concatenate two rooted paths, or concatenate a rooted path to a partial path
    fn add(mut self, rhs: &JsonPath<'a>) -> Self {
        if !self.is_partial() && !rhs.is_partial() {
            panic!("attempted to concatenate two rooted paths")
        }
        if self.is_partial() && !rhs.is_partial() {
            panic!("attempted to concatenate a rooted path to a partial path")
        }
        rhs.components
            .iter()
            .for_each(|c| self.components.push(c.clone()));
        self
    }
}

#[cfg(test)]
mod tests {

    use crate::paths::{JsonPath, JsonPathComponent};

    #[test]
    fn a_new_partial_path_should_be_partial() {
        let path = JsonPath::new_partial();
        assert!(path.is_partial())
    }

    #[test]
    fn a_new_partial_path_should_have_an_empty_representation() {
        let path = JsonPath::new_partial();
        assert_eq!("".to_string(), path.as_string())
    }

    #[test]
    fn a_new_partial_path_should_be_empty() {
        let path = JsonPath::new_partial();
        assert!(path.is_empty())
    }

    #[test]
    fn a_new_rooted_path_should_not_be_partial() {
        let path = JsonPath::new();
        assert!(!path.is_partial())
    }

    #[test]
    fn a_new_rooted_path_should_have_the_correct_representation() {
        let path = JsonPath::new();
        assert_eq!("$".to_string(), path.as_string())
    }

    #[test]
    fn a_new_rooted_path_should_not_be_empty() {
        let path = JsonPath::new();
        assert!(!path.is_empty())
    }

    #[test]
    fn simple_paths_should_have_correct_representations() {
        let mut path = JsonPath::new();
        path.push_str_selector("a");
        path.push_str_selector("b");
        path.push_str_selector("c");
        path.push_str_selector("d");
        assert_eq!(&path.as_string(), "$.['a'].['b'].['c'].['d']")
    }

    #[test]
    fn complex_paths_should_have_correct_representations() {
        let mut path = JsonPath::new();
        path.push_str_selector("array");
        path.push_wildcard_selector();
        path.push_index_select(4);
        path.push_range_selector(6, 7);
        assert_eq!(path.as_string(), "$.['array'].[*].[4].[6..7]")
    }

    #[test]
    fn popping_elements_should_correctly_alter_representation() {
        let mut path = JsonPath::new();
        path.push_str_selector("a");
        path.push_str_selector("b");
        path.push_str_selector("c");
        path.push_str_selector("d");
        path.pop();
        path.pop();
        assert_eq!(&path.as_string(), "$.['a'].['b']")
    }

    #[test]
    fn array_paths_should_be_identified_as_such() {
        let mut path = JsonPath::new();
        path.push_str_selector("a");
        path.push_index_select(4);
        assert!(path.is_array_path())
    }

    #[test]
    fn root_and_partial_paths_can_be_concatenated_correctly() {
        let mut root = JsonPath::new();
        let mut partial = JsonPath::new_partial();
        partial.push_str_selector("a");
        root = root + &partial;
        assert_eq!(root.as_string(), "$.['a']")
    }

    #[test]
    #[should_panic]
    fn concatenating_two_rooted_paths_should_panic() {
        let root1 = JsonPath::new();
        let root2 = JsonPath::new();
        let _combined = root1 + &root2;
    }

    #[test]
    #[should_panic]
    fn concatenating_a_root_path_to_a_partial_should_panic() {
        let partial = JsonPath::new_partial();
        let root = JsonPath::new();
        let _combined = partial + &root;
    }

    #[test]
    fn empty_paths_should_strictly_match() {
        let left = JsonPath::new();
        let right = JsonPath::new();
        assert!(left.matches_strict(&right))
    }

    #[test]
    fn complex_paths_should_strictly_match() {
        let mut left = JsonPath::new();
        let mut right = JsonPath::new();
        left.push_str_selector("a");
        right.push_str_selector("a");
        left.push_index_select(6);
        right.push_index_select(6);
        left.push_range_selector(4, 5);
        right.push_range_selector(4, 5);
        assert!(left.matches_strict(&right));
        assert_eq!(left.to_string(), right.to_string())
    }

    #[test]
    fn slightly_different_complex_paths_should_not_strictly_match() {
        let mut left = JsonPath::new();
        let mut right = JsonPath::new();
        left.push_str_selector("a");
        right.push_str_selector("a");
        left.push_index_select(6);
        right.push_index_select(6);
        left.push_range_selector(4, 5);
        right.push_range_selector(3, 5);
        assert!(!left.matches_strict(&right));
        assert_ne!(left.to_string(), right.to_string())
    }

    #[test]
    fn partial_paths_should_match_strictly() {
        let mut left = JsonPath::new_partial();
        let mut right = JsonPath::new_partial();
        left.push_str_selector("a");
        right.push_str_selector("a");
        left.push_wildcard_selector();
        right.push_wildcard_selector();
        assert!(left.matches(&right));
        assert_eq!(left.to_string(), right.to_string())
    }

    #[test]
    fn indexes_should_match_on_ranges() {
        // x == y
        let mut left = JsonPath::new();
        let mut right = JsonPath::new();
        left.push_range_selector(0, 4);
        right.push_index_select(3);
        assert!(left.matches(&right));

        // y == x
        let mut left = JsonPath::new();
        let mut right = JsonPath::new();
        left.push_range_selector(0, 4);
        right.push_index_select(3);
        assert!(right.matches(&left))
    }
    #[test]
    fn indexes_should_not_match_outside_of_ranges() {
        // x == y
        let mut left = JsonPath::new();
        let mut right = JsonPath::new();
        left.push_range_selector(0, 4);
        right.push_index_select(6);
        assert!(!left.matches(&right));

        // y == x
        let mut left = JsonPath::new();
        let mut right = JsonPath::new();
        left.push_range_selector(0, 4);
        right.push_index_select(6);
        assert!(!right.matches(&left))
    }
}