@@ -3,7 +3,7 @@ use crate::parser::errors2::JsonPathParserError;
33use crate :: parser:: parser2:: Parsed ;
44
55/// Represents a JSONPath query with a list of segments.
6- #[ derive( Debug , Clone ) ]
6+ #[ derive( Debug , Clone , PartialEq ) ]
77pub struct JpQuery {
88 segments : Vec < Segment >
99}
@@ -22,7 +22,7 @@ impl Display for JpQuery {
2222 }
2323}
2424/// Enum representing different types of segments in a JSONPath query.
25- #[ derive( Debug , Clone ) ]
25+ #[ derive( Debug , Clone , PartialEq ) ]
2626pub enum Segment {
2727 /// Represents a descendant segment.
2828 Descendant ,
@@ -32,6 +32,12 @@ pub enum Segment {
3232 Selectors ( Vec < Selector > ) ,
3333}
3434
35+ impl Segment {
36+ pub fn name ( name : & str ) -> Self {
37+ Segment :: Selector ( Selector :: Name ( name. to_string ( ) ) )
38+ }
39+ }
40+
3541impl Display for Segment {
3642 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
3743 match self {
@@ -42,16 +48,16 @@ impl Display for Segment {
4248 }
4349}
4450/// Enum representing different types of selectors in a JSONPath query.
45- #[ derive( Debug , Clone ) ]
46- enum Selector {
51+ #[ derive( Debug , Clone , PartialEq ) ]
52+ pub enum Selector {
4753 /// Represents a name selector.
4854 Name ( String ) ,
4955 /// Represents a wildcard selector.
5056 Wildcard ,
5157 /// Represents an index selector.
5258 Index ( i64 ) ,
5359 /// Represents a slice selector.
54- Slice ( i64 , i64 , i64 ) ,
60+ Slice ( Option < i64 > , Option < i64 > , Option < i64 > ) ,
5561 /// Represents a filter selector.
5662 Filter ( Filter ) ,
5763}
@@ -62,37 +68,41 @@ impl Display for Selector {
6268 Selector :: Name ( name) => write ! ( f, "{}" , name) ,
6369 Selector :: Wildcard => write ! ( f, "*" ) ,
6470 Selector :: Index ( index) => write ! ( f, "{}" , index) ,
65- Selector :: Slice ( start, end, step) => write ! ( f, "{}:{}:{}" , start, end, step) ,
71+ Selector :: Slice ( start, end, step) => write ! ( f, "{}:{}:{}" ,
72+ start. unwrap_or( 0 ) ,
73+ end. unwrap_or( 0 ) ,
74+ step. unwrap_or( 1 ) ) ,
6675 Selector :: Filter ( filter) => write ! ( f, "[?{}]" , filter) ,
6776 }
6877 }
6978}
7079/// Enum representing different types of filters in a JSONPath query.
71- #[ derive( Debug , Clone ) ]
80+ #[ derive( Debug , Clone , PartialEq ) ]
7281pub enum Filter {
7382 /// Represents a logical OR filter.
74- Or ( Box < Filter > , Box < Filter > ) ,
83+ Or ( Vec < Filter > ) ,
7584 /// Represents a logical AND filter.
76- And ( Box < Filter > , Box < Filter > ) ,
77- /// Represents a logical NOT filter.
78- Not ( Box < Filter > ) ,
85+ And ( Vec < Filter > ) ,
7986 /// Represents an atomic filter.
8087 Atom ( FilterAtom ) ,
8188}
8289
8390impl Display for Filter {
8491 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
92+
93+ let items_to_str = |items : & Vec < Filter > , sep : & str |
94+ items. iter ( ) . map ( |f| f. to_string ( ) ) . collect :: < Vec < _ > > ( ) . join ( sep) ;
95+
8596 match self {
86- Filter :: Or ( left, right) => write ! ( f, "{} || {}" , left, right) ,
87- Filter :: And ( left, right) => write ! ( f, "{} && {}" , left, right) ,
88- Filter :: Not ( expr) => write ! ( f, "!{}" , expr) ,
97+ Filter :: Or ( filters) => write ! ( f, "{}" , items_to_str( filters, " || " ) ) ,
98+ Filter :: And ( filters) => write ! ( f, "{}" , items_to_str( filters, " && " ) ) ,
8999 Filter :: Atom ( atom) => write ! ( f, "{}" , atom) ,
90100 }
91101 }
92102}
93103
94104/// Enum representing different types of atomic filters in a JSONPath query.
95- #[ derive( Debug , Clone ) ]
105+ #[ derive( Debug , Clone , PartialEq ) ]
96106pub enum FilterAtom {
97107 /// Represents a nested filter with an optional NOT flag.
98108 Filter {
@@ -144,7 +154,7 @@ impl Display for FilterAtom {
144154 }
145155}
146156/// Enum representing different types of comparisons in a JSONPath query.
147- #[ derive( Debug , Clone ) ]
157+ #[ derive( Debug , Clone , PartialEq ) ]
148158pub enum Comparison {
149159 /// Represents an equality comparison.
150160 Eq ( Comparable , Comparable ) ,
@@ -188,7 +198,7 @@ impl Display for Comparison {
188198}
189199
190200/// Enum representing different types of comparable values in a JSONPath query.
191- #[ derive( Debug , Clone ) ]
201+ #[ derive( Debug , Clone , PartialEq ) ]
192202pub enum Comparable {
193203 /// Represents a literal value.
194204 Literal ( Literal ) ,
@@ -245,7 +255,7 @@ impl Display for SingularQuerySegment {
245255}
246256
247257/// Enum representing different types of tests in a JSONPath query.
248- #[ derive( Debug , Clone ) ]
258+ #[ derive( Debug , Clone , PartialEq ) ]
249259pub enum Test {
250260 /// Represents a relative query.
251261 RelQuery ( Vec < Segment > ) ,
@@ -266,7 +276,7 @@ impl Display for Test {
266276}
267277
268278/// Enum representing different types of test functions in a JSONPath query.
269- #[ derive( Debug , Clone ) ]
279+ #[ derive( Debug , Clone , PartialEq ) ]
270280pub enum TestFunction {
271281 /// Represents a custom function.
272282 Custom ( String , Vec < FnArg > ) ,
@@ -311,7 +321,7 @@ impl Display for TestFunction {
311321}
312322
313323/// Enum representing different types of function arguments in a JSONPath query.
314- #[ derive( Debug , Clone ) ]
324+ #[ derive( Debug , Clone , PartialEq ) ]
315325pub enum FnArg {
316326 /// Represents a literal argument.
317327 Literal ( Literal ) ,
0 commit comments