116116
117117use crate :: parser:: model:: JsonPath ;
118118use crate :: parser:: parser:: parse_json_path;
119- use crate :: path:: config:: JsonPathConfig ;
120119use crate :: path:: { json_path_instance, PathInstance } ;
121120use serde_json:: Value ;
122121use std:: convert:: TryInto ;
@@ -184,12 +183,8 @@ impl FromStr for JsonPathInst {
184183}
185184
186185impl JsonPathInst {
187- pub fn find_slice < ' a > (
188- & ' a self ,
189- value : & ' a Value ,
190- cfg : JsonPathConfig ,
191- ) -> Vec < JsonPtr < ' a , Value > > {
192- json_path_instance ( & self . inner , value, cfg)
186+ pub fn find_slice < ' a > ( & ' a self , value : & ' a Value ) -> Vec < JsonPtr < ' a , Value > > {
187+ json_path_instance ( & self . inner , value)
193188 . find ( JsonPathValue :: from_root ( value) )
194189 . into_iter ( )
195190 . filter ( |v| v. has_value ( ) )
@@ -230,27 +225,13 @@ impl JsonPathQuery for Box<Value> {
230225 }
231226}
232227
233- impl JsonPathQuery for ( Box < Value > , JsonPathConfig ) {
234- fn path ( self , query : & str ) -> Result < Value , String > {
235- let p = JsonPathInst :: from_str ( query) ?;
236- Ok ( JsonPathFinder :: new_with_cfg ( self . 0 , Box :: new ( p) , self . 1 ) . find ( ) )
237- }
238- }
239-
240228impl JsonPathQuery for Value {
241229 fn path ( self , query : & str ) -> Result < Value , String > {
242230 let p = JsonPathInst :: from_str ( query) ?;
243231 Ok ( JsonPathFinder :: new ( Box :: new ( self ) , Box :: new ( p) ) . find ( ) )
244232 }
245233}
246234
247- impl JsonPathQuery for ( Value , JsonPathConfig ) {
248- fn path ( self , query : & str ) -> Result < Value , String > {
249- let p = JsonPathInst :: from_str ( query) ?;
250- Ok ( JsonPathFinder :: new_with_cfg ( Box :: new ( self . 0 ) , Box :: new ( p) , self . 1 ) . find ( ) )
251- }
252- }
253-
254235/// just to create a json path value of data
255236/// Example:
256237/// - json_path_value(&json) = `JsonPathValue::Slice(&json)`
@@ -314,7 +295,6 @@ type JsPathStr = String;
314295pub ( crate ) fn jsp_idx ( prefix : & str , idx : usize ) -> String {
315296 format ! ( "{}[{}]" , prefix, idx)
316297}
317-
318298pub ( crate ) fn jsp_obj ( prefix : & str , key : & str ) -> String {
319299 format ! ( "{}.['{}']" , prefix, key)
320300}
@@ -358,7 +338,7 @@ impl<'a, Data: Clone + Debug + Default> JsonPathValue<'a, Data> {
358338}
359339
360340impl < ' a , Data > JsonPathValue < ' a , Data > {
361- fn only_no_value ( input : & [ JsonPathValue < ' a , Data > ] ) -> bool {
341+ fn only_no_value ( input : & Vec < JsonPathValue < ' a , Data > > ) -> bool {
362342 !input. is_empty ( ) && input. iter ( ) . filter ( |v| v. has_value ( ) ) . count ( ) == 0
363343 }
364344 fn map_vec ( data : Vec < ( & ' a Data , JsPathStr ) > ) -> Vec < JsonPathValue < ' a , Data > > {
@@ -428,7 +408,6 @@ impl<'a, Data> JsonPathValue<'a, Data> {
428408pub struct JsonPathFinder {
429409 json : Box < Value > ,
430410 path : Box < JsonPathInst > ,
431- cfg : JsonPathConfig ,
432411}
433412
434413impl Debug for JsonPathFinder {
@@ -445,20 +424,7 @@ impl Debug for JsonPathFinder {
445424impl JsonPathFinder {
446425 /// creates a new instance of [JsonPathFinder]
447426 pub fn new ( json : Box < Value > , path : Box < JsonPathInst > ) -> Self {
448- JsonPathFinder {
449- json,
450- path,
451- cfg : JsonPathConfig :: default ( ) ,
452- }
453- }
454-
455- pub fn new_with_cfg ( json : Box < Value > , path : Box < JsonPathInst > , cfg : JsonPathConfig ) -> Self {
456- JsonPathFinder { json, path, cfg }
457- }
458-
459- /// sets a cfg with a new one
460- pub fn set_cfg ( & mut self , cfg : JsonPathConfig ) {
461- self . cfg = cfg
427+ JsonPathFinder { json, path }
462428 }
463429
464430 /// updates a path with a new one
@@ -486,15 +452,10 @@ impl JsonPathFinder {
486452 let path = Box :: new ( JsonPathInst :: from_str ( path) ?) ;
487453 Ok ( JsonPathFinder :: new ( json, path) )
488454 }
489- pub fn from_str_with_cfg ( json : & str , path : & str , cfg : JsonPathConfig ) -> Result < Self , String > {
490- let json = serde_json:: from_str ( json) . map_err ( |e| e. to_string ( ) ) ?;
491- let path = Box :: new ( JsonPathInst :: from_str ( path) ?) ;
492- Ok ( JsonPathFinder :: new_with_cfg ( json, path, cfg) )
493- }
494455
495456 /// creates an instance to find a json slice from the json
496457 pub fn instance ( & self ) -> PathInstance {
497- json_path_instance ( & self . path . inner , & self . json , self . cfg . clone ( ) )
458+ json_path_instance ( & self . path . inner , & self . json )
498459 }
499460 /// finds a slice of data in the set json.
500461 /// The result is a vector of references to the incoming structure.
@@ -545,7 +506,6 @@ impl JsonPathFinder {
545506
546507#[ cfg( test) ]
547508mod tests {
548- use crate :: path:: config:: JsonPathConfig ;
549509 use crate :: JsonPathQuery ;
550510 use crate :: JsonPathValue :: { NoValue , Slice } ;
551511 use crate :: { jp_v, JsonPathFinder , JsonPathInst , JsonPathValue } ;
@@ -1246,7 +1206,7 @@ mod tests {
12461206 let query = JsonPathInst :: from_str ( "$..book[?(@.author size 10)].title" )
12471207 . expect ( "the path is correct" ) ;
12481208
1249- let results = query. find_slice ( & json, JsonPathConfig :: default ( ) ) ;
1209+ let results = query. find_slice ( & json) ;
12501210 let v = results. first ( ) . expect ( "to get value" ) ;
12511211
12521212 // V can be implicitly converted to &Value
@@ -1309,7 +1269,7 @@ mod tests {
13091269 v,
13101270 vec![ Slice (
13111271 & json!( { "second" : { "active" : 1 } } ) ,
1312- "$.['first']" . to_string( ) ,
1272+ "$.['first']" . to_string( )
13131273 ) ]
13141274 ) ;
13151275
@@ -1323,7 +1283,7 @@ mod tests {
13231283 v,
13241284 vec![ Slice (
13251285 & json!( { "second" : { "active" : 1 } } ) ,
1326- "$.['first']" . to_string( ) ,
1286+ "$.['first']" . to_string( )
13271287 ) ]
13281288 ) ;
13291289
@@ -1337,7 +1297,7 @@ mod tests {
13371297 v,
13381298 vec![ Slice (
13391299 & json!( { "second" : { "active" : 1 } } ) ,
1340- "$.['first']" . to_string( ) ,
1300+ "$.['first']" . to_string( )
13411301 ) ]
13421302 ) ;
13431303
@@ -1351,7 +1311,7 @@ mod tests {
13511311 v,
13521312 vec![ Slice (
13531313 & json!( { "second" : { "active" : 1 } } ) ,
1354- "$.['first']" . to_string( ) ,
1314+ "$.['first']" . to_string( )
13551315 ) ]
13561316 ) ;
13571317 }
0 commit comments