Skip to main content

PatchOperation

Enum PatchOperation 

Source
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

§path: String

JSON Pointer to the location to add.

§value: Value

The value to insert. An omitted value reads as null; see the type-level docs.

§

Remove

Removes the value at path.

Fields

§path: String

JSON Pointer to the location to remove.

§

Replace

Replaces the value at path, which must already exist.

Fields

§path: String

JSON Pointer to the location to overwrite.

§value: Value

The replacement value. An omitted value reads as null.

§

Move

Moves the value at from to path.

Fields

§from: String

JSON Pointer to the source location.

§path: String

JSON Pointer to the destination.

§

Copy

Copies the value at from to path.

Fields

§from: String

JSON Pointer to the source location.

§path: String

JSON Pointer to the destination.

§

Test

Asserts that the value at path equals value; a failed test aborts the whole patch.

Fields

§path: String

JSON Pointer to the location to test.

§value: Value

The value the location is expected to hold. An omitted value reads as null.

Implementations§

Source§

impl PatchOperation

Source

pub fn add(path: impl Into<String>, value: impl Into<Value>) -> Self

Builds an Add operation.

Source

pub fn remove(path: impl Into<String>) -> Self

Builds a Remove operation.

Source

pub fn replace(path: impl Into<String>, value: impl Into<Value>) -> Self

Builds a Replace operation.

Source

pub fn mv(from: impl Into<String>, path: impl Into<String>) -> Self

Builds a Move operation.

Source

pub fn copy(from: impl Into<String>, path: impl Into<String>) -> Self

Builds a Copy operation.

Source

pub fn test(path: impl Into<String>, value: impl Into<Value>) -> Self

Builds a Test operation.

Source

pub const fn op(&self) -> &'static str

The op string as it appears on the wire.

Source

pub fn path(&self) -> &str

The JSON Pointer this operation targets.

Source

pub fn from(&self) -> Option<&str>

The source pointer of a move or copy, or None for other operations.

Source

pub const fn value(&self) -> Option<&Value>

The payload of an add, replace or test, or None for other operations.

Trait Implementations§

Source§

impl Clone for PatchOperation

Source§

fn clone(&self) -> PatchOperation

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl ComposeSchema for PatchOperation

Source§

fn compose(generics: Vec<RefOr<Schema>>) -> RefOr<Schema>

Source§

impl Debug for PatchOperation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for PatchOperation

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl JsonSchema for PatchOperation

Source§

fn schema_name() -> Cow<'static, str>

The name of the generated JSON Schema. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

fn json_schema(generator: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

fn inline_schema() -> bool

Whether JSON Schemas generated for this type should be included directly in parent schemas, rather than being re-used where possible using the $ref keyword. Read more
Source§

impl PartialEq for PatchOperation

Source§

fn eq(&self, other: &PatchOperation) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for PatchOperation

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for PatchOperation

Source§

impl ToSchema for PatchOperation

Source§

fn name() -> Cow<'static, str>

Return name of the schema. Read more
Source§

fn schemas(schemas: &mut Vec<(String, RefOr<Schema>)>)

Implement reference [utoipa::openapi::schema::Schema]s for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PartialSchema for T
where T: ComposeSchema + ?Sized,

§

fn schema() -> RefOr<Schema>

Return ref or schema of implementing type that can then be used to construct combined schemas.
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more