Crate morphix

Crate morphix 

Source
Expand description

§morphix

Crates.io Documentation

A Rust library for observing and serializing changes.

§Installation

Add this to your Cargo.toml:

[dependencies]
morphix = { version = "0.1", features = ["json"] }

§Basic Usage

use serde::Serialize;
use serde_json::json;
use morphix::{Change, JsonAdapter, Observe, Operation, observe};

// 1. Define any data structure with `#[derive(Observe)]`.
#[derive(Serialize, PartialEq, Debug, Observe)]
struct Foo {
    pub bar: Bar,
    pub qux: String,
}

#[derive(Serialize, PartialEq, Debug, Observe)]
struct Bar {
    pub baz: i32,
}

let mut foo = Foo {
    bar: Bar { baz: 42 },
    qux: "hello".to_string(),
};

// 2. Use `observe!` to mutate data and track changes.
let change = observe!(JsonAdapter, |mut foo| {
    foo.bar.baz += 1;
    foo.qux.push(' ');
    foo.qux += "world";
}).unwrap();

// 3. Inspect the changes.
assert_eq!(
    change,
    Some(Change {
        path_rev: vec![],
        operation: Operation::Batch(vec![
            Change {
                path_rev: vec!["baz".into(), "bar".into()],
                operation: Operation::Replace(json!(43)),
            },
            Change {
                path_rev: vec!["qux".into()],
                operation: Operation::Append(json!(" world")),
            },
        ]),
    }),
);

// 4. The original data structure is also mutated.
assert_eq!(
    foo,
    Foo {
        bar: Bar { baz: 43 },
        qux: "hello world".to_string(),
    },
);

§Change Types

morphix recognizes three types of changes:

§Replace

The most general change type, used for any mutation that replaces a value:

person.age = 35;                        // Replace at .age
person.name = "Bob".into();             // Replace at .name

§Append

Optimized for appending to strings and vectors:

person.name.push_str(" Smith");         // Append to .name
person.hobbies.push("gaming".into());   // Append to .hobbies

§Batch

Multiple changes combined into a single operation.

§Features

  • derive (default): Enables the Observe derive macro
  • json: Includes JSON serialization support via serde_json
  • yaml: Includes YAML serialization support via serde_yml

Macros§

observe
Observe mutations within a closure and collect changes.

Structs§

Batch
A batch collector for aggregating and optimizing multiple changes.
Change
A change in JSON format.
ShallowObserver
A generic observer that only tracks complete replacements.

Enums§

ChangeError
Error types for mutation operations.
Operation
A change in JSON format.

Traits§

Adapter
Trait for adapting changes to different serialization formats.
Observe
A trait for types that can be observed for changes.
Observer
A trait for observer types that wrap and track mutations to values.

Derive Macros§

Observe
Derive the Observe trait for structs to enable change tracking.