Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit 2ec6147

Browse files
author
Marko Mikulicic
committed
Fix nomenclature
1 parent 644ddc9 commit 2ec6147

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/ast.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,22 @@ use serde_json::Value;
2525
/// / \
2626
/// ^ \___ Union
2727
/// / \ \
28-
/// / \___ Union \___ [Field("bar")]
28+
/// / \___ Union \___ [Name("bar")]
2929
/// / \
30-
/// ^ \___ [Number(1), Number(2)]
30+
/// ^ \___ [Index(1), Index(2)]
3131
/// / \
3232
/// Root ___/ \___ DotName("foo")
3333
/// ```
3434
///
3535
/// Selectors are left associative, thus `$.foo[1,2]["bar"]` behaves
3636
/// like (pseudocode) `(($.foo)[1,2])["bar"]`; thus the root of the resulting
3737
/// 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.
3844
#[derive(Debug)]
3945
pub enum Path {
4046
Root,
@@ -43,15 +49,15 @@ pub enum Path {
4349

4450
#[derive(Debug)]
4551
pub enum Selector {
46-
Union(Vec<Index>),
52+
Union(Vec<UnionElement>),
4753
DotName(String),
4854
DotWildcard,
4955
}
5056

5157
#[derive(Debug)]
52-
pub enum Index {
53-
Field(String),
54-
Number(i64),
58+
pub enum UnionElement {
59+
Name(String),
60+
Index(i64),
5561
}
5662

5763
type Iter<'a> = Box<dyn Iterator<Item = &'a Value> + 'a>;
@@ -79,11 +85,11 @@ impl Selector {
7985
}
8086
}
8187

82-
impl Index {
88+
impl UnionElement {
8389
pub fn get<'a>(&self, v: &'a Value) -> Iter<'a> {
8490
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()),
8793
}
8894
}
8995
}
@@ -113,9 +119,9 @@ mod test {
113119
let a2 = Path::Sel(Box::new(a1), Selector::DotName("bar".to_owned()));
114120
let a3 = Path::Sel(
115121
Box::new(a2),
116-
Selector::Union(vec![Index::Field("baz".to_owned())]),
122+
Selector::Union(vec![UnionElement::Name("baz".to_owned())]),
117123
);
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)]));
119125

120126
let j = json!({"foo":{"bar":{"baz":[10,20,30,40,50,60]}}});
121127
println!("j: {}", j);

src/grammar.pest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
selector = _{ SOI ~ rootSelector ~ jsonPath ~ EOI }
1+
selector = _{ SOI ~ rootSelector ~ matchers ~ EOI }
22

3-
jsonPath = ${ matcher* }
3+
matchers = ${ matcher* }
44
rootSelector = { "$" }
55

66
matcher = { dotChild | union }

src/parser.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn parse_child_name(matcher_rule: pest::iterators::Pair<Rule>) -> String {
4545
}
4646
}
4747

48-
fn parse_union_indices(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Index> {
48+
fn parse_union_indices(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<UnionElement> {
4949
let mut res = Vec::new();
5050

5151
for r in matcher_rule.into_inner() {
@@ -58,20 +58,20 @@ fn parse_union_indices(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Index>
5858
res
5959
}
6060

61-
fn parse_union_child(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Index> {
61+
fn parse_union_child(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<UnionElement> {
6262
matcher_rule
6363
.into_inner()
6464
.map(|r| match r.as_rule() {
65-
Rule::doubleInner => Index::Field(unescape(r.as_str())),
66-
Rule::singleInner => Index::Field(unescape_single(r.as_str())),
65+
Rule::doubleInner => UnionElement::Name(unescape(r.as_str())),
66+
Rule::singleInner => UnionElement::Name(unescape_single(r.as_str())),
6767
_ => panic!("invalid parse tree {:?}", r),
6868
})
6969
.collect()
7070
}
7171

72-
fn parse_union_array_index(matcher_rule: pest::iterators::Pair<Rule>) -> Index {
72+
fn parse_union_array_index(matcher_rule: pest::iterators::Pair<Rule>) -> UnionElement {
7373
let i = matcher_rule.as_str().parse().unwrap();
74-
Index::Number(i)
74+
UnionElement::Index(i)
7575
}
7676

7777
fn unescape(contents: &str) -> String {

0 commit comments

Comments
 (0)