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