Mutation

Struct Mutation 

Source
pub struct Mutation<A: Adapter> {
    pub path_rev: Vec<Cow<'static, str>>,
    pub operation: MutationKind<A>,
}
Expand description

A mutation representing a change to a value at a specific path.

Mutation captures both the location where a change occurred (via path_rev) and the kind of change that was made (via operation). Mutations can be applied to values to reproduce the changes they represent.

§Path Representation

The path is stored in reverse order for efficiency during collection. For example, a change at foo.bar.baz would have path_rev = ["baz", "bar", "foo"].

§Example

use morphix::{JsonAdapter, Mutation, MutationKind};
use serde_json::json;

// A mutation that replaces the value at path "user.name"
let mutation = Mutation::<JsonAdapter> {
    path_rev: vec!["name".into(), "user".into()],
    operation: MutationKind::Replace(json!("Alice")),
};

// Apply the mutation to a JSON value
let mut data = json!({"user": {"name": "Bob", "age": 30}});
mutation.apply(&mut data).unwrap();
assert_eq!(data, json!({"user": {"name": "Alice", "age": 30}}));

Fields§

§path_rev: Vec<Cow<'static, str>>

The path to the mutated value, stored in reverse order.

An empty vec indicates a mutation at the root level.

§operation: MutationKind<A>

The kind of mutation that occurred.

Implementations§

Source§

impl<A: Adapter> Mutation<A>

Source

pub fn apply(self, value: &mut A::Value) -> Result<(), MutationError>

Applies this mutation to a value.

§Arguments
  • value - value to mutate
§Errors
  • Returns IndexError if the path doesn’t exist in the value.
  • Returns OperationError if the mutation cannot be performed.
§Example
use morphix::{Mutation, MutationKind, JsonAdapter};
use serde_json::json;

let mut value = json!({"count": 0});

Mutation::<JsonAdapter> {
    path_rev: vec!["count".into()],
    operation: MutationKind::Replace(json!(42)),
}
.apply(&mut value)
.unwrap();

assert_eq!(value, json!({"count": 42}));

Trait Implementations§

Source§

impl<A: Adapter> Clone for Mutation<A>
where A::Value: Clone,

Source§

fn clone(&self) -> Self

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<A: Adapter> Debug for Mutation<A>
where A::Value: Debug,

Source§

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

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

impl<A: Adapter> PartialEq for Mutation<A>
where A::Value: PartialEq,

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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<A: Adapter> Eq for Mutation<A>
where A::Value: Eq,

Auto Trait Implementations§

§

impl<A> Freeze for Mutation<A>
where <A as Adapter>::Value: Freeze,

§

impl<A> RefUnwindSafe for Mutation<A>
where <A as Adapter>::Value: RefUnwindSafe,

§

impl<A> Send for Mutation<A>
where <A as Adapter>::Value: Send,

§

impl<A> Sync for Mutation<A>
where <A as Adapter>::Value: Sync,

§

impl<A> Unpin for Mutation<A>
where <A as Adapter>::Value: Unpin,

§

impl<A> UnwindSafe for Mutation<A>
where <A as Adapter>::Value: 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> 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.