@@ -25,16 +25,22 @@ use serde_json::Value;
25
25
/// / \
26
26
/// ^ \___ Union
27
27
/// / \ \
28
- /// / \___ Union \___ [Field ("bar")]
28
+ /// / \___ Union \___ [Name ("bar")]
29
29
/// / \
30
- /// ^ \___ [Number (1), Number (2)]
30
+ /// ^ \___ [Index (1), Index (2)]
31
31
/// / \
32
32
/// Root ___/ \___ DotName("foo")
33
33
/// ```
34
34
///
35
35
/// Selectors are left associative, thus `$.foo[1,2]["bar"]` behaves
36
36
/// like (pseudocode) `(($.foo)[1,2])["bar"]`; thus the root of the resulting
37
37
/// tree is actually the right-most selector (the last one to be applied).
38
+ ///
39
+ /// The Path::Root AST node is called "root" because that's the
40
+ /// name of the node in the JSONPath grammar. It represents the source of
41
+ /// the json value stream which gets operated upon by Selector nodes.
42
+ /// This is why despite being called "root", this node doesn't lie at the root
43
+ /// of the AST tree.
38
44
#[ derive( Debug ) ]
39
45
pub enum Path {
40
46
Root ,
@@ -43,15 +49,15 @@ pub enum Path {
43
49
44
50
#[ derive( Debug ) ]
45
51
pub enum Selector {
46
- Union ( Vec < Index > ) ,
52
+ Union ( Vec < UnionElement > ) ,
47
53
DotName ( String ) ,
48
54
DotWildcard ,
49
55
}
50
56
51
57
#[ derive( Debug ) ]
52
- pub enum Index {
53
- Field ( String ) ,
54
- Number ( i64 ) ,
58
+ pub enum UnionElement {
59
+ Name ( String ) ,
60
+ Index ( i64 ) ,
55
61
}
56
62
57
63
type Iter < ' a > = Box < dyn Iterator < Item = & ' a Value > + ' a > ;
@@ -79,11 +85,11 @@ impl Selector {
79
85
}
80
86
}
81
87
82
- impl Index {
88
+ impl UnionElement {
83
89
pub fn get < ' a > ( & self , v : & ' a Value ) -> Iter < ' a > {
84
90
match self {
85
- Index :: Field ( name) => Box :: new ( v. get ( name) . into_iter ( ) ) ,
86
- Index :: Number ( num) => Box :: new ( v. get ( abs_index ( * num, v) ) . into_iter ( ) ) ,
91
+ UnionElement :: Name ( name) => Box :: new ( v. get ( name) . into_iter ( ) ) ,
92
+ UnionElement :: Index ( num) => Box :: new ( v. get ( abs_index ( * num, v) ) . into_iter ( ) ) ,
87
93
}
88
94
}
89
95
}
@@ -113,9 +119,9 @@ mod test {
113
119
let a2 = Path :: Sel ( Box :: new ( a1) , Selector :: DotName ( "bar" . to_owned ( ) ) ) ;
114
120
let a3 = Path :: Sel (
115
121
Box :: new ( a2) ,
116
- Selector :: Union ( vec ! [ Index :: Field ( "baz" . to_owned( ) ) ] ) ,
122
+ Selector :: Union ( vec ! [ UnionElement :: Name ( "baz" . to_owned( ) ) ] ) ,
117
123
) ;
118
- let a4 = Path :: Sel ( Box :: new ( a3) , Selector :: Union ( vec ! [ Index :: Number ( 4 ) ] ) ) ;
124
+ let a4 = Path :: Sel ( Box :: new ( a3) , Selector :: Union ( vec ! [ UnionElement :: Index ( 4 ) ] ) ) ;
119
125
120
126
let j = json ! ( { "foo" : { "bar" : { "baz" : [ 10 , 20 , 30 , 40 , 50 , 60 ] } } } ) ;
121
127
println ! ( "j: {}" , j) ;
0 commit comments