Skip to content

Commit 41ba954

Browse files
author
meir
committed
small api changes
1 parent f4da1a0 commit 41ba954

File tree

3 files changed

+82
-57
lines changed

3 files changed

+82
-57
lines changed

src/grammer.pest

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
complex_literal = _{(!("."|"*"|"\""|"'"|"["|"]"|"="|" "|"("|")"|"{"|"}"|"|"|"<"|">"|"!") ~ ANY)+}
1+
complex_literal = _{(!("."|"*"|"\""|"'"|"["|"]"|"="|" "|"("|")"|"{"|"}"|"|"|"<"|">"|"!"|",") ~ ANY)+}
22
literal = @{('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | "-" | "_")+ | complex_literal}
33

44
string = _{("'" ~ (string_value) ~ "'") | ("\"" ~ (string_value) ~ "\"") | ("\"" ~ (string_value_escape_1) ~ "\"") | ("'" ~ (string_value_escape_2) ~ "'")}
@@ -64,6 +64,10 @@ first_element = _{literal | all}
6464

6565
root = {(element|first_element) ~ (element)*}
6666

67+
simple_root = {"." ~ literal}
68+
6769
query = _{SOI ~ "$" ~ root? ~ EOI}
6870

71+
simple_query = _{SOI ~ "$" ~ simple_root ~ EOI}
72+
6973
WHITESPACE = _{ " " }

src/json_path.rs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct JsonPathParser;
1414
#[derive(Debug)]
1515
pub struct Query<'i>{
1616
// query: QueryElement<'i>
17-
query: Pairs<'i, Rule>,
17+
pub query: Pairs<'i, Rule>,
1818
}
1919

2020
#[derive(Debug)]
@@ -107,6 +107,43 @@ impl UserPathTrackerGenerator for DummyTrackerGenerator {
107107
}
108108
}
109109

110+
#[derive(Debug, PartialEq)]
111+
pub enum PTrackerElement {
112+
Key(String),
113+
Index(usize),
114+
}
115+
116+
#[derive(Debug, PartialEq)]
117+
pub struct PTracker {
118+
pub elemenets: Vec<PTrackerElement>,
119+
}
120+
impl UserPathTracker for PTracker {
121+
fn add_str(&mut self, s: &str){
122+
self.elemenets.push(PTrackerElement::Key(s.to_string()))
123+
}
124+
125+
fn add_index(&mut self, i: usize){
126+
self.elemenets.push(PTrackerElement::Index(i))
127+
}
128+
129+
fn to_string_path(self) -> Vec<String> {
130+
self.elemenets.into_iter().map(|e|{
131+
match e {
132+
PTrackerElement::Key(s) => s,
133+
PTrackerElement::Index(i) => i.to_string(),
134+
}
135+
}).collect()
136+
}
137+
}
138+
139+
pub struct PTrackerGenerator;
140+
impl UserPathTrackerGenerator for PTrackerGenerator {
141+
type PT = PTracker;
142+
fn generate(&self) -> Self::PT {
143+
PTracker{elemenets: Vec::new()}
144+
}
145+
}
146+
110147
#[derive(Clone)]
111148
enum PathTrackerElement<'i> {
112149
Index(usize),
@@ -279,8 +316,8 @@ impl<'i, 'j, S:SelectValue> TermEvaluationResult<'i, 'j, S> {
279316

280317
#[derive(Debug)]
281318
pub struct PathCalculator<'i, UPTG: UserPathTrackerGenerator>{
282-
query: Option<&'i Query<'i>>,
283-
tracker_generator: Option<UPTG>,
319+
pub query: Option<&'i Query<'i>>,
320+
pub tracker_generator: Option<UPTG>,
284321
}
285322

286323
#[derive(Debug, PartialEq)]
@@ -785,9 +822,8 @@ impl<'i, UPTG: UserPathTrackerGenerator> PathCalculator<'i, UPTG> {
785822
}
786823
}
787824

788-
pub fn calc_with_paths<'j:'i, S:SelectValue>(&self, json: &'j S) -> Vec<CalculationResult<'j, S, UPTG::PT>>
825+
pub fn calc_with_paths_on_root<'j:'i, S:SelectValue>(&self, json: &'j S, root: Pair<Rule>) -> Vec<CalculationResult<'j, S, UPTG::PT>>
789826
{
790-
let root = self.query.unwrap().query.clone().next().unwrap();
791827
let mut calc_data = PathCalculatorData{
792828
results: Vec::new(),
793829
root: json,
@@ -799,18 +835,10 @@ impl<'i, UPTG: UserPathTrackerGenerator> PathCalculator<'i, UPTG> {
799835
}
800836
calc_data.results.drain(..).collect()
801837
}
802-
803-
pub fn calc_once<'j, 'p, S:SelectValue>(mut q: Query<'j>, json: &'p S) -> Vec<&'p S> {
804-
let root = q.query.next().unwrap();
805-
let mut calc_data = PathCalculatorData{
806-
results: Vec::new(),
807-
root: json,
808-
};
809-
PathCalculator::<'p, DummyTrackerGenerator> {
810-
query: None,
811-
tracker_generator: None,
812-
}.calc_internal(root.into_inner(), json, None, &mut calc_data, true);
813-
calc_data.results.into_iter().map(|e: CalculationResult<'p, S, DummyTracker>| e.res).collect()
838+
839+
pub fn calc_with_paths<'j:'i, S:SelectValue>(&self, json: &'j S) -> Vec<CalculationResult<'j, S, UPTG::PT>>
840+
{
841+
self.calc_with_paths_on_root(json, self.query.unwrap().query.clone().next().unwrap())
814842
}
815843

816844
pub fn calc<'j:'i, S:SelectValue>(&self, json: &'j S) -> Vec<&'j S>

src/lib.rs

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ pub mod select_value;
77
pub mod json_path;
88
pub mod json_node;
99

10+
use crate::select_value::{SelectValue};
1011
use json_path::{
11-
UserPathTracker,
12-
UserPathTrackerGenerator,
12+
CalculationResult,
13+
PTrackerGenerator,
1314
PathCalculator,
1415
Query,
1516
QueryCompilationError,
1617
DummyTrackerGenerator,
18+
DummyTracker,
19+
PTracker,
20+
UserPathTracker,
1721
};
1822

1923
/// Create a PathCalculator object. The path calculator can be re-used
@@ -22,43 +26,6 @@ pub fn create<'i>(query: &'i Query<'i>) -> PathCalculator<'i, DummyTrackerGenera
2226
PathCalculator::create(query)
2327
}
2428

25-
#[derive(Debug, PartialEq)]
26-
pub enum PTrackerElement {
27-
Key(String),
28-
Index(usize),
29-
}
30-
31-
#[derive(Debug, PartialEq)]
32-
pub struct PTracker {
33-
pub elemenets: Vec<PTrackerElement>,
34-
}
35-
impl UserPathTracker for PTracker {
36-
fn add_str(&mut self, s: &str){
37-
self.elemenets.push(PTrackerElement::Key(s.to_string()))
38-
}
39-
40-
fn add_index(&mut self, i: usize){
41-
self.elemenets.push(PTrackerElement::Index(i))
42-
}
43-
44-
fn to_string_path(self) -> Vec<String> {
45-
self.elemenets.into_iter().map(|e|{
46-
match e {
47-
PTrackerElement::Key(s) => s,
48-
PTrackerElement::Index(i) => i.to_string(),
49-
}
50-
}).collect()
51-
}
52-
}
53-
54-
pub struct PTrackerGenerator;
55-
impl UserPathTrackerGenerator for PTrackerGenerator {
56-
type PT = PTracker;
57-
fn generate(&self) -> Self::PT {
58-
PTracker{elemenets: Vec::new()}
59-
}
60-
}
61-
6229
/// Create a PathCalculator object. The path calculator can be re-used
6330
/// to calculate json paths on different jsons.
6431
/// Unlike create(), this function will return results with full path as PTracker object.
@@ -73,6 +40,32 @@ pub fn compile<'i>(s: &'i str) -> Result<Query<'i>, QueryCompilationError> {
7340
json_path::compile(s)
7441
}
7542

43+
pub fn calc_once<'j, 'p, S:SelectValue>(mut q: Query<'j>, json: &'p S) -> Vec<&'p S> {
44+
let root = q.query.next().unwrap();
45+
PathCalculator::<'p, DummyTrackerGenerator> {
46+
query: None,
47+
tracker_generator: None,
48+
}.calc_with_paths_on_root(json, root).into_iter().map(|e: CalculationResult<'p, S, DummyTracker>| e.res).collect()
49+
}
50+
51+
pub fn calc_once_with_paths<'j, 'p, S:SelectValue>(mut q: Query<'j>, json: &'p S) -> Vec<CalculationResult<'p, S, PTracker>> {
52+
let root = q.query.next().unwrap();
53+
PathCalculator {
54+
query: None,
55+
tracker_generator: Some(PTrackerGenerator),
56+
}.calc_with_paths_on_root(json, root)
57+
}
58+
59+
pub fn calc_once_paths<'j, 'p, S:SelectValue>(mut q: Query<'j>, json: &'p S) -> Vec<Vec<String>> {
60+
let root = q.query.next().unwrap();
61+
PathCalculator {
62+
query: None,
63+
tracker_generator: Some(PTrackerGenerator),
64+
}.calc_with_paths_on_root(json, root).into_iter().map(|e| {
65+
e.path_tracker.unwrap().to_string_path()
66+
}).collect()
67+
}
68+
7669

7770
#[cfg(test)]
7871
mod json_path_tests {

0 commit comments

Comments
 (0)