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 representReplaceandAppendvalues.Error: Error type for serialization / deserialization operations.
Required Associated Types§
Required Methods§
Sourcefn from_mutation(mutation: Option<Mutation<Self::Value>>) -> Self
fn from_mutation(mutation: Option<Mutation<Self::Value>>) -> Self
Constructs the adapter from an optional mutation.
Sourcefn serialize_value<T: Serialize + ?Sized>(
value: &T,
) -> Result<Self::Value, Self::Error>
fn serialize_value<T: Serialize + ?Sized>( value: &T, ) -> Result<Self::Value, Self::Error>
Serializes a value into the adapter’s Value type.
Sourcefn get_mut<'a>(
value: &'a mut Self::Value,
segment: &PathSegment,
allow_create: bool,
) -> Option<&'a mut Self::Value>
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 intosegment: The path segment indicating which nested value to accessallow_create: Iftrueand 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 valueNone: If the operation is not supported on this value type, or if the segment refers to a non-existent location andallow_createisfalse
Sourcefn append(value: &mut Self::Value, append_value: Self::Value) -> Option<usize>
Available on crate feature append only.
fn append(value: &mut Self::Value, append_value: Self::Value) -> Option<usize>
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 toappend_value: The value to be appended
§Returns
Some(append_len): The length of the appended portionNone: If the operation is not supported (e.g., incompatible types betweenvalueandappend_value, orvalueis not an appendable type)
§Note
For strings, the returned length represents the char count, not the byte length.
Sourcefn len(value: &Self::Value) -> Option<usize>
Available on crate feature append only.
fn len(value: &Self::Value) -> Option<usize>
append only.Sourcefn truncate(value: &mut Self::Value, truncate_len: usize) -> Option<usize>
Available on crate feature truncate only.
fn truncate(value: &mut Self::Value, truncate_len: usize) -> Option<usize>
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 truncatetruncate_len: The number of elements to remove from the end
§Returns
Some(remaining): The remaining truncation length that could not be applied. Returns0if the full truncation was successful. Iftruncate_lenexceeds the actual length, returnstruncate_len - actual_lenand 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§
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.