1
1
#![ allow( clippy:: empty_docs) ]
2
2
3
- use crate :: parser:: errors:: JsonPathParserError :: ParserError ;
4
- use crate :: parser:: errors:: { parser_err, JsonPathParserError } ;
3
+ use crate :: parser:: errors:: JsonPathParserError ;
5
4
use crate :: parser:: model:: FilterExpression :: { And , Not , Or } ;
6
5
use crate :: parser:: model:: {
7
6
FilterExpression , FilterSign , Function , JsonPath , JsonPathIndex , Operand ,
@@ -20,9 +19,10 @@ struct JsonPathParser;
20
19
///
21
20
/// Returns a variant of [JsonPathParserError] if the parsing operation failed.
22
21
pub fn parse_json_path ( jp_str : & str ) -> Result < JsonPath , JsonPathParserError > {
23
- JsonPathParser :: parse ( Rule :: path, jp_str) ?
22
+ JsonPathParser :: parse ( Rule :: path, jp_str)
23
+ . map_err ( Box :: new) ?
24
24
. next ( )
25
- . ok_or ( parser_err ( jp_str ) )
25
+ . ok_or ( JsonPathParserError :: UnexpectedPestOutput )
26
26
. and_then ( parse_internal)
27
27
}
28
28
@@ -36,7 +36,7 @@ fn parse_internal(rule: Pair<Rule>) -> Result<JsonPath, JsonPathParserError> {
36
36
Rule :: path => rule
37
37
. into_inner ( )
38
38
. next ( )
39
- . ok_or ( parser_err ( "expected a Rule::path but found nothing" ) )
39
+ . ok_or ( JsonPathParserError :: NoRulePath )
40
40
. and_then ( parse_internal) ,
41
41
Rule :: current => rule
42
42
. into_inner ( )
@@ -53,14 +53,14 @@ fn parse_internal(rule: Pair<Rule>) -> Result<JsonPath, JsonPathParserError> {
53
53
Rule :: wildcard => Ok ( JsonPath :: Wildcard ) ,
54
54
Rule :: descent => parse_key ( down ( rule) ?) ?
55
55
. map ( JsonPath :: Descent )
56
- . ok_or ( parser_err ( "expected a JsonPath::Descent but found nothing" ) ) ,
56
+ . ok_or ( JsonPathParserError :: NoJsonPathDescent ) ,
57
57
Rule :: descent_w => Ok ( JsonPath :: DescentW ) ,
58
58
Rule :: function => Ok ( JsonPath :: Fn ( Function :: Length ) ) ,
59
59
Rule :: field => parse_key ( down ( rule) ?) ?
60
60
. map ( JsonPath :: Field )
61
- . ok_or ( parser_err ( "expected a JsonPath::Field but found nothing" ) ) ,
61
+ . ok_or ( JsonPathParserError :: NoJsonPathField ) ,
62
62
Rule :: index => parse_index ( rule) . map ( JsonPath :: Index ) ,
63
- _ => Err ( ParserError ( format ! ( "{ rule} did not match any 'Rule' " ) ) ) ,
63
+ rule => Err ( JsonPathParserError :: InvalidTopLevelRule ( rule) ) ,
64
64
}
65
65
}
66
66
@@ -106,12 +106,17 @@ fn number_to_value(number: &str) -> Result<Value, JsonPathParserError> {
106
106
. or_else ( || number. parse :: < f64 > ( ) . ok ( ) . map ( Value :: from) )
107
107
{
108
108
Some ( value) => Ok ( value) ,
109
- None => Err ( JsonPathParserError :: ParserError ( format ! (
110
- "Failed to parse {number} as either f64 or i64"
111
- ) ) ) ,
109
+ None => Err ( JsonPathParserError :: InvalidNumber ( number. to_string ( ) ) ) ,
112
110
}
113
111
}
114
112
113
+ fn bool_to_value ( boolean : & str ) -> Value {
114
+ boolean
115
+ . parse :: < bool > ( )
116
+ . map ( Value :: from)
117
+ . expect ( "unreachable: according to .pest this is either `true` or `false`" )
118
+ }
119
+
115
120
fn parse_unit_indexes ( pairs : Pairs < Rule > ) -> Result < JsonPathIndex , JsonPathParserError > {
116
121
let mut keys = vec ! [ ] ;
117
122
@@ -152,34 +157,40 @@ fn parse_filter_index(pair: Pair<Rule>) -> Result<JsonPathIndex, JsonPathParserE
152
157
153
158
fn parse_logic_or ( pairs : Pairs < Rule > ) -> Result < FilterExpression , JsonPathParserError > {
154
159
let mut expr: Option < FilterExpression > = None ;
155
- let error_message = format ! ( "Failed to parse logical expression: {:?}" , pairs) ;
156
- for pair in pairs {
160
+ // only possible for the loop not to produce any value (except Errors)
161
+ if pairs. len ( ) == 0 {
162
+ return Err ( JsonPathParserError :: UnexpectedNoneLogicError (
163
+ pairs. get_input ( ) . to_string ( ) ,
164
+ pairs. as_str ( ) . to_string ( ) ,
165
+ ) ) ;
166
+ }
167
+ for pair in pairs. into_iter ( ) {
157
168
let next_expr = parse_logic_and ( pair. into_inner ( ) ) ?;
158
169
match expr {
159
170
None => expr = Some ( next_expr) ,
160
171
Some ( e) => expr = Some ( Or ( Box :: new ( e) , Box :: new ( next_expr) ) ) ,
161
172
}
162
173
}
163
- match expr {
164
- Some ( expr) => Ok ( expr) ,
165
- None => Err ( JsonPathParserError :: ParserError ( error_message) ) ,
166
- }
174
+ Ok ( expr. expect ( "unreachable: above len() == 0 check should have catched this" ) )
167
175
}
168
176
169
177
fn parse_logic_and ( pairs : Pairs < Rule > ) -> Result < FilterExpression , JsonPathParserError > {
170
178
let mut expr: Option < FilterExpression > = None ;
171
- let error_message = format ! ( "Failed to parse logical `and` expression: {:?}" , pairs, ) ;
179
+ // only possible for the loop not to produce any value (except Errors)
180
+ if pairs. len ( ) == 0 {
181
+ return Err ( JsonPathParserError :: UnexpectedNoneLogicError (
182
+ pairs. get_input ( ) . to_string ( ) ,
183
+ pairs. as_str ( ) . to_string ( ) ,
184
+ ) ) ;
185
+ }
172
186
for pair in pairs {
173
187
let next_expr = parse_logic_not ( pair. into_inner ( ) ) ?;
174
188
match expr {
175
189
None => expr = Some ( next_expr) ,
176
190
Some ( e) => expr = Some ( And ( Box :: new ( e) , Box :: new ( next_expr) ) ) ,
177
191
}
178
192
}
179
- match expr {
180
- Some ( expr) => Ok ( expr) ,
181
- None => Err ( JsonPathParserError :: ParserError ( error_message) ) ,
182
- }
193
+ Ok ( expr. expect ( "unreachable: above len() == 0 check should have catched this" ) )
183
194
}
184
195
185
196
fn parse_logic_not ( mut pairs : Pairs < Rule > ) -> Result < FilterExpression , JsonPathParserError > {
@@ -191,10 +202,13 @@ fn parse_logic_not(mut pairs: Pairs<Rule>) -> Result<FilterExpression, JsonPathP
191
202
. map ( |expr|Not ( Box :: new ( expr) ) )
192
203
} ,
193
204
Rule :: logic_atom => parse_logic_atom ( pairs. next ( ) . expect ( "unreachable in arithmetic: should have a value as pairs.peek() was Some(_)" ) . into_inner ( ) ) ,
194
- x => Err ( JsonPathParserError :: UnexpectedRuleLogicError ( x , pairs) ) ,
205
+ rule => Err ( JsonPathParserError :: UnexpectedRuleLogicError ( rule , pairs. get_input ( ) . to_string ( ) , pairs . as_str ( ) . to_string ( ) ) ) ,
195
206
}
196
207
} else {
197
- Err ( JsonPathParserError :: UnexpectedNoneLogicError ( pairs) )
208
+ Err ( JsonPathParserError :: UnexpectedNoneLogicError (
209
+ pairs. get_input ( ) . to_string ( ) ,
210
+ pairs. as_str ( ) . to_string ( ) ,
211
+ ) )
198
212
}
199
213
}
200
214
@@ -213,10 +227,13 @@ fn parse_logic_atom(mut pairs: Pairs<Rule>) -> Result<FilterExpression, JsonPath
213
227
Ok ( FilterExpression :: Atom ( left, sign, right) )
214
228
}
215
229
}
216
- x => Err ( JsonPathParserError :: UnexpectedRuleLogicError ( x , pairs) ) ,
230
+ rule => Err ( JsonPathParserError :: UnexpectedRuleLogicError ( rule , pairs. get_input ( ) . to_string ( ) , pairs . as_str ( ) . to_string ( ) ) ) ,
217
231
}
218
232
} else {
219
- Err ( JsonPathParserError :: UnexpectedNoneLogicError ( pairs) )
233
+ Err ( JsonPathParserError :: UnexpectedNoneLogicError (
234
+ pairs. get_input ( ) . to_string ( ) ,
235
+ pairs. as_str ( ) . to_string ( ) ,
236
+ ) )
220
237
}
221
238
}
222
239
@@ -226,7 +243,7 @@ fn parse_atom(rule: Pair<Rule>) -> Result<Operand, JsonPathParserError> {
226
243
Rule :: number => Operand :: Static ( number_to_value ( rule. as_str ( ) ) ?) ,
227
244
Rule :: string_qt => Operand :: Static ( Value :: from ( down ( atom) ?. as_str ( ) ) ) ,
228
245
Rule :: chain => parse_chain_in_operand ( down ( rule) ?) ?,
229
- Rule :: boolean => Operand :: Static ( rule. as_str ( ) . parse :: < Value > ( ) ? ) ,
246
+ Rule :: boolean => Operand :: Static ( bool_to_value ( rule. as_str ( ) ) ) ,
230
247
_ => Operand :: Static ( Value :: Null ) ,
231
248
} ;
232
249
Ok ( parsed_atom)
@@ -246,10 +263,10 @@ fn parse_index(rule: Pair<Rule>) -> Result<JsonPathIndex, JsonPathParserError> {
246
263
}
247
264
248
265
fn down ( rule : Pair < Rule > ) -> Result < Pair < Rule > , JsonPathParserError > {
249
- let error_message = format ! ( "Failed to get inner pairs for {:?}" , rule) ;
266
+ let error_message = rule. to_string ( ) ;
250
267
match rule. into_inner ( ) . next ( ) {
251
- Some ( rule) => Ok ( rule. to_owned ( ) ) ,
252
- None => Err ( ParserError ( error_message) ) ,
268
+ Some ( rule) => Ok ( rule) ,
269
+ None => Err ( JsonPathParserError :: EmptyInner ( error_message) ) ,
253
270
}
254
271
}
255
272
0 commit comments