pub struct Patch<T> { /* private fields */ }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>
impl<T> Patch<T>
Sourcepub fn from_value(body: Value) -> Self
pub fn from_value(body: Value) -> Self
Wraps a JSON value as a patch.
Sourcepub fn from_partial<P: Serialize>(value: &P) -> Result<Self, Error>
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.
Sourcepub fn into_value(self) -> Value
pub fn into_value(self) -> Value
Consumes the patch and yields the JSON value.
Sourcepub fn touches(&self, field: &str) -> bool
pub fn touches(&self, field: &str) -> bool
Whether the patch writes field at the top level, including writing it to null.
Sourcepub fn last_updated(&self) -> Option<DateTime>
pub fn last_updated(&self) -> Option<DateTime>
The last_updated the patch carries, if any.
Sourcepub fn retype<U>(self) -> Patch<U>
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§impl<T> Patch<T>
impl<T> Patch<T>
Sourcepub fn apply(&self, target: &T) -> Result<T, OcpiError>
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_updatedis refused with2001, 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.