smbcloud_model/
account.rs

1use {
2    crate::signup::GithubEmail,
3    chrono::{DateTime, Utc},
4    serde::{Deserialize, Serialize},
5    serde_repr::{Deserialize_repr, Serialize_repr},
6    std::fmt::{Display, Formatter},
7    tsync::tsync,
8};
9
10// smbcloud Users.
11#[derive(Debug, Serialize, Deserialize)]
12#[tsync]
13pub struct User {
14    pub id: i32,
15    pub email: String,
16    pub created_at: DateTime<Utc>,
17    pub updated_at: DateTime<Utc>,
18}
19
20impl Display for User {
21    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22        write!(f, "User: id: {}, email: {}", self.id, self.email)
23    }
24}
25
26#[derive(Debug, Serialize, Deserialize)]
27pub struct Status {
28    pub code: Option<i32>,
29    pub message: String,
30}
31
32#[derive(Debug, Serialize, Deserialize)]
33#[tsync]
34pub struct Data {
35    id: i32,
36    email: String,
37    created_at: String,
38}
39
40/// This is smbCloud authorization model
41/// that is currently tightly coupled with GitHub OAuth model.
42#[derive(Debug, Serialize, Deserialize)]
43pub struct SmbAuthorization {
44    pub message: String,
45    pub user: Option<User>,
46    pub user_email: Option<GithubEmail>,
47    pub user_info: Option<GithubInfo>,
48    pub error_code: Option<ErrorCode>,
49}
50
51#[derive(Debug, Serialize_repr, Deserialize_repr, PartialEq)]
52#[repr(u32)]
53#[tsync]
54pub enum ErrorCode {
55    EmailNotFound = 1000,
56    EmailUnverified = 1001,
57    PasswordNotSet = 1003,
58    GithubNotLinked = 1004,
59}
60
61impl Display for ErrorCode {
62    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
63        match self {
64            ErrorCode::EmailNotFound => write!(f, "Email not found."),
65            ErrorCode::EmailUnverified => write!(f, "Email not verified."),
66            ErrorCode::PasswordNotSet => write!(f, "Password not set."),
67            ErrorCode::GithubNotLinked => write!(f, "Github not connected."),
68        }
69    }
70}
71
72impl Copy for ErrorCode {}
73
74impl Clone for ErrorCode {
75    fn clone(&self) -> Self {
76        *self
77    }
78}
79
80#[derive(Debug, Serialize, Deserialize)]
81pub struct GithubInfo {
82    pub id: i64,
83    pub login: String,
84    pub name: String,
85    pub avatar_url: String,
86    pub html_url: String,
87    pub email: Option<String>,
88    pub created_at: String,
89    pub updated_at: String,
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95    use serde_json::json;
96    #[test]
97    fn test_smb_authorization() {
98        let smb_authorization = SmbAuthorization {
99            message: "test".to_owned(),
100            user: None,
101            user_email: None,
102            user_info: None,
103            error_code: Some(ErrorCode::EmailUnverified),
104        };
105        let json = json!({
106            "message": "test",
107            "user": null,
108            "user_email": null,
109            "user_info": null,
110            "error_code": 1001,
111        });
112        assert_eq!(serde_json::to_value(smb_authorization).unwrap(), json);
113    }
114}