Skip to main content

PatchBuilder

Struct PatchBuilder 

Source
pub struct PatchBuilder { /* private fields */ }
Expand description

A builder for constructing JSON Patches programmatically.

This provides a more ergonomic way to create patches when you know exactly what operations you want to perform.

§Example

use ag_ui_core::patch::PatchBuilder;
use serde_json::json;

let patch = PatchBuilder::new()
    .add("/name", json!("Alice"))
    .replace("/age", json!(31))
    .remove("/temp")
    .build();

assert_eq!(patch.0.len(), 3);

Implementations§

Source§

impl PatchBuilder

Source

pub fn new() -> Self

Creates a new empty patch builder.

Source

pub fn add(self, path: impl AsRef<str>, value: JsonValue) -> Self

Adds an “add” operation to the patch.

The add operation adds a value at the target location. If the target location specifies an array index, the value is inserted at that index.

Source

pub fn remove(self, path: impl AsRef<str>) -> Self

Adds a “remove” operation to the patch.

The remove operation removes the value at the target location.

Source

pub fn replace(self, path: impl AsRef<str>, value: JsonValue) -> Self

Adds a “replace” operation to the patch.

The replace operation replaces the value at the target location with the new value.

Source

pub fn move_value(self, from: impl AsRef<str>, path: impl AsRef<str>) -> Self

Adds a “move” operation to the patch.

The move operation removes the value at a specified location and adds it to the target location.

Source

pub fn copy(self, from: impl AsRef<str>, path: impl AsRef<str>) -> Self

Adds a “copy” operation to the patch.

The copy operation copies the value at a specified location to the target location.

Source

pub fn test(self, path: impl AsRef<str>, value: JsonValue) -> Self

Adds a “test” operation to the patch.

The test operation tests that a value at the target location is equal to a specified value. If the test fails, the entire patch fails.

Source

pub fn build(self) -> Patch

Builds the patch from the accumulated operations.

Source

pub fn build_vec(self) -> Vec<JsonValue>

Builds the patch and returns it as a vector of JSON values.

This is the format expected by StateDeltaEvent and ActivityDeltaEvent.

Trait Implementations§

Source§

impl Clone for PatchBuilder

Source§

fn clone(&self) -> PatchBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for PatchBuilder

Source§

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

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

impl Default for PatchBuilder

Source§

fn default() -> PatchBuilder

Returns the “default value” for a 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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