author_web/user/
mod.rs

1use crate::session::store::in_memory::InMemorySessionData;
2use crate::session::store::SessionDataValueStorage;
3use async_trait::async_trait;
4use std::sync::Arc;
5
6#[async_trait]
7pub trait UserSession {
8    type User;
9
10    async fn set_user(&self, user: Self::User) -> anyhow::Result<()>;
11    async fn unset_user(&self) -> anyhow::Result<()>;
12    async fn current_user(&self) -> anyhow::Result<Option<Self::User>>;
13}
14
15#[cfg(feature = "in-memory")]
16#[async_trait]
17impl<U> UserSession for InMemorySessionData<String, U>
18where
19    U: Clone + Send,
20{
21    type User = U;
22
23    async fn set_user(&self, user: U) -> anyhow::Result<()> {
24        Ok(self.set_value("current_user", user).await?)
25    }
26
27    async fn unset_user(&self) -> anyhow::Result<()> {
28        Ok(self.unset_value("current_user").await?)
29    }
30
31    async fn current_user(&self) -> anyhow::Result<Option<Self::User>> {
32        Ok(self.get_value("current_user").await?)
33    }
34}
35
36#[async_trait]
37impl<U, Sess> UserSession for Arc<Sess>
38where
39    Sess: UserSession<User = U> + Send + Sync,
40    U: Clone + Send + 'static,
41{
42    type User = U;
43
44    async fn set_user(&self, user: U) -> anyhow::Result<()> {
45        Ok((&*self as &Sess).set_user(user).await?)
46    }
47
48    async fn unset_user(&self) -> anyhow::Result<()> {
49        Ok((&*self as &Sess).unset_user().await?)
50    }
51
52    async fn current_user(&self) -> anyhow::Result<Option<Self::User>> {
53        Ok((&*self as &Sess).current_user().await?)
54    }
55}