attune-core 0.1.0

Core traits and types for attune: runtime-mutable, persisted, observable configuration.
Documentation
use crossbeam_channel::Receiver;
use std::collections::HashMap;

use crate::{BackendError, StoredValue};

pub trait StorageBackend: Send + Sync {
    /// Load all persisted settings into memory. This method is called at load and on
    /// watcher ticks.
    fn load_all(&self) -> Result<HashMap<String, StoredValue>, BackendError>;

    /// Persist a setting to the database.
    ///
    /// ### Arguments
    /// - **key** (&str): The identifier of the setting to persist.
    /// - **value** (&StoredValue): The value associated with the setting's identifier.
    fn set(&self, key: &str, value: &StoredValue) -> Result<(), BackendError>;

    /// Remove a setting from the database.
    /// ### Arguments
    /// - **key** (&str): The identifier of the setting to delete.
    fn delete(&self, key: &str) -> Result<(), BackendError>;

    /// Subscribe to the backend storage event stream. Default returns `None`.
    /// The returned receiver fires for every change, including own-process writes.
    fn watch_changes(&self) -> Option<Receiver<()>> {
        None
    }
}

#[derive(Debug, Default)]
pub struct NoopBackend;

impl StorageBackend for NoopBackend {
    fn load_all(&self) -> Result<HashMap<String, StoredValue>, BackendError> {
        Ok(HashMap::new())
    }

    fn set(&self, _key: &str, _value: &StoredValue) -> Result<(), BackendError> {
        Ok(())
    }

    fn delete(&self, _key: &str) -> Result<(), BackendError> {
        Ok(())
    }
}