11use std:: fmt:: { Display , Formatter } ;
2+ use crate :: parser:: errors2:: JsonPathParserError ;
3+ use crate :: parser:: parser2:: Parsed ;
24
35/// Represents a JSONPath query with a list of segments.
46#[ derive( Debug , Clone ) ]
@@ -171,8 +173,8 @@ impl Display for Comparable {
171173}
172174
173175/// Enum representing different types of singular queries in a JSONPath query.
174- #[ derive( Debug , Clone ) ]
175- enum SingularQuery {
176+ #[ derive( Debug , Clone , PartialEq ) ]
177+ pub enum SingularQuery {
176178 /// Represents a current node query.
177179 Current ( Vec < SingularQuerySegment > ) ,
178180 /// Represents a root node query.
@@ -189,8 +191,8 @@ impl Display for SingularQuery {
189191}
190192
191193/// Enum representing different types of singular query segments in a JSONPath query.
192- #[ derive( Debug , Clone ) ]
193- enum SingularQuerySegment {
194+ #[ derive( Debug , Clone , PartialEq ) ]
195+ pub enum SingularQuerySegment {
194196 /// Represents an index segment.
195197 Index ( i64 ) ,
196198 /// Represents a name segment.
@@ -244,6 +246,21 @@ enum TestFunction {
244246 Match ( FnArg , FnArg ) ,
245247}
246248
249+ impl TestFunction {
250+ pub fn new ( name : & str , args : Vec < FnArg > ) -> Parsed < Self > {
251+ match ( name, args. as_slice ( ) ) {
252+ ( "length" , [ a] ) => Ok ( TestFunction :: Length ( Box :: new ( a. clone ( ) ) ) ) ,
253+ ( "value" , [ a] ) => Ok ( TestFunction :: Value ( a. clone ( ) ) ) ,
254+ ( "count" , [ a] ) => Ok ( TestFunction :: Count ( a. clone ( ) ) ) ,
255+ ( "search" , [ a, b] ) => Ok ( TestFunction :: Search ( a. clone ( ) , b. clone ( ) ) ) ,
256+ ( "match" , [ a, b] ) => Ok ( TestFunction :: Match ( a. clone ( ) , b. clone ( ) ) ) ,
257+ ( "length" | "value" | "count" | "match" | "search" , args ) =>
258+ Err ( JsonPathParserError :: InvalidJsonPath ( format ! ( "Invalid number of arguments for the function `{}`: got {}" , name, args. len( ) ) ) ) ,
259+ ( custom, _) => Ok ( TestFunction :: Custom ( custom. to_string ( ) , args) ) ,
260+ }
261+ }
262+ }
263+
247264impl Display for TestFunction {
248265 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
249266 match self {
0 commit comments