pub struct Mutation<A: Adapter> {
pub path: Path<true>,
pub kind: 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) 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 = ["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: vec!["user".into(), "name".into()].into(),
kind: 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: Path<true>The path to the mutated value, stored in reverse order.
An empty vec indicates a mutation at the root level.
kind: MutationKind<A>The kind of mutation that occurred.
Implementations§
Source§impl<A: Adapter> Mutation<A>
impl<A: Adapter> Mutation<A>
Sourcepub fn apply(self, value: &mut A::Value) -> Result<(), MutationError>
pub fn apply(self, value: &mut A::Value) -> Result<(), MutationError>
Applies this mutation to a value.
§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: vec!["count".into()].into(),
kind: MutationKind::Replace(json!(42)),
}
.apply(&mut value)
.unwrap();
assert_eq!(value, json!({"count": 42}));Trait Implementations§
impl<A: Adapter> Eq for Mutation<A>
Auto Trait Implementations§
impl<A> Freeze for Mutation<A>
impl<A> RefUnwindSafe for Mutation<A>
impl<A> Send for Mutation<A>
impl<A> Sync for Mutation<A>
impl<A> Unpin for Mutation<A>
impl<A> UnwindSafe for Mutation<A>
Blanket Implementations§
Source§impl<T> AsDerefCoinductive<Zero> for Twhere
T: ?Sized,
impl<T> AsDerefCoinductive<Zero> for Twhere
T: ?Sized,
Source§impl<T> AsDerefMut<Zero> for Twhere
T: ?Sized,
impl<T> AsDerefMut<Zero> for Twhere
T: ?Sized,
Source§fn as_deref_mut(&mut self) -> &mut T
fn as_deref_mut(&mut self) -> &mut T
Mutably dereferences self
N times.Source§impl<T> AsDerefMutCoinductive<Zero> for Twhere
T: ?Sized,
impl<T> AsDerefMutCoinductive<Zero> for Twhere
T: ?Sized,
Source§fn as_deref_mut_coinductive(&mut self) -> &mut T
fn as_deref_mut_coinductive(&mut self) -> &mut T
Mutably dereferences self
N times.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more