use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct PatchEntry {
op: String,
path: String,
#[serde(default)]
#[serde(skip_serializing_if = "Value::is_null")]
value: Value,
}
impl PatchEntry {
pub fn new<S1: Into<String>, S2: Into<String>>(op: S1, path: S2, value: Value) -> Self {
Self {
op: op.into(),
path: path.into(),
value,
}
}
pub fn op(&self) -> &str {
&self.op
}
pub fn path(&self) -> &str {
&self.path
}
pub const fn value(&self) -> &Value {
&self.value
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_patch_entry() {
let pe = PatchEntry::new("replace", "/enwiki/title", json!("Foo Bar"));
assert_eq!(pe.op, "replace");
assert_eq!(pe.path, "/enwiki/title");
assert_eq!(pe.value, json!("Foo Bar"));
}
#[test]
fn test_patch_entry_default() {
let pe = PatchEntry::default();
assert_eq!(pe.op, "");
assert_eq!(pe.path, "");
assert_eq!(pe.value, Value::Null);
}
#[test]
fn test_patch_entry_methods() {
let pe = PatchEntry::new("replace", "/enwiki/title", json!("Foo Bar"));
assert_eq!(pe.op(), "replace");
assert_eq!(pe.path(), "/enwiki/title");
assert_eq!(pe.value(), &json!("Foo Bar"));
}
}