Skip to main content

attune_core/
backend.rs

1use crossbeam_channel::Receiver;
2use std::collections::HashMap;
3
4use crate::{BackendError, StoredValue};
5
6pub trait StorageBackend: Send + Sync {
7    /// Load all persisted settings into memory. This method is called at load and on
8    /// watcher ticks.
9    fn load_all(&self) -> Result<HashMap<String, StoredValue>, BackendError>;
10
11    /// Persist a setting to the database.
12    ///
13    /// ### Arguments
14    /// - **key** (&str): The identifier of the setting to persist.
15    /// - **value** (&StoredValue): The value associated with the setting's identifier.
16    fn set(&self, key: &str, value: &StoredValue) -> Result<(), BackendError>;
17
18    /// Remove a setting from the database.
19    /// ### Arguments
20    /// - **key** (&str): The identifier of the setting to delete.
21    fn delete(&self, key: &str) -> Result<(), BackendError>;
22
23    /// Subscribe to the backend storage event stream. Default returns `None`.
24    /// The returned receiver fires for every change, including own-process writes.
25    fn watch_changes(&self) -> Option<Receiver<()>> {
26        None
27    }
28}
29
30#[derive(Debug, Default)]
31pub struct NoopBackend;
32
33impl StorageBackend for NoopBackend {
34    fn load_all(&self) -> Result<HashMap<String, StoredValue>, BackendError> {
35        Ok(HashMap::new())
36    }
37
38    fn set(&self, _key: &str, _value: &StoredValue) -> Result<(), BackendError> {
39        Ok(())
40    }
41
42    fn delete(&self, _key: &str) -> Result<(), BackendError> {
43        Ok(())
44    }
45}