grammers_session/storages/
memory.rs1use std::fmt;
10use std::sync::{Mutex, MutexGuard};
11
12use crate::types::{ChannelState, DcOption, PeerId, PeerInfo, UpdateState, UpdatesState};
13use crate::{BoxFuture, Session, SessionData};
14
15#[derive(Default)]
23pub struct MemorySession(Mutex<SessionData>);
24
25impl From<SessionData> for MemorySession {
26 fn from(session_data: SessionData) -> Self {
29 Self(Mutex::new(session_data))
30 }
31}
32
33#[derive(Debug)]
34pub enum MemorySessionError {
35 Poisoned,
36}
37
38impl std::error::Error for MemorySessionError {}
39
40impl fmt::Display for MemorySessionError {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 match self {
43 MemorySessionError::Poisoned => write!(f, "session lock is poisoned"),
44 }
45 }
46}
47
48impl MemorySession {
49 fn data(&self) -> Result<MutexGuard<'_, SessionData>, MemorySessionError> {
51 self.0.lock().map_err(|_| MemorySessionError::Poisoned)
52 }
53}
54
55impl Session for MemorySession {
56 type Error = MemorySessionError;
57
58 fn home_dc_id(&self) -> Result<i32, MemorySessionError> {
59 Ok(self.data()?.home_dc)
60 }
61
62 fn set_home_dc_id(&self, dc_id: i32) -> BoxFuture<'_, Result<(), MemorySessionError>> {
63 Box::pin(async move {
64 self.data()?.home_dc = dc_id;
65 Ok(())
66 })
67 }
68
69 fn dc_option(&self, dc_id: i32) -> Result<Option<DcOption>, MemorySessionError> {
70 Ok(self.data()?.dc_options.get(&dc_id).cloned())
71 }
72
73 fn set_dc_option(&self, dc_option: &DcOption) -> BoxFuture<'_, Result<(), MemorySessionError>> {
74 let dc_option = dc_option.clone();
75 Box::pin(async move {
76 self.data()?
77 .dc_options
78 .insert(dc_option.id, dc_option.clone());
79 Ok(())
80 })
81 }
82
83 fn peer(&self, peer: PeerId) -> BoxFuture<'_, Result<Option<PeerInfo>, MemorySessionError>> {
84 Box::pin(async move { Ok(self.data()?.peer_infos.get(&peer).cloned()) })
85 }
86
87 fn cache_peer(&self, peer: PeerInfo) -> BoxFuture<'_, Result<(), MemorySessionError>> {
88 Box::pin(async move {
89 self.data()?
90 .peer_infos
91 .entry(peer.id())
92 .or_insert_with(|| peer.clone())
93 .extend_info(&peer);
94 Ok(())
95 })
96 }
97
98 fn updates_state(&self) -> BoxFuture<'_, Result<UpdatesState, MemorySessionError>> {
99 Box::pin(async move { Ok(self.data()?.updates_state.clone()) })
100 }
101
102 fn set_update_state(
103 &self,
104 update: UpdateState,
105 ) -> BoxFuture<'_, Result<(), MemorySessionError>> {
106 Box::pin(async move {
107 let mut data = self.data()?;
108
109 match update {
110 UpdateState::All(updates_state) => {
111 data.updates_state = updates_state;
112 }
113 UpdateState::Primary { pts, date, seq } => {
114 data.updates_state.pts = pts;
115 data.updates_state.date = date;
116 data.updates_state.seq = seq;
117 }
118 UpdateState::Secondary { qts } => {
119 data.updates_state.qts = qts;
120 }
121 UpdateState::Channel { id, pts } => {
122 data.updates_state.channels.retain(|c| c.id != id);
123 data.updates_state.channels.push(ChannelState { id, pts });
124 }
125 }
126
127 Ok(())
128 })
129 }
130}