@@ -4,18 +4,26 @@ use crate::parser::parser2::Parsed;
44
55/// Represents a JSONPath query with a list of segments.
66#[ derive( Debug , Clone ) ]
7- struct JpQuery {
7+ pub struct JpQuery {
88 segments : Vec < Segment >
99}
1010
11+ impl JpQuery {
12+ pub fn new ( segments : Vec < Segment > ) -> Self {
13+ JpQuery {
14+ segments
15+ }
16+ }
17+ }
18+
1119impl Display for JpQuery {
1220 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
1321 write ! ( f, "${}" , self . segments. iter( ) . map( |s| s. to_string( ) ) . collect:: <String >( ) )
1422 }
1523}
1624/// Enum representing different types of segments in a JSONPath query.
1725#[ derive( Debug , Clone ) ]
18- enum Segment {
26+ pub enum Segment {
1927 /// Represents a descendant segment.
2028 Descendant ,
2129 /// Represents a selector segment.
@@ -61,7 +69,7 @@ impl Display for Selector {
6169}
6270/// Enum representing different types of filters in a JSONPath query.
6371#[ derive( Debug , Clone ) ]
64- enum Filter {
72+ pub enum Filter {
6573 /// Represents a logical OR filter.
6674 Or ( Box < Filter > , Box < Filter > ) ,
6775 /// Represents a logical AND filter.
@@ -85,7 +93,7 @@ impl Display for Filter {
8593
8694/// Enum representing different types of atomic filters in a JSONPath query.
8795#[ derive( Debug , Clone ) ]
88- enum FilterAtom {
96+ pub enum FilterAtom {
8997 /// Represents a nested filter with an optional NOT flag.
9098 Filter {
9199 expr : Box < Filter > ,
@@ -100,6 +108,20 @@ enum FilterAtom {
100108 Comparison ( Box < Comparison > ) ,
101109}
102110
111+ impl FilterAtom {
112+ pub fn filter ( expr : Filter , not : bool ) -> Self {
113+ FilterAtom :: Filter { expr : Box :: new ( expr) , not }
114+ }
115+
116+ pub fn test ( expr : Test , not : bool ) -> Self {
117+ FilterAtom :: Test { expr : Box :: new ( expr) , not }
118+ }
119+
120+ pub fn cmp ( cmp : Box < Comparison > ) -> Self {
121+ FilterAtom :: Comparison ( cmp)
122+ }
123+ }
124+
103125impl Display for FilterAtom {
104126 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
105127 match self {
@@ -123,7 +145,7 @@ impl Display for FilterAtom {
123145}
124146/// Enum representing different types of comparisons in a JSONPath query.
125147#[ derive( Debug , Clone ) ]
126- enum Comparison {
148+ pub enum Comparison {
127149 /// Represents an equality comparison.
128150 Eq ( Comparable , Comparable ) ,
129151 /// Represents a non-equality comparison.
@@ -138,6 +160,20 @@ enum Comparison {
138160 Lte ( Comparable , Comparable ) ,
139161}
140162
163+ impl Comparison {
164+ pub fn try_new ( op : & str , left : Comparable , right : Comparable ) -> Parsed < Self > {
165+ match op {
166+ "==" => Ok ( Comparison :: Eq ( left, right) ) ,
167+ "!=" => Ok ( Comparison :: Ne ( left, right) ) ,
168+ ">" => Ok ( Comparison :: Gt ( left, right) ) ,
169+ ">=" => Ok ( Comparison :: Gte ( left, right) ) ,
170+ "<" => Ok ( Comparison :: Lt ( left, right) ) ,
171+ "<=" => Ok ( Comparison :: Lte ( left, right) ) ,
172+ _ => Err ( JsonPathParserError :: InvalidJsonPath ( format ! ( "Invalid comparison operator: {}" , op) ) ) ,
173+ }
174+ }
175+ }
176+
141177impl Display for Comparison {
142178 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
143179 match self {
@@ -153,7 +189,7 @@ impl Display for Comparison {
153189
154190/// Enum representing different types of comparable values in a JSONPath query.
155191#[ derive( Debug , Clone ) ]
156- enum Comparable {
192+ pub enum Comparable {
157193 /// Represents a literal value.
158194 Literal ( Literal ) ,
159195 /// Represents a function.
@@ -210,7 +246,7 @@ impl Display for SingularQuerySegment {
210246
211247/// Enum representing different types of tests in a JSONPath query.
212248#[ derive( Debug , Clone ) ]
213- enum Test {
249+ pub enum Test {
214250 /// Represents a relative query.
215251 RelQuery ( Vec < Segment > ) ,
216252 /// Represents an absolute query.
@@ -231,7 +267,7 @@ impl Display for Test {
231267
232268/// Enum representing different types of test functions in a JSONPath query.
233269#[ derive( Debug , Clone ) ]
234- enum TestFunction {
270+ pub enum TestFunction {
235271 /// Represents a custom function.
236272 Custom ( String , Vec < FnArg > ) ,
237273 /// Represents a length function.
@@ -247,7 +283,7 @@ enum TestFunction {
247283}
248284
249285impl TestFunction {
250- pub fn new ( name : & str , args : Vec < FnArg > ) -> Parsed < Self > {
286+ pub fn try_new ( name : & str , args : Vec < FnArg > ) -> Parsed < Self > {
251287 match ( name, args. as_slice ( ) ) {
252288 ( "length" , [ a] ) => Ok ( TestFunction :: Length ( Box :: new ( a. clone ( ) ) ) ) ,
253289 ( "value" , [ a] ) => Ok ( TestFunction :: Value ( a. clone ( ) ) ) ,
@@ -276,7 +312,7 @@ impl Display for TestFunction {
276312
277313/// Enum representing different types of function arguments in a JSONPath query.
278314#[ derive( Debug , Clone ) ]
279- enum FnArg {
315+ pub enum FnArg {
280316 /// Represents a literal argument.
281317 Literal ( Literal ) ,
282318 /// Represents a test argument.
0 commit comments