actix_session/storage/
interface.rs1use std::{collections::HashMap, future::Future};
2
3use actix_web::cookie::time::Duration;
4use derive_more::derive::Display;
5
6use super::SessionKey;
7
8pub(crate) type SessionState = HashMap<String, String>;
9
10pub trait SessionStore {
14 fn load(
16 &self,
17 session_key: &SessionKey,
18 ) -> impl Future<Output = Result<Option<SessionState>, LoadError>>;
19
20 fn save(
24 &self,
25 session_state: SessionState,
26 ttl: &Duration,
27 ) -> impl Future<Output = Result<SessionKey, SaveError>>;
28
29 fn update(
31 &self,
32 session_key: SessionKey,
33 session_state: SessionState,
34 ttl: &Duration,
35 ) -> impl Future<Output = Result<SessionKey, UpdateError>>;
36
37 fn update_ttl(
39 &self,
40 session_key: &SessionKey,
41 ttl: &Duration,
42 ) -> impl Future<Output = Result<(), anyhow::Error>>;
43
44 fn delete(&self, session_key: &SessionKey) -> impl Future<Output = Result<(), anyhow::Error>>;
46}
47
48#[derive(Debug, Display)]
54pub enum LoadError {
55 #[display("Failed to deserialize session state")]
57 Deserialization(anyhow::Error),
58
59 #[display("Something went wrong when retrieving the session state")]
61 Other(anyhow::Error),
62}
63
64impl std::error::Error for LoadError {
65 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
66 match self {
67 Self::Deserialization(err) => Some(err.as_ref()),
68 Self::Other(err) => Some(err.as_ref()),
69 }
70 }
71}
72
73#[derive(Debug, Display)]
75pub enum SaveError {
76 #[display("Failed to serialize session state")]
78 Serialization(anyhow::Error),
79
80 #[display("Something went wrong when persisting the session state")]
82 Other(anyhow::Error),
83}
84
85impl std::error::Error for SaveError {
86 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
87 match self {
88 Self::Serialization(err) => Some(err.as_ref()),
89 Self::Other(err) => Some(err.as_ref()),
90 }
91 }
92}
93
94#[derive(Debug, Display)]
95pub enum UpdateError {
97 #[display("Failed to serialize session state")]
99 Serialization(anyhow::Error),
100
101 #[display("Something went wrong when updating the session state.")]
103 Other(anyhow::Error),
104}
105
106impl std::error::Error for UpdateError {
107 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
108 match self {
109 Self::Serialization(err) => Some(err.as_ref()),
110 Self::Other(err) => Some(err.as_ref()),
111 }
112 }
113}