perstruct 0.2.0

Utility macro for adding persistent backing to a struct
Documentation
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]

#[doc(hidden)]
pub use ::perstruct_proc_macros::perstruct;

/// Result of loading a struct from a key-value store.
/// Contains the deserialized struct, any deserialization errors, and unknown fields.
#[derive(Debug)]
pub struct LoadResult<T> {
    /// The loaded struct instance
    pub value: T,
    /// Fields that failed to deserialize, with field name and error message
    pub deserialization_errors: Vec<(&'static str, String)>,
    /// Keys in the store that didn't match any struct fields
    pub unknown_fields: Vec<String>,
}

/// Trait defining persistence behavior for structs.
/// This trait is automatically implemented by the `perstruct` macro.
pub trait Perstruct: Sized {
    /// Load an instance from a key-value map.
    /// The map should contain serialized field values as strings, keyed by field names or custom keys.
    fn from_map(map: &std::collections::HashMap<&str, &str>) -> LoadResult<Self>;

    /// Get all available persistence keys for this struct
    fn keys() -> Vec<&'static str>
    where
        Self: Sized;

    /// Get all dirty fields (fields that have been modified since last save)
    fn changed_keys(&self) -> &std::collections::HashSet<&'static str>;

    /// Mark specific keys as dirty
    fn mark_keys_changed(&mut self, keys: &[&'static str]);

    /// Get all changes made to the struct since last save.
    /// Returns a vector of (key, serialized_value) pairs for modified fields.
    fn serialize_changes(&self) -> Result<Vec<(&'static str, String)>, String>;

    /// Mark all current changes as saved.
    /// This resets the change tracking state.
    fn clear_changes(&mut self);
}