@@ -184,7 +184,7 @@ fn add_or_replace(obj: &mut Value, path: &PointerBuf, value: &Value, replace: bo
184184 Value :: Object ( map) => {
185185 let key = tail. decoded ( ) . into ( ) ;
186186 if replace && !map. contains_key ( & key) {
187- return Err ( PatchError :: TargetDoesNotExist ( path. as_str ( ) . into ( ) ) ) ;
187+ return Err ( PatchError :: TargetDoesNotExist ( path. to_string ( ) ) ) ;
188188 }
189189 map. insert ( key, value. clone ( ) ) ;
190190 } ,
@@ -202,7 +202,7 @@ fn add_or_replace(obj: &mut Value, path: &PointerBuf, value: &Value, replace: bo
202202 } ,
203203 } ,
204204 _ => {
205- return Err ( PatchError :: UnexpectedType ( path. as_str ( ) . into ( ) ) ) ;
205+ return Err ( PatchError :: UnexpectedType ( path. to_string ( ) ) ) ;
206206 } ,
207207 }
208208 }
@@ -216,9 +216,22 @@ fn remove(obj: &mut Value, path: &PointerBuf) -> Result<(), PatchError> {
216216 } ;
217217
218218 for v in patch_ext_helper ( subpath, obj, PatchMode :: Skip ) ? {
219- v. as_object_mut ( )
220- . ok_or ( PatchError :: UnexpectedType ( subpath. as_str ( ) . into ( ) ) ) ?
221- . remove ( key. decoded ( ) . as_ref ( ) ) ;
219+ match v {
220+ Value :: Object ( map) => {
221+ map. remove ( key. decoded ( ) . as_ref ( ) ) ;
222+ } ,
223+ Value :: Array ( vec) => {
224+ if let Index :: Num ( idx) = key. to_index ( ) ? {
225+ vec. get ( idx) . ok_or ( PatchError :: OutOfBounds ( idx) ) ?;
226+ vec. remove ( idx) ;
227+ } else {
228+ return Err ( PatchError :: UnexpectedType ( key. to_string ( ) ) ) ;
229+ }
230+ } ,
231+ _ => {
232+ return Err ( PatchError :: UnexpectedType ( path. to_string ( ) ) ) ;
233+ } ,
234+ }
222235 }
223236
224237 Ok ( ( ) )
@@ -363,6 +376,49 @@ mod tests {
363376 ) ;
364377 }
365378
379+ #[ rstest]
380+ fn test_patch_ext_add_vec1 ( mut data : Value ) {
381+ let path = format_ptr ! ( "/foo/1" ) ;
382+ let res = patch_ext ( & mut data, add_operation ( path, json ! ( 42 ) ) ) ;
383+ assert_ok ! ( res) ;
384+ assert_eq ! (
385+ data,
386+ json!( {
387+ "foo" : [
388+ { "baz" : { "buzz" : 0 } } ,
389+ 42 ,
390+ { "baz" : { "quzz" : 1 } } ,
391+ { "baz" : { "fixx" : 2 } } ,
392+ ] ,
393+ } )
394+ ) ;
395+ }
396+
397+ #[ rstest]
398+ fn test_patch_ext_add_vec2 ( mut data : Value ) {
399+ let path = format_ptr ! ( "/foo/-" ) ;
400+ let res = patch_ext ( & mut data, add_operation ( path, json ! ( 42 ) ) ) ;
401+ assert_ok ! ( res) ;
402+ assert_eq ! (
403+ data,
404+ json!( {
405+ "foo" : [
406+ { "baz" : { "buzz" : 0 } } ,
407+ { "baz" : { "quzz" : 1 } } ,
408+ { "baz" : { "fixx" : 2 } } ,
409+ 42 ,
410+ ] ,
411+ } )
412+ ) ;
413+ }
414+
415+ #[ rstest]
416+ fn test_patch_ext_add_vec_err ( mut data : Value ) {
417+ let path = format_ptr ! ( "/foo/a" ) ;
418+ let res = patch_ext ( & mut data, add_operation ( path, json ! ( 42 ) ) ) ;
419+ assert_err ! ( res) ;
420+ }
421+
366422 #[ rstest]
367423 fn test_patch_ext_add_escaped ( ) {
368424 let path = format_ptr ! ( "/foo/bar/{}" , escape( "testing.sh/baz" ) ) ;
@@ -389,11 +445,45 @@ mod tests {
389445 ) ;
390446 }
391447
448+ #[ rstest]
449+ fn test_patch_ext_replace_vec1 ( mut data : Value ) {
450+ let path = format_ptr ! ( "/foo/1" ) ;
451+ let res = patch_ext ( & mut data, replace_operation ( path, json ! ( 42 ) ) ) ;
452+ assert_ok ! ( res) ;
453+ assert_eq ! (
454+ data,
455+ json!( {
456+ "foo" : [
457+ { "baz" : { "buzz" : 0 } } ,
458+ 42 ,
459+ { "baz" : { "fixx" : 2 } } ,
460+ ] ,
461+ } )
462+ ) ;
463+ }
464+
465+ #[ rstest]
466+ fn test_patch_ext_replace_vec2 ( mut data : Value ) {
467+ let path = format_ptr ! ( "/foo/-" ) ;
468+ let res = patch_ext ( & mut data, replace_operation ( path, json ! ( 42 ) ) ) ;
469+ assert_ok ! ( res) ;
470+ assert_eq ! (
471+ data,
472+ json!( {
473+ "foo" : [
474+ { "baz" : { "buzz" : 0 } } ,
475+ { "baz" : { "quzz" : 1 } } ,
476+ { "baz" : { "fixx" : 2 } } ,
477+ 42 ,
478+ ] ,
479+ } )
480+ ) ;
481+ }
482+
392483 #[ rstest]
393484 fn test_patch_ext_replace_err ( mut data : Value ) {
394485 let path = format_ptr ! ( "/foo/*/baz/buzz" ) ;
395486 let res = patch_ext ( & mut data, replace_operation ( path, json ! ( 42 ) ) ) ;
396- println ! ( "{data:?}" ) ;
397487 assert_err ! ( res) ;
398488 }
399489
@@ -413,4 +503,27 @@ mod tests {
413503 } )
414504 ) ;
415505 }
506+
507+ #[ rstest]
508+ fn test_patch_ext_remove_vec ( mut data : Value ) {
509+ let path = format_ptr ! ( "/foo/1" ) ;
510+ let res = patch_ext ( & mut data, remove_operation ( path) ) ;
511+ assert_ok ! ( res) ;
512+ assert_eq ! (
513+ data,
514+ json!( {
515+ "foo" : [
516+ { "baz" : { "buzz" : 0 } } ,
517+ { "baz" : { "fixx" : 2 } } ,
518+ ] ,
519+ } )
520+ ) ;
521+ }
522+
523+ #[ rstest]
524+ fn test_patch_ext_remove_vec_err ( mut data : Value ) {
525+ let path = format_ptr ! ( "/foo/-" ) ;
526+ let res = patch_ext ( & mut data, remove_operation ( path) ) ;
527+ assert_err ! ( res) ;
528+ }
416529}
0 commit comments