meerkat_core/
session_store.rs1use async_trait::async_trait;
7
8use crate::Session;
9use crate::session::SessionMeta;
10use crate::time_compat::SystemTime;
11use crate::types::SessionId;
12
13#[derive(Debug, Clone, Default)]
15pub struct SessionFilter {
16 pub created_after: Option<SystemTime>,
18 pub updated_after: Option<SystemTime>,
20 pub limit: Option<usize>,
22 pub offset: Option<usize>,
24}
25
26#[derive(Debug, thiserror::Error)]
31pub enum SessionStoreError {
32 #[error("IO error: {0}")]
33 Io(#[from] std::io::Error),
34
35 #[error("Serialization error: {0}")]
36 Serialization(String),
37
38 #[error("Session not found: {0}")]
39 NotFound(SessionId),
40
41 #[error("Session corrupted: {0}")]
42 Corrupted(SessionId),
43
44 #[error("Internal error: {0}")]
45 Internal(String),
46}
47
48impl From<serde_json::Error> for SessionStoreError {
49 fn from(e: serde_json::Error) -> Self {
50 Self::Serialization(e.to_string())
51 }
52}
53
54#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
59#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
60pub trait SessionStore: Send + Sync {
61 async fn save(&self, session: &Session) -> Result<(), SessionStoreError>;
63
64 async fn load(&self, id: &SessionId) -> Result<Option<Session>, SessionStoreError>;
66
67 async fn list(&self, filter: SessionFilter) -> Result<Vec<SessionMeta>, SessionStoreError>;
69
70 async fn delete(&self, id: &SessionId) -> Result<(), SessionStoreError>;
72
73 async fn exists(&self, id: &SessionId) -> Result<bool, SessionStoreError> {
75 Ok(self.load(id).await?.is_some())
76 }
77}