Observe

Derive Macro Observe 

Source
#[derive(Observe)]
{
    // Attributes available to this derive:
    #[observe]
}
Expand description

Derive the Observe trait for structs to enable mutation tracking.

This macro generates an observer type that wraps your struct and tracks mutations to its fields. The generated observer provides field-level mutation detection with support for nested structures.

§Requirements

  • The struct must also derive or implement Serialize
  • Only named structs are supported (not tuple structs or enums)

§Field Attributes

You can customize how individual fields are observed using the #[observe(...)] attribute:

  • #[observe(ignore)] - Field mutations will not be tracked
  • #[observe(shallow)] - Field will use ShallowObserver
  • #[observe(snapshot)] - Field will use SnapshotObserver

§Example

use serde::Serialize;
use morphix::Observe;

#[derive(Serialize, Observe)]
struct User {
    name: String,         // StringObserver
    age: i32,             // SnapshotObserver<i32>

    #[observe(ignore)]
    cache: String,        // Not tracked

    #[observe(shallow)]
    metadata: Metadata,   // ShallowObserver<Metadata>
}

#[derive(Serialize)]
struct Metadata {
    created_at: String,
    updated_at: String,
}