Skip to main content

Patch

Struct Patch 

Source
pub struct Patch<T> { /* private fields */ }
Available on crate feature transport only.
Expand description

A partial update to an OCPI object.

Held as a JSON object rather than as a per-field Option struct, because that is what a merge patch is: the difference between “absent” and “present and null” is the whole semantics, and a struct of Options cannot express it.

use ocpi_kit::transport::Patch;
use ocpi_kit::v2_3_0::locations::Evse;

let patch: Patch<Evse> = serde_json::from_str(
    r#"{"status":"CHARGING","last_updated":"2019-06-24T12:39:09Z"}"#,
).unwrap();
assert!(patch.last_updated().is_some());
assert!(patch.touches("status"));

Spec: 2.3.0 §transport_and_format_patch

Implementations§

Source§

impl<T> Patch<T>

Source

pub fn from_value(body: Value) -> Self

Wraps a JSON value as a patch.

Source

pub fn from_partial<P: Serialize>(value: &P) -> Result<Self, Error>

Builds a patch by serialising value, keeping only the fields it writes.

§Errors

Propagates the serde_json error if value cannot be serialised.

Source

pub const fn as_value(&self) -> &Value

The patch as a JSON value.

Source

pub fn into_value(self) -> Value

Consumes the patch and yields the JSON value.

Source

pub fn touches(&self, field: &str) -> bool

Whether the patch writes field at the top level, including writing it to null.

Source

pub fn last_updated(&self) -> Option<DateTime>

The last_updated the patch carries, if any.

Source

pub fn retype<U>(self) -> Patch<U>

Re-types this patch to the object it is meant to be applied to.

A merge patch is untyped on the wire, so a server extractor produces a Patch<serde_json::Value>; this names the type it will be applied to, which is what Patch::apply needs in order to check the result.

Source

pub fn fields(&self) -> Vec<&str>

The fields the patch writes, at the top level.

Source§

impl<T> Patch<T>

Source

pub fn apply(&self, target: &T) -> Result<T, OcpiError>

Applies this patch to target, returning the updated object.

The whole operation is checked before anything is returned:

  • a patch without last_updated is refused with 2001, as the spec’s own example says;
  • the merged object must still deserialise into T, so a patch that removes a required field is refused rather than producing a half-object;
  • the merged object must still satisfy Validate, so a patch cannot turn a conformant object into a non-conformant one.
§Errors

Returns OcpiError::Decode with a 2001-shaped message when any of those fail.

Trait Implementations§

Source§

impl<T: Clone> Clone for Patch<T>

Source§

fn clone(&self) -> Patch<T>

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<T: Debug> Debug for Patch<T>

Source§

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

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

impl<'de, T> Deserialize<'de> for Patch<T>

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<T> Display for Patch<T>

Source§

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

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

impl<T: PartialEq> PartialEq for Patch<T>

Source§

fn eq(&self, other: &Patch<T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T> Serialize for Patch<T>

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<T: PartialEq> StructuralPartialEq for Patch<T>

Auto Trait Implementations§

§

impl<T> Freeze for Patch<T>
where PhantomData<fn() -> T>: Freeze,

§

impl<T> RefUnwindSafe for Patch<T>
where PhantomData<fn() -> T>: RefUnwindSafe,

§

impl<T> Send for Patch<T>
where PhantomData<fn() -> T>: Send,

§

impl<T> Sync for Patch<T>
where PhantomData<fn() -> T>: Sync,

§

impl<T> Unpin for Patch<T>
where PhantomData<fn() -> T>: Unpin,

§

impl<T> UnsafeUnpin for Patch<T>
where PhantomData<fn() -> T>: UnsafeUnpin,

§

impl<T> UnwindSafe for Patch<T>
where PhantomData<fn() -> T>: UnwindSafe,

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.

Source§

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

Source§

fn from_ref(input: &T) -> T

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

impl<T> Instrument for T

Source§

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

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

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.

Source§

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

Source§

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
Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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