bitwarden_ipc/traits/
session_repository.rs1use std::{collections::HashMap, fmt::Debug};
2
3use tokio::sync::RwLock;
4
5use crate::endpoint::Endpoint;
6
7pub trait SessionRepository<Session>: Send + Sync + 'static {
8 type GetError: Debug + Send + Sync + 'static;
9 type SaveError: Debug + Send + Sync + 'static;
10 type RemoveError: Debug + Send + Sync + 'static;
11
12 fn get(
13 &self,
14 destination: Endpoint,
15 ) -> impl std::future::Future<Output = Result<Option<Session>, Self::GetError>>;
16 fn save(
17 &self,
18 destination: Endpoint,
19 session: Session,
20 ) -> impl std::future::Future<Output = Result<(), Self::SaveError>>;
21 fn remove(
22 &self,
23 destination: Endpoint,
24 ) -> impl std::future::Future<Output = Result<(), Self::RemoveError>>;
25}
26
27pub type InMemorySessionRepository<Session> = RwLock<HashMap<Endpoint, Session>>;
28impl<Session> SessionRepository<Session> for InMemorySessionRepository<Session>
29where
30 Session: Clone + Send + Sync + 'static,
31{
32 type GetError = ();
33 type SaveError = ();
34 type RemoveError = ();
35
36 async fn get(&self, destination: Endpoint) -> Result<Option<Session>, ()> {
37 Ok(self.read().await.get(&destination).cloned())
38 }
39
40 async fn save(&self, destination: Endpoint, session: Session) -> Result<(), ()> {
41 self.write().await.insert(destination, session);
42 Ok(())
43 }
44
45 async fn remove(&self, destination: Endpoint) -> Result<(), ()> {
46 self.write().await.remove(&destination);
47 Ok(())
48 }
49}