use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
#[non_exhaustive]
pub enum Patch {
Set {
path: String,
value: Value,
},
Replace {
path: String,
value: Value,
},
Remove {
path: String,
},
Append {
path: String,
values: Vec<Value>,
},
Batch {
patches: Vec<Patch>,
},
}
impl Patch {
pub fn apply(&self, root: &mut Value) -> Result<(), PatchError> {
match self {
Self::Set { path, value } | Self::Replace { path, value } => set(root, path, value.clone()),
Self::Remove { path } => remove(root, path),
Self::Append { path, values } => append(root, path, values.clone()),
Self::Batch { patches } => {
for patch in patches {
patch.apply(root)?;
}
Ok(())
}
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum PatchError {
#[error("invalid JSON pointer `{0}`")]
InvalidPointer(String),
#[error("missing path `{0}`")]
Missing(String),
#[error("`{0}` is not an array")]
NotArray(String),
#[error("bad array index in `{0}`")]
BadIndex(String),
}
fn tokens(path: &str) -> Result<Vec<String>, PatchError> {
if path.is_empty() {
return Ok(Vec::new());
}
if !path.starts_with('/') {
return Err(PatchError::InvalidPointer(path.to_owned()));
}
Ok(path
.split('/')
.skip(1)
.map(|token| token.replace("~1", "/").replace("~0", "~"))
.collect())
}
fn set(root: &mut Value, path: &str, value: Value) -> Result<(), PatchError> {
let tokens = tokens(path)?;
if tokens.is_empty() {
*root = value;
return Ok(());
}
let (last, parents) = tokens
.split_last()
.ok_or_else(|| PatchError::Missing(path.to_owned()))?;
let mut current = root;
for token in parents {
current = child_mut(current, token, path)?;
}
match current {
Value::Object(object) => {
object.insert(last.clone(), value);
Ok(())
}
Value::Array(array) => {
let index = last
.parse::<usize>()
.map_err(|_| PatchError::BadIndex(path.to_owned()))?;
let slot = array
.get_mut(index)
.ok_or_else(|| PatchError::Missing(path.to_owned()))?;
*slot = value;
Ok(())
}
_ => Err(PatchError::Missing(path.to_owned())),
}
}
fn remove(root: &mut Value, path: &str) -> Result<(), PatchError> {
let tokens = tokens(path)?;
let (last, parents) = tokens
.split_last()
.ok_or_else(|| PatchError::Missing(path.to_owned()))?;
let mut current = root;
for token in parents {
current = child_mut(current, token, path)?;
}
match current {
Value::Object(object) => object
.remove(last)
.map(|_| ())
.ok_or_else(|| PatchError::Missing(path.to_owned())),
Value::Array(array) => {
let index = last
.parse::<usize>()
.map_err(|_| PatchError::BadIndex(path.to_owned()))?;
if index >= array.len() {
return Err(PatchError::Missing(path.to_owned()));
}
array.remove(index);
Ok(())
}
_ => Err(PatchError::Missing(path.to_owned())),
}
}
fn append(root: &mut Value, path: &str, values: Vec<Value>) -> Result<(), PatchError> {
let tokens = tokens(path)?;
let mut current = root;
for token in &tokens {
current = child_mut(current, token, path)?;
}
let Value::Array(array) = current else {
return Err(PatchError::NotArray(path.to_owned()));
};
array.extend(values);
Ok(())
}
fn child_mut<'a>(current: &'a mut Value, token: &str, path: &str) -> Result<&'a mut Value, PatchError> {
match current {
Value::Object(object) => object
.get_mut(token)
.ok_or_else(|| PatchError::Missing(path.to_owned())),
Value::Array(array) => {
let index = token
.parse::<usize>()
.map_err(|_| PatchError::BadIndex(path.to_owned()))?;
array.get_mut(index).ok_or_else(|| PatchError::Missing(path.to_owned()))
}
_ => Err(PatchError::Missing(path.to_owned())),
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn set_replace_remove_and_append() {
let mut value = json!({"widgets": [{"text": "a"}], "items": [1]});
Patch::Set {
path: "/widgets/0/text".to_owned(),
value: json!("b"),
}
.apply(&mut value)
.expect("set");
assert_eq!(value["widgets"][0]["text"], "b");
Patch::Append {
path: "/items".to_owned(),
values: vec![json!(2), json!(3)],
}
.apply(&mut value)
.expect("append");
assert_eq!(value["items"], json!([1, 2, 3]));
Patch::Remove {
path: "/items/0".to_owned(),
}
.apply(&mut value)
.expect("remove");
assert_eq!(value["items"], json!([2, 3]));
Patch::Batch {
patches: vec![
Patch::Set {
path: "/new".to_owned(),
value: json!(true),
},
Patch::Replace {
path: "/new".to_owned(),
value: json!(false),
},
],
}
.apply(&mut value)
.expect("batch");
assert_eq!(value["new"], false);
}
#[test]
fn bad_paths_are_errors() {
let mut value = json!({"a": 1});
assert!(
Patch::Set {
path: "a".to_owned(),
value: json!(2)
}
.apply(&mut value)
.is_err()
);
assert!(
Patch::Append {
path: "/a".to_owned(),
values: vec![json!(2)]
}
.apply(&mut value)
.is_err()
);
}
}