Skip to main content

fluers_runtime/
persistence.rs

1//! Persistence adapter contract.
2//!
3//! Lives in `fluers-runtime` (not `fluers-postgres`) so that [`SessionStore`]
4//! can be generic over it. A concrete backend (`fluers-postgres`) implements
5//! [`PersistenceAdapter`]; a JSON-file adapter ships in MVP 2 so sessions can
6//! resume after a process restart without requiring Postgres.
7//!
8//! [`SessionStore`]: crate::session::SessionStore
9
10use async_trait::async_trait;
11use serde_json::Value;
12
13/// The persistence contract for session state.
14///
15/// The in-memory [`SessionStore`](crate::session::SessionStore) swaps to an
16/// impl of this trait when persistence is configured.
17#[async_trait]
18pub trait PersistenceAdapter: Send + Sync {
19    /// Persist a session's serialized state.
20    async fn save_session(&self, id: &str, data: &Value) -> Result<()>;
21
22    /// Load a session's serialized state.
23    async fn load_session(&self, id: &str) -> Result<Option<Value>>;
24
25    /// List all known session ids (for discovery / `--resume`).
26    async fn list_sessions(&self) -> Result<Vec<String>>;
27}
28
29/// Errors from the persistence layer.
30#[derive(Debug, thiserror::Error)]
31pub enum PersistenceError {
32    /// A database / I/O error.
33    #[error("persistence error: {0}")]
34    Backend(String),
35}
36
37/// Result alias for persistence operations.
38pub type Result<T> = std::result::Result<T, PersistenceError>;