Skip to content

Use pest rules to validate whitespace #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions rfc9535/test_suite/results.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
Total; Passed; Failed; Date
687; 652; 10; 2025-03-18 22:29:42
687; 652; 5; 2025-03-18 22:34:48
687; 652; 4; 2025-03-18 22:40:55
687; 652; 2; 2025-03-18 22:41:43
687; 652; 1; 2025-03-18 22:41:53
Expand All @@ -9,3 +7,4 @@ Total; Passed; Failed; Date
687; 652; 0; 2025-03-18 22:56:39
687; 652; 0; 2025-03-18 22:57:01
687; 652; 0; 2025-05-13 21:21:59
687; 652; 0; 2025-05-19 15:05:31
18 changes: 6 additions & 12 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ pub type Parsed<T> = Result<T, JsonPathError>;
///
/// Returns a variant of [crate::JsonPathParserError] if the parsing operation failed.
pub fn parse_json_path(jp_str: &str) -> Parsed<JpQuery> {
if jp_str != jp_str.trim() {
Err(JsonPathError::InvalidJsonPath(
"Leading or trailing whitespaces".to_string(),
))
} else {
JSPathParser::parse(Rule::main, jp_str)
.map_err(Box::new)?
.next()
.ok_or(JsonPathError::UnexpectedPestOutput)
.and_then(next_down)
.and_then(jp_query)
}
JSPathParser::parse(Rule::main, jp_str)
.map_err(Box::new)?
.next()
.ok_or(JsonPathError::UnexpectedPestOutput)
.and_then(next_down)
.and_then(jp_query)
}

pub fn jp_query(rule: Pair<Rule>) -> Parsed<JpQuery> {
Expand Down
4 changes: 2 additions & 2 deletions src/parser/grammar/json_path_9535.pest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main = { SOI ~ jp_query ~ EOI }
main = ${ SOI ~ jp_query ~ EOI }
jp_query = {root ~ segments}
segments = {(S ~ segment)*}
segments = !{(S ~ segment)*}
segment = { child_segment | descendant_segment }
child_segment = { bracketed_selection | ("." ~ (wildcard_selector | member_name_shorthand)) }
bracketed_selection = { "[" ~ S ~ selector ~ (S ~ "," ~ S ~ selector)* ~ S ~ "]" }
Expand Down
Loading