pub enum PatchOperation {
Add {
path: String,
value: Value,
},
Remove {
path: String,
},
Replace {
path: String,
value: Value,
},
Move {
from: String,
path: String,
},
Copy {
from: String,
path: String,
},
Test {
path: String,
value: Value,
},
}Expand description
A single RFC 6902 operation.
The serde representation is the RFC wire format exactly: an object with an
op discriminator, a path JSON Pointer, and — depending on the operation
— a value or a from pointer.
§Why value has a default
Upstream types both patch fields as z.array(z.any()) / List[Any] and
validates nothing, so a producer that drops value still parses there. The
case is not hypothetical: JSON.stringify({op: "add", path: "/x", value: undefined}) yields {"op":"add","path":"/x"}, which is what a JavaScript
producer emits whenever the new state holds undefined at that key. Making
value required would turn that into a deserialization failure for the whole
STATE_DELTA event — and in an SSE stream a failed event is usually a failed
run. An omitted value therefore reads as JSON null, which is how the
JavaScript patch libraries apply it, and re-serializes explicitly as null.
Everything else about the operation stays strictly typed: an unrecognized
op is still rejected, because the six RFC operations are the whole
vocabulary and a seventh is a producer bug worth surfacing rather than
carrying silently to an applier that cannot execute it.
let op = PatchOperation::Replace {
path: "/counter".into(),
value: serde_json::json!(2),
};
assert_eq!(
serde_json::to_string(&op).unwrap(),
r#"{"op":"replace","path":"/counter","value":2}"#
);Variants§
Add
Inserts value at path, shifting array elements right when path
ends in an array index or -.
Fields
Remove
Removes the value at path.
Replace
Replaces the value at path, which must already exist.
Fields
Move
Moves the value at from to path.
Fields
Copy
Copies the value at from to path.
Fields
Test
Asserts that the value at path equals value; a failed test aborts
the whole patch.
Implementations§
Trait Implementations§
Source§impl Clone for PatchOperation
impl Clone for PatchOperation
Source§fn clone(&self) -> PatchOperation
fn clone(&self) -> PatchOperation
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl ComposeSchema for PatchOperation
impl ComposeSchema for PatchOperation
Source§impl Debug for PatchOperation
impl Debug for PatchOperation
Source§impl<'de> Deserialize<'de> for PatchOperation
impl<'de> Deserialize<'de> for PatchOperation
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl JsonSchema for PatchOperation
impl JsonSchema for PatchOperation
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read moreSource§impl PartialEq for PatchOperation
impl PartialEq for PatchOperation
Source§fn eq(&self, other: &PatchOperation) -> bool
fn eq(&self, other: &PatchOperation) -> bool
self and other values to be equal, and is used by ==.