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/// TODO: Move to ErrorResponse
52#[derive(Debug, Serialize_repr, Deserialize_repr, PartialEq)]
53#[repr(u32)]
54#[tsync]
55pub enum ErrorCode {
56    EmailNotFound = 1000,
57    EmailUnverified = 1001,
58    PasswordNotSet = 1003,
59    GithubNotLinked = 1004,
60}
61
62impl Display for ErrorCode {
63    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
64        match self {
65            ErrorCode::EmailNotFound => write!(f, "Email not found."),
66            ErrorCode::EmailUnverified => write!(f, "Email not verified."),
67            ErrorCode::PasswordNotSet => write!(f, "Password not set."),
68            ErrorCode::GithubNotLinked => write!(f, "Github not connected."),
69        }
70    }
71}
72
73impl Copy for ErrorCode {}
74
75impl Clone for ErrorCode {
76    fn clone(&self) -> Self {
77        *self
78    }
79}
80
81#[derive(Debug, Serialize, Deserialize)]
82pub struct GithubInfo {
83    pub id: i64,
84    pub login: String,
85    pub name: String,
86    pub avatar_url: String,
87    pub html_url: String,
88    pub email: Option<String>,
89    pub created_at: String,
90    pub updated_at: String,
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96    use serde_json::json;
97    #[test]
98    fn test_smb_authorization() {
99        let smb_authorization = SmbAuthorization {
100            message: "test".to_owned(),
101            user: None,
102            user_email: None,
103            user_info: None,
104            error_code: Some(ErrorCode::EmailUnverified),
105        };
106        let json = json!({
107            "message": "test",
108            "user": null,
109            "user_email": null,
110            "user_info": null,
111            "error_code": 1001,
112        });
113        assert_eq!(serde_json::to_value(smb_authorization).unwrap(), json);
114    }
115}