1
+ use super :: errors:: JsonPathParserError ;
1
2
use super :: parse_json_path;
2
3
use serde_json:: Value ;
4
+ use std:: fmt:: { Display , Formatter } ;
3
5
use std:: { convert:: TryFrom , str:: FromStr } ;
4
6
5
- use super :: errors:: JsonPathParserError ;
6
-
7
7
/// The basic structures for parsing json paths.
8
8
/// The common logic of the structures pursues to correspond the internal parsing structure.
9
9
///
@@ -32,6 +32,26 @@ pub enum JsonPath {
32
32
Fn ( Function ) ,
33
33
}
34
34
35
+ impl Display for JsonPath {
36
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
37
+ let str = match self {
38
+ JsonPath :: Root => "$" . to_string ( ) ,
39
+ JsonPath :: Field ( e) => format ! ( ".'{}'" , e) ,
40
+ JsonPath :: Chain ( elems) => elems. iter ( ) . map ( ToString :: to_string) . collect :: < String > ( ) ,
41
+ JsonPath :: Descent ( e) => {
42
+ format ! ( "..{}" , e)
43
+ }
44
+ JsonPath :: DescentW => "..*" . to_string ( ) ,
45
+ JsonPath :: Index ( e) => e. to_string ( ) ,
46
+ JsonPath :: Current ( e) => format ! ( "@{}" , e) ,
47
+ JsonPath :: Wildcard => "[*]" . to_string ( ) ,
48
+ JsonPath :: Empty => "" . to_string ( ) ,
49
+ JsonPath :: Fn ( e) => format ! ( ".{}" , e) ,
50
+ } ;
51
+ write ! ( f, "{}" , str )
52
+ }
53
+ }
54
+
35
55
impl TryFrom < & str > for JsonPath {
36
56
type Error = JsonPathParserError ;
37
57
@@ -63,6 +83,16 @@ pub enum Function {
63
83
/// length()
64
84
Length ,
65
85
}
86
+
87
+ impl Display for Function {
88
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
89
+ let str = match self {
90
+ Function :: Length => "length()" . to_string ( ) ,
91
+ } ;
92
+ write ! ( f, "{}" , str )
93
+ }
94
+ }
95
+
66
96
#[ derive( Debug , Clone ) ]
67
97
pub enum JsonPathIndex {
68
98
/// A single element in array
@@ -77,6 +107,39 @@ pub enum JsonPathIndex {
77
107
Filter ( FilterExpression ) ,
78
108
}
79
109
110
+ impl Display for JsonPathIndex {
111
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
112
+ let str = match self {
113
+ JsonPathIndex :: Single ( e) => format ! ( "[{}]" , e) ,
114
+ JsonPathIndex :: UnionIndex ( elems) => {
115
+ format ! (
116
+ "[{}]" ,
117
+ elems
118
+ . iter( )
119
+ . map( ToString :: to_string)
120
+ . collect:: <Vec <_>>( )
121
+ . join( "," )
122
+ )
123
+ }
124
+ JsonPathIndex :: UnionKeys ( elems) => {
125
+ format ! (
126
+ "[{}]" ,
127
+ elems
128
+ . iter( )
129
+ . map( |el| format!( "'{}'" , el) )
130
+ . collect:: <Vec <_>>( )
131
+ . join( "," )
132
+ )
133
+ }
134
+ JsonPathIndex :: Slice ( s, e, st) => {
135
+ format ! ( "[{}:{}:{}]" , s, e, st)
136
+ }
137
+ JsonPathIndex :: Filter ( filter) => format ! ( "[?({})]" , filter) ,
138
+ } ;
139
+ write ! ( f, "{}" , str )
140
+ }
141
+ }
142
+
80
143
#[ derive( Debug , Clone , PartialEq ) ]
81
144
pub enum FilterExpression {
82
145
/// a single expression like a > 2
@@ -89,6 +152,26 @@ pub enum FilterExpression {
89
152
Not ( Box < FilterExpression > ) ,
90
153
}
91
154
155
+ impl Display for FilterExpression {
156
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
157
+ let str = match self {
158
+ FilterExpression :: Atom ( left, sign, right) => {
159
+ format ! ( "{} {} {}" , left, sign, right)
160
+ }
161
+ FilterExpression :: And ( left, right) => {
162
+ format ! ( "{} && {}" , left, right)
163
+ }
164
+ FilterExpression :: Or ( left, right) => {
165
+ format ! ( "{} || {}" , left, right)
166
+ }
167
+ FilterExpression :: Not ( expr) => {
168
+ format ! ( "!{}" , expr)
169
+ }
170
+ } ;
171
+ write ! ( f, "{}" , str )
172
+ }
173
+ }
174
+
92
175
impl FilterExpression {
93
176
pub fn exists ( op : Operand ) -> Self {
94
177
FilterExpression :: Atom (
@@ -106,6 +189,16 @@ pub enum Operand {
106
189
Dynamic ( Box < JsonPath > ) ,
107
190
}
108
191
192
+ impl Display for Operand {
193
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
194
+ let str = match self {
195
+ Operand :: Static ( e) => e. to_string ( ) ,
196
+ Operand :: Dynamic ( e) => e. to_string ( ) ,
197
+ } ;
198
+ write ! ( f, "{}" , str )
199
+ }
200
+ }
201
+
109
202
#[ allow( dead_code) ]
110
203
impl Operand {
111
204
pub fn val ( v : Value ) -> Self {
@@ -132,6 +225,28 @@ pub enum FilterSign {
132
225
Exists ,
133
226
}
134
227
228
+ impl Display for FilterSign {
229
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
230
+ let str = match self {
231
+ FilterSign :: Equal => "==" ,
232
+ FilterSign :: Unequal => "!=" ,
233
+ FilterSign :: Less => "<" ,
234
+ FilterSign :: Greater => ">" ,
235
+ FilterSign :: LeOrEq => "<=" ,
236
+ FilterSign :: GrOrEq => ">=" ,
237
+ FilterSign :: Regex => "~=" ,
238
+ FilterSign :: In => "in" ,
239
+ FilterSign :: Nin => "nin" ,
240
+ FilterSign :: Size => "size" ,
241
+ FilterSign :: NoneOf => "noneOf" ,
242
+ FilterSign :: AnyOf => "anyOf" ,
243
+ FilterSign :: SubSetOf => "subsetOf" ,
244
+ FilterSign :: Exists => "exists" ,
245
+ } ;
246
+ write ! ( f, "{}" , str )
247
+ }
248
+ }
249
+
135
250
impl FilterSign {
136
251
pub fn new ( key : & str ) -> Self {
137
252
match key {
0 commit comments