smbcloud_model/
account.rs

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