Adapter

Trait Adapter 

Source
pub trait Adapter: Sized {
    type Value;
    type Error;

    // Required methods
    fn from_mutation(mutation: Option<Mutation<Self::Value>>) -> Self;
    fn serialize_value<T: Serialize + ?Sized>(
        value: &T,
    ) -> Result<Self::Value, Self::Error>;
    fn get_mut<'a>(
        value: &'a mut Self::Value,
        segment: &PathSegment,
        allow_create: bool,
    ) -> Option<&'a mut Self::Value>;
    fn append(
        value: &mut Self::Value,
        append_value: Self::Value,
    ) -> Option<usize>;
    fn len(value: &Self::Value) -> Option<usize>;
    fn truncate(value: &mut Self::Value, truncate_len: usize) -> Option<usize>;

    // Provided method
    fn mutate(
        value: &mut Self::Value,
        mutation: Mutation<Self::Value>,
        path_stack: &mut Path<false>,
    ) -> Result<(), MutationError> { ... }
}
Expand description

Trait for adapting mutations to different serialization formats.

The Adapter trait provides an abstraction layer between the mutation detection system and the serialization format. This allows morphix to support multiple output formats while maintaining type safety.

§Type Parameters

  • Value: Type used to represent Replace and Append values.
  • Error: Error type for serialization / deserialization operations.

Required Associated Types§

Source

type Value

Type used to represent Replace and Append values.

Source

type Error

Error type for serialization / deserialization operations.

Required Methods§

Source

fn from_mutation(mutation: Option<Mutation<Self::Value>>) -> Self

Constructs the adapter from an optional mutation.

Source

fn serialize_value<T: Serialize + ?Sized>( value: &T, ) -> Result<Self::Value, Self::Error>

Serializes a value into the adapter’s Value type.

Source

fn get_mut<'a>( value: &'a mut Self::Value, segment: &PathSegment, allow_create: bool, ) -> Option<&'a mut Self::Value>

Gets a mutable reference to a nested value by path segment.

This method navigates into value using the provided segment and returns a mutable reference to the nested value if it exists.

§Parameters
  • value: The value to navigate into
  • segment: The path segment indicating which nested value to access
  • allow_create: If true and the segment refers to a non-existent key in an object / map, an empty value will be created at that location
§Returns
  • Some(value): A mutable reference to the nested value
  • None: If the operation is not supported on this value type, or if the segment refers to a non-existent location and allow_create is false
Source

fn append(value: &mut Self::Value, append_value: Self::Value) -> Option<usize>

Available on crate feature append only.

Appends a value to the end of another value.

This method performs an append operation similar to String::push_str or Extend::extend, merging append_value into the end of value.

§Parameters
  • value: The value to append to
  • append_value: The value to be appended
§Returns
  • Some(append_len): The length of the appended portion
  • None: If the operation is not supported (e.g., incompatible types between value and append_value, or value is not an appendable type)
§Note

For strings, the returned length represents the char count, not the byte length.

Source

fn len(value: &Self::Value) -> Option<usize>

Available on crate feature append only.

Returns the appendable length of a value.

This method returns the current length of a value that can be used with append operations.

§Returns
  • Some(len): The current length of the value
  • None: If the value is not an appendable type
§Note

For strings, the returned length represents the char count, not the byte length.

Source

fn truncate(value: &mut Self::Value, truncate_len: usize) -> Option<usize>

Available on crate feature truncate only.

Truncates a value by removing elements from the end.

This method removes up to truncate_len elements from the end of value.

§Parameters
  • value: The value to truncate
  • truncate_len: The number of elements to remove from the end
§Returns
  • Some(remaining): The remaining truncation length that could not be applied. Returns 0 if the full truncation was successful. If truncate_len exceeds the actual length, returns truncate_len - actual_len and clears the value.
  • None: If the operation is not supported on this value type
§Note

For strings, the returned length represents the char count, not the byte length.

Provided Methods§

Source

fn mutate( value: &mut Self::Value, mutation: Mutation<Self::Value>, path_stack: &mut Path<false>, ) -> Result<(), MutationError>

Applies a Mutation to an existing value.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Adapter for Json

Available on crate feature json only.
Source§

impl Adapter for Yaml

Available on crate feature yaml only.