2626//! { "foo": {"bar": 1}}
2727//! ```
2828
29- pub mod errors;
29+ mod errors;
30+ mod macros;
3031
3132use json_patch:: patch;
3233// mark these as re-exports in the generated docs (maybe related to
@@ -36,13 +37,16 @@ pub use json_patch::{
3637 AddOperation ,
3738 CopyOperation ,
3839 MoveOperation ,
40+ Patch ,
3941 PatchOperation ,
4042 RemoveOperation ,
4143 ReplaceOperation ,
4244 TestOperation ,
4345} ;
46+ #[ doc( no_inline) ]
4447use jsonptr:: index:: Index ;
45- use jsonptr:: {
48+ use jsonptr:: Token ;
49+ pub use jsonptr:: {
4650 Pointer ,
4751 PointerBuf ,
4852} ;
@@ -51,7 +55,7 @@ use serde_json::{
5155 Value ,
5256} ;
5357
54- use crate :: errors:: PatchError ;
58+ pub use crate :: errors:: PatchError ;
5559
5660// PatchMode controls what to do if the referenced element does not exist in the object.
5761#[ derive( Debug , Clone , Copy ) ]
@@ -61,6 +65,34 @@ enum PatchMode {
6165 Skip ,
6266}
6367
68+ pub fn add_operation ( path : PointerBuf , value : Value ) -> PatchOperation {
69+ PatchOperation :: Add ( AddOperation { path, value } )
70+ }
71+
72+ pub fn replace_operation ( path : PointerBuf , value : Value ) -> PatchOperation {
73+ PatchOperation :: Replace ( ReplaceOperation { path, value } )
74+ }
75+
76+ pub fn remove_operation ( path : PointerBuf ) -> PatchOperation {
77+ PatchOperation :: Remove ( RemoveOperation { path } )
78+ }
79+
80+ pub fn move_operation ( from : PointerBuf , path : PointerBuf ) -> PatchOperation {
81+ PatchOperation :: Move ( MoveOperation { from, path } )
82+ }
83+
84+ pub fn copy_operation ( from : PointerBuf , path : PointerBuf ) -> PatchOperation {
85+ PatchOperation :: Copy ( CopyOperation { from, path } )
86+ }
87+
88+ pub fn test_operation ( path : PointerBuf , value : Value ) -> PatchOperation {
89+ PatchOperation :: Test ( TestOperation { path, value } )
90+ }
91+
92+ pub fn escape ( input : & str ) -> String {
93+ Token :: new ( input) . encoded ( ) . into ( )
94+ }
95+
6496pub fn patch_ext ( obj : & mut Value , p : PatchOperation ) -> Result < ( ) , PatchError > {
6597 match p {
6698 PatchOperation :: Add ( op) => add_or_replace ( obj, & op. path , & op. value , false ) ?,
@@ -175,10 +207,10 @@ fn patch_ext_helper<'a>(
175207#[ cfg( test) ]
176208mod tests {
177209 use assertables:: * ;
178- use jsonptr:: PointerBuf ;
179210 use rstest:: * ;
180211
181212 use super :: * ;
213+ use crate as json_patch_ext; // make the macros work in the tests
182214
183215 #[ fixture]
184216 fn data ( ) -> Value {
@@ -193,8 +225,8 @@ mod tests {
193225
194226 #[ rstest]
195227 fn test_patch_ext_add ( mut data : Value ) {
196- let path = PointerBuf :: parse ( "/foo/*/baz/buzz" ) . unwrap ( ) ;
197- let res = patch_ext ( & mut data, PatchOperation :: Add ( AddOperation { path, value : json ! ( 42 ) } ) ) ;
228+ let path = format_ptr ! ( "/foo/*/baz/buzz" ) ;
229+ let res = patch_ext ( & mut data, add_operation ( path, json ! ( 42 ) ) ) ;
198230 assert_ok ! ( res) ;
199231 assert_eq ! (
200232 data,
@@ -208,10 +240,19 @@ mod tests {
208240 ) ;
209241 }
210242
243+ #[ rstest]
244+ fn test_patch_ext_add_escaped ( ) {
245+ let path = format_ptr ! ( "/foo/bar/{}" , escape( "testing.sh/baz" ) ) ;
246+ let mut data = json ! ( { } ) ;
247+ let res = patch_ext ( & mut data, add_operation ( path, json ! ( 42 ) ) ) ;
248+ assert_ok ! ( res) ;
249+ assert_eq ! ( data, json!( { "foo" : { "bar" : { "testing.sh/baz" : 42 } } } ) ) ;
250+ }
251+
211252 #[ rstest]
212253 fn test_patch_ext_replace ( mut data : Value ) {
213- let path = PointerBuf :: parse ( "/foo/*/baz" ) . unwrap ( ) ;
214- let res = patch_ext ( & mut data, PatchOperation :: Replace ( ReplaceOperation { path, value : json ! ( 42 ) } ) ) ;
254+ let path = format_ptr ! ( "/foo/*/baz" ) ;
255+ let res = patch_ext ( & mut data, replace_operation ( path, json ! ( 42 ) ) ) ;
215256 assert_ok ! ( res) ;
216257 assert_eq ! (
217258 data,
@@ -227,17 +268,17 @@ mod tests {
227268
228269 #[ rstest]
229270 fn test_patch_ext_replace_err ( mut data : Value ) {
230- let path = PointerBuf :: parse ( "/foo/*/baz/buzz" ) . unwrap ( ) ;
231- let res = patch_ext ( & mut data, PatchOperation :: Replace ( ReplaceOperation { path, value : json ! ( 42 ) } ) ) ;
271+ let path = format_ptr ! ( "/foo/*/baz/buzz" ) ;
272+ let res = patch_ext ( & mut data, replace_operation ( path, json ! ( 42 ) ) ) ;
232273 println ! ( "{data:?}" ) ;
233274 assert_err ! ( res) ;
234275 }
235276
236277
237278 #[ rstest]
238279 fn test_patch_ext_remove ( mut data : Value ) {
239- let path = PointerBuf :: parse ( "/foo/*/baz/quzz" ) . unwrap ( ) ;
240- let res = patch_ext ( & mut data, PatchOperation :: Remove ( RemoveOperation { path } ) ) ;
280+ let path = format_ptr ! ( "/foo/*/baz/quzz" ) ;
281+ let res = patch_ext ( & mut data, remove_operation ( path) ) ;
241282 assert_ok ! ( res) ;
242283 assert_eq ! (
243284 data,
0 commit comments