Skip to content

Commit 128c565

Browse files
author
meir
committed
better error messages
1 parent 41ba954 commit 128c565

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/grammer.pest

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
complex_literal = _{(!("."|"*"|"\""|"'"|"["|"]"|"="|" "|"("|")"|"{"|"}"|"|"|"<"|">"|"!"|",") ~ ANY)+}
2-
literal = @{('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | "-" | "_")+ | complex_literal}
1+
literal = @{('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | "-" | "_" | "`" | "~" | "+" | "#" | "%" | "$" | "^" | "/")+}
32

43
string = _{("'" ~ (string_value) ~ "'") | ("\"" ~ (string_value) ~ "\"") | ("\"" ~ (string_value_escape_1) ~ "\"") | ("'" ~ (string_value_escape_2) ~ "'")}
54
string_escape = @{"\\"}

src/json_path.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ impl std::fmt::Display for QueryCompilationError {
2929
}
3030
}
3131

32+
impl std::fmt::Display for Rule {
33+
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
34+
match self {
35+
Rule::literal => write!(f, "<string>"),
36+
Rule::all => write!(f, "'*'"),
37+
Rule::full_scan => write!(f, "'..'"),
38+
Rule::numbers_list => write!(f, "'<number>[,<number>,...]'"),
39+
Rule::string_list => write!(f, "'<string>[,<string>,...]'"),
40+
Rule::numbers_range => write!(f, "['start:end:steps']"),
41+
Rule::number => write!(f, "'<number>'"),
42+
Rule::filter => write!(f, "'[?(filter_expression)]'"),
43+
_ => write!(f, "{:?}", self)
44+
}
45+
}
46+
}
47+
3248
pub (crate) fn compile<'i>(s: &'i str) -> Result<Query<'i>, QueryCompilationError> {
3349
let q = JsonPathParser::parse(Rule::query, s);
3450
match q {
@@ -47,12 +63,12 @@ pub (crate) fn compile<'i>(s: &'i str) -> Result<Query<'i>, QueryCompilationErro
4763
ref negatives,
4864
} => {
4965
let positives = if positives.len() > 0 {
50-
Some(positives.iter().map(|v| format!("{:?}", v)).collect::<Vec<_>>().join(", "))
66+
Some(positives.iter().map(|v| format!("{}", v)).collect::<Vec<_>>().join(", "))
5167
} else {
5268
None
5369
};
5470
let negatives = if negatives.len() > 0 {
55-
Some(negatives.iter().map(|v| format!("{:?}", v)).collect::<Vec<_>>().join(", "))
71+
Some(negatives.iter().map(|v| format!("{}", v)).collect::<Vec<_>>().join(", "))
5672
} else {
5773
None
5874
};

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ pub fn compile<'i>(s: &'i str) -> Result<Query<'i>, QueryCompilationError> {
4040
json_path::compile(s)
4141
}
4242

43+
/// Calc once allows to performe a one time calculation on the give query.
44+
/// The query ownership is taken so it can not be used after. This allows
45+
/// the get a better performance if there is a need to calculate the query
46+
/// only once.
4347
pub fn calc_once<'j, 'p, S:SelectValue>(mut q: Query<'j>, json: &'p S) -> Vec<&'p S> {
4448
let root = q.query.next().unwrap();
4549
PathCalculator::<'p, DummyTrackerGenerator> {
@@ -48,6 +52,7 @@ pub fn calc_once<'j, 'p, S:SelectValue>(mut q: Query<'j>, json: &'p S) -> Vec<&'
4852
}.calc_with_paths_on_root(json, root).into_iter().map(|e: CalculationResult<'p, S, DummyTracker>| e.res).collect()
4953
}
5054

55+
/// A version of `calc_once` that returns also paths.
5156
pub fn calc_once_with_paths<'j, 'p, S:SelectValue>(mut q: Query<'j>, json: &'p S) -> Vec<CalculationResult<'p, S, PTracker>> {
5257
let root = q.query.next().unwrap();
5358
PathCalculator {
@@ -56,6 +61,7 @@ pub fn calc_once_with_paths<'j, 'p, S:SelectValue>(mut q: Query<'j>, json: &'p S
5661
}.calc_with_paths_on_root(json, root)
5762
}
5863

64+
/// A version of `calc_once` that returns only paths as Vec<Vec<String>>.
5965
pub fn calc_once_paths<'j, 'p, S:SelectValue>(mut q: Query<'j>, json: &'p S) -> Vec<Vec<String>> {
6066
let root = q.query.next().unwrap();
6167
PathCalculator {

0 commit comments

Comments
 (0)