attuned_store/
traits.rs

1//! Storage trait definitions.
2
3use crate::error::StoreError;
4use async_trait::async_trait;
5use attuned_core::StateSnapshot;
6
7/// Trait for storing and retrieving user state snapshots.
8///
9/// Implementations must be thread-safe (`Send + Sync`) and support
10/// async operations.
11#[async_trait]
12pub trait StateStore: Send + Sync {
13    /// Insert or update the latest state snapshot for a user.
14    ///
15    /// If a snapshot already exists for the user, it is replaced.
16    /// The snapshot is validated before storage.
17    async fn upsert_latest(&self, snapshot: StateSnapshot) -> Result<(), StoreError>;
18
19    /// Get the latest state snapshot for a user.
20    ///
21    /// Returns `None` if no state exists for the user.
22    async fn get_latest(&self, user_id: &str) -> Result<Option<StateSnapshot>, StoreError>;
23
24    /// Delete the state for a user.
25    ///
26    /// This removes all state data for the user (GDPR compliance).
27    /// Returns `Ok(())` even if the user did not exist.
28    async fn delete(&self, user_id: &str) -> Result<(), StoreError> {
29        // Default implementation - stores should override if they need custom logic
30        let _ = user_id;
31        Ok(())
32    }
33
34    /// Get historical snapshots for a user.
35    ///
36    /// Returns up to `limit` snapshots, ordered by most recent first.
37    /// Default implementation returns empty vec (history not supported).
38    async fn get_history(
39        &self,
40        user_id: &str,
41        limit: usize,
42    ) -> Result<Vec<StateSnapshot>, StoreError> {
43        let _ = (user_id, limit);
44        Ok(vec![])
45    }
46
47    /// Check if the store is healthy and can accept requests.
48    ///
49    /// Default implementation always returns true.
50    async fn health_check(&self) -> Result<bool, StoreError> {
51        Ok(true)
52    }
53}