Skip to main content

matrix_ui_serializable/stores/
login_store.rs

1use std::ops::{Deref, DerefMut};
2
3use matrix_sdk::encryption::VerificationState;
4use matrix_sdk_ui::sync_service;
5use serde::{Serialize, Serializer, ser::SerializeStruct};
6
7/// Wether the user has logged in, or is in another state.
8#[derive(Debug, PartialEq, Serialize)]
9#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")]
10pub enum LoginState {
11    Initiating,
12    Restored,
13    AwaitingForHomeserver,
14    LoggedIn,
15}
16
17impl LoginState {
18    pub fn to_camel_case(&self) -> String {
19        match self {
20            LoginState::Initiating => "initiating".to_owned(),
21            LoginState::Restored => "restored".to_owned(),
22            LoginState::AwaitingForHomeserver => "awaitingForHomeserver".to_owned(),
23            LoginState::LoggedIn => "loggedIn".to_owned(),
24        }
25    }
26}
27
28/// Wether this Matrix client session has been verified or not
29#[derive(Debug, Clone)]
30pub struct FrontendVerificationState(VerificationState);
31
32impl Deref for FrontendVerificationState {
33    type Target = VerificationState;
34
35    fn deref(&self) -> &Self::Target {
36        &self.0
37    }
38}
39
40impl From<VerificationState> for FrontendVerificationState {
41    fn from(state: VerificationState) -> Self {
42        Self(state)
43    }
44}
45
46impl DerefMut for FrontendVerificationState {
47    fn deref_mut(&mut self) -> &mut Self::Target {
48        &mut self.0
49    }
50}
51
52impl FrontendVerificationState {
53    pub fn new(state: VerificationState) -> Self {
54        Self(state)
55    }
56
57    pub fn to_camel_case(&self) -> &str {
58        match self {
59            FrontendVerificationState(VerificationState::Unknown) => "unknown",
60            FrontendVerificationState(VerificationState::Verified) => "verified",
61            FrontendVerificationState(VerificationState::Unverified) => "unverified",
62        }
63    }
64}
65
66impl Serialize for FrontendVerificationState {
67    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
68    where
69        S: Serializer,
70    {
71        let mut state = serializer.serialize_struct("FrontendVerificationState", 1)?;
72
73        state.serialize_field("verificationState", &self.to_camel_case())?;
74
75        state.end()
76    }
77}
78
79/// Wether the matrix-sdk-ui Sync Service is running or not.
80pub struct FrontendSyncServiceState(sync_service::State);
81
82impl Deref for FrontendSyncServiceState {
83    type Target = sync_service::State;
84
85    fn deref(&self) -> &Self::Target {
86        &self.0
87    }
88}
89
90impl DerefMut for FrontendSyncServiceState {
91    fn deref_mut(&mut self) -> &mut Self::Target {
92        &mut self.0
93    }
94}
95
96impl FrontendSyncServiceState {
97    pub fn new(state: sync_service::State) -> Self {
98        Self(state)
99    }
100
101    pub fn to_camel_case(&self) -> &str {
102        match self {
103            FrontendSyncServiceState(sync_service::State::Error(_)) => "error",
104            FrontendSyncServiceState(sync_service::State::Idle) => "idle",
105            FrontendSyncServiceState(sync_service::State::Offline) => "offline",
106            FrontendSyncServiceState(sync_service::State::Running) => "running",
107            FrontendSyncServiceState(sync_service::State::Terminated) => "terminated",
108        }
109    }
110}
111
112impl Serialize for FrontendSyncServiceState {
113    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
114    where
115        S: Serializer,
116    {
117        let mut state = serializer.serialize_struct("FrontendSyncServiceState", 1)?;
118
119        state.serialize_field("syncServiceState", &self.to_camel_case())?;
120
121        state.end()
122    }
123}