Skip to content

Commit 1f94aac

Browse files
committed
adding some helper functions
1 parent 43cd68c commit 1f94aac

4 files changed

Lines changed: 61 additions & 14 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "json_patch_ext"
2+
name = "json-patch-ext"
33
version = "0.1.0"
44
edition = "2021"
55

src/lib.rs

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
//! { "foo": {"bar": 1}}
2727
//! ```
2828
29-
pub mod errors;
29+
mod errors;
30+
mod macros;
3031

3132
use 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)]
4447
use 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+
6496
pub 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)]
176208
mod 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,

src/macros.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[macro_export]
2+
macro_rules! format_ptr {
3+
($str:literal $($args:tt)*) => {
4+
json_patch_ext::PointerBuf::parse(&format!($str $($args)*)).expect("pointer parse error")
5+
};
6+
}

0 commit comments

Comments
 (0)