@smbcloud / cli / commits / 230b55d

Extract models

Seto Elkahfi committed Sep 16, 2023 at 10:05 UTC 230b55d716652599ad6ec8a4d0fa032ce1e74dbf
10 files changed +190 -180
Cargo.lock
+1
@@ -1244,6 +1244,7 @@ dependencies = [
1244 "chrono",
1245 "serde",
1246 "serde_json",
1247 + "serde_repr",
1248 "spinners",
1249 ]
1250
smbpndk-cli/src/account/lib.rs
+2 -54
@@ -1,11 +1,10 @@
1 -use super::{model::User, signup::GithubEmail};
1 use anyhow::{anyhow, Result};
2 use console::style;
3 use log::debug;
4 use regex::Regex;
5 use reqwest::{Client, Response, StatusCode};
7 -use serde::{Deserialize, Serialize};
8 -use serde_repr::Deserialize_repr;
6 +
7 +use smbpndk_model::account::SmbAuthorization;
8 use smbpndk_networking::{
9 constants::{
10 GH_OAUTH_CLIENT_ID, GH_OAUTH_REDIRECT_HOST, GH_OAUTH_REDIRECT_PORT, PATH_AUTHORIZE,
@@ -14,7 +13,6 @@ use smbpndk_networking::{
13 };
14 use spinners::Spinner;
15 use std::{
17 - fmt::{Display, Formatter},
16 fs::{create_dir_all, OpenOptions},
17 io::{BufRead, BufReader, Write},
18 net::{TcpListener, TcpStream},
@@ -22,56 +20,6 @@ use std::{
20 };
21 use url_builder::URLBuilder;
22
25 -// This is smb authorization model.
26 -#[derive(Debug, Serialize, Deserialize)]
27 -pub struct SmbAuthorization {
28 - pub message: String,
29 - pub user: Option<User>,
30 - pub user_email: Option<GithubEmail>,
31 - pub user_info: Option<GithubInfo>,
32 - pub error_code: Option<ErrorCode>,
33 -}
34 -
35 -#[derive(Debug, serde_repr::Serialize_repr, Deserialize_repr, PartialEq)]
36 -#[repr(u32)]
37 -pub enum ErrorCode {
38 - EmailNotFound = 1000,
39 - EmailUnverified = 1001,
40 - PasswordNotSet = 1003,
41 - GithubNotLinked = 1004,
42 -}
43 -
44 -impl Display for ErrorCode {
45 - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46 - match self {
47 - ErrorCode::EmailNotFound => write!(f, "Email not found."),
48 - ErrorCode::EmailUnverified => write!(f, "Email not verified."),
49 - ErrorCode::PasswordNotSet => write!(f, "Password not set."),
50 - ErrorCode::GithubNotLinked => write!(f, "Github not connected."),
51 - }
52 - }
53 -}
54 -
55 -impl Copy for ErrorCode {}
56 -
57 -impl Clone for ErrorCode {
58 - fn clone(&self) -> Self {
59 - *self
60 - }
61 -}
62 -
63 -#[derive(Debug, Serialize, Deserialize)]
64 -pub struct GithubInfo {
65 - pub id: i64,
66 - pub login: String,
67 - pub name: String,
68 - pub avatar_url: String,
69 - pub html_url: String,
70 - pub email: Option<String>,
71 - pub created_at: String,
72 - pub updated_at: String,
73 -}
74 -
23 pub async fn authorize_github() -> Result<SmbAuthorization> {
24 // Spin up a simple localhost server to listen for the GitHub OAuth callback
25 // setup_oauth_callback_server();
smbpndk-cli/src/account/login/process.rs
+8 -29
@@ -1,18 +1,19 @@
1 use crate::account::{
2 forgot::{Param, UserUpdatePassword},
3 - lib::{authorize_github, save_token, ErrorCode, GithubInfo, SmbAuthorization},
4 - model::{Data, Status, User},
5 - signup::{
6 - do_signup, GithubEmail, Provider, SignupGithubParams, SignupMethod, SignupUserGithub,
7 - },
3 + lib::{authorize_github, save_token},
4 + signup::{do_signup, SignupMethod},
5 };
6 use anyhow::{anyhow, Result};
7 use console::{style, Term};
8 use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password, Select};
9 use log::debug;
10 use reqwest::{Client, StatusCode};
14 -use serde::{Deserialize, Serialize};
15 -use smbpndk_model::CommandResult;
11 +use smbpndk_model::{
12 + account::{ErrorCode, GithubInfo, SmbAuthorization, User},
13 + login::{LoginArgs, LoginParams, UserParam},
14 + signup::{GithubEmail, Provider, SignupGithubParams, SignupUserGithub},
15 + CommandResult,
16 +};
17 use smbpndk_networking::{
18 constants::{
19 PATH_LINK_GITHUB_ACCOUNT, PATH_RESEND_CONFIRMATION, PATH_RESET_PASSWORD_INSTRUCTIONS,
@@ -24,28 +25,6 @@ use smbpndk_utils::email_validation;
25 use spinners::Spinner;
26 use std::fs::{self};
27
27 -pub struct LoginArgs {
28 - pub username: String,
29 - pub password: String,
30 -}
31 -
32 -#[derive(Debug, Serialize)]
33 -struct LoginParams {
34 - user: UserParam,
35 -}
36 -
37 -#[derive(Debug, Serialize)]
38 -struct UserParam {
39 - email: String,
40 - password: String,
41 -}
42 -
43 -#[derive(Debug, Serialize, Deserialize)]
44 -struct LoginResult {
45 - status: Status,
46 - data: Data,
47 -}
48 -
28 pub async fn process_login() -> Result<CommandResult> {
29 // Check if token file exists
30 if smb_token_file_path().is_some() {
smbpndk-cli/src/account/model/mod.rs
-28
@@ -1,29 +1 @@
1 -use std::fmt::{Display, Formatter};
1
3 -use serde::{Deserialize, Serialize};
4 -
5 -// SMBPNDK Users.
6 -#[derive(Debug, Serialize, Deserialize)]
7 -pub struct User {
8 - pub(crate) id: i32,
9 - pub(crate) email: String,
10 -}
11 -
12 -impl Display for User {
13 - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
14 - write!(f, "User: id: {}, email: {}", self.id, self.email)
15 - }
16 -}
17 -
18 -#[derive(Debug, Serialize, Deserialize)]
19 -pub struct Status {
20 - code: Option<i32>,
21 - pub(crate) message: String,
22 -}
23 -
24 -#[derive(Debug, Serialize, Deserialize)]
25 -pub struct Data {
26 - id: i32,
27 - email: String,
28 - created_at: String,
29 -}
smbpndk-cli/src/account/signup/process.rs
+6 -69
@@ -1,61 +1,18 @@
1 use super::SignupMethod;
2 -use crate::account::{
3 - lib::authorize_github,
4 - model::{Data, Status},
5 -};
2 +use crate::account::lib::authorize_github;
3 use anyhow::{anyhow, Result};
4 use console::{style, Term};
5 use dialoguer::{theme::ColorfulTheme, Input, Password, Select};
6 use log::debug;
7 use reqwest::{Client, StatusCode};
11 -use serde::{Deserialize, Serialize};
12 -use smbpndk_model::CommandResult;
13 -
8 +use serde::Serialize;
9 +use smbpndk_model::{
10 + signup::{SignupEmailParams, SignupResult, SignupUserEmail},
11 + CommandResult,
12 +};
13 use smbpndk_networking::{constants::PATH_USERS, smb_base_url_builder, smb_token_file_path};
14 use smbpndk_utils::email_validation;
15 use spinners::Spinner;
17 -use std::fmt::{Display, Formatter};
18 -
19 -pub struct SignupArgs {
20 - pub email: String,
21 - pub password: Option<String>,
22 - pub password_confirmation: Option<String>,
23 - pub authorizations_attributes: Vec<Provider>,
24 -}
25 -
26 -#[derive(Debug, Serialize)]
27 -pub struct Provider {
28 - pub uid: String,
29 - pub provider: i8,
30 -}
31 -
32 -#[derive(Debug, Serialize)]
33 -pub struct SignupGithubParams {
34 - pub user: SignupUserGithub,
35 -}
36 -
37 -#[derive(Debug, Serialize)]
38 -pub struct SignupEmailParams {
39 - pub user: SignupUserEmail,
40 -}
41 -
42 -#[derive(Debug, Serialize)]
43 -pub struct SignupUserGithub {
44 - pub email: String,
45 - pub authorizations_attributes: Vec<Provider>,
46 -}
47 -
48 -#[derive(Debug, Serialize)]
49 -pub struct SignupUserEmail {
50 - pub email: String,
51 - pub password: String,
52 -}
53 -
54 -#[derive(Debug, Serialize, Deserialize)]
55 -struct SignupResult {
56 - status: Status,
57 - data: Option<Data>,
58 -}
16
17 pub async fn process_signup() -> Result<CommandResult> {
18 // Check if token file exists
@@ -154,26 +111,6 @@ async fn signup_with_github() -> Result<CommandResult> {
111 }
112 }
113
157 -#[derive(Debug, Serialize, Deserialize)]
158 -struct GithubUser {
159 - email: Option<String>,
160 - name: String,
161 -}
162 -
163 -#[derive(Debug, Serialize, Deserialize)]
164 -pub struct GithubEmail {
165 - pub email: String,
166 - primary: bool,
167 - verified: bool,
168 - visibility: Option<String>,
169 -}
170 -
171 -impl Display for GithubEmail {
172 - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
173 - write!(f, "{}", self.email)
174 - }
175 -}
176 -
114 pub async fn do_signup<T: Serialize + ?Sized>(args: &T) -> Result<CommandResult> {
115 let spinner = Spinner::new(
116 spinners::Spinners::BouncingBall,
smbpndk-model/Cargo.toml
+1
@@ -15,4 +15,5 @@ readme = "README.md"
15 chrono = { version = "0.4", features = ["serde"] }
16 serde = { version = "1", features = ["derive"] }
17 serde_json = "1.0.82"
18 +serde_repr = "0.1"
19 spinners = "4.1.0"
smbpndk-model/src/account.rs new
+80
@@ -0,0 +1,80 @@
1 +use crate::signup::GithubEmail;
2 +use serde::{Deserialize, Serialize};
3 +use serde_repr::Serialize_repr;
4 +use std::fmt::{Display, Formatter};
5 +
6 +// SMBPNDK Users.
7 +#[derive(Debug, Serialize, Deserialize)]
8 +pub struct User {
9 + pub id: i32,
10 + pub email: String,
11 +}
12 +
13 +impl 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)]
20 +pub struct Status {
21 + pub code: Option<i32>,
22 + pub message: String,
23 +}
24 +
25 +#[derive(Debug, Serialize, Deserialize)]
26 +pub 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)]
34 +pub 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, PartialEq)]
43 +#[repr(u32)]
44 +pub enum ErrorCode {
45 + EmailNotFound = 1000,
46 + EmailUnverified = 1001,
47 + PasswordNotSet = 1003,
48 + GithubNotLinked = 1004,
49 +}
50 +
51 +impl 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 +
62 +impl Copy for ErrorCode {}
63 +
64 +impl Clone for ErrorCode {
65 + fn clone(&self) -> Self {
66 + *self
67 + }
68 +}
69 +
70 +#[derive(Debug, Serialize, Deserialize)]
71 +pub 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 +}
smbpndk-model/src/lib.rs
+4
@@ -1,3 +1,7 @@
1 +pub mod account;
2 +pub mod login;
3 +pub mod signup;
4 +
5 use chrono::{DateTime, Utc};
6 use serde::{Deserialize, Serialize};
7 use spinners::Spinner;
smbpndk-model/src/login.rs new
+24
@@ -0,0 +1,24 @@
1 +use crate::account::{Data, Status};
2 +use serde::{Deserialize, Serialize};
3 +
4 +pub struct LoginArgs {
5 + pub username: String,
6 + pub password: String,
7 +}
8 +
9 +#[derive(Debug, Serialize)]
10 +pub struct LoginParams {
11 + pub user: UserParam,
12 +}
13 +
14 +#[derive(Debug, Serialize)]
15 +pub struct UserParam {
16 + pub email: String,
17 + pub password: String,
18 +}
19 +
20 +#[derive(Debug, Serialize, Deserialize)]
21 +pub struct LoginResult {
22 + pub status: Status,
23 + pub data: Data,
24 +}
smbpndk-model/src/signup.rs new
+64
@@ -0,0 +1,64 @@
1 +use crate::account::{Data, Status};
2 +use serde::{Deserialize, Serialize};
3 +use std::fmt::{Display, Formatter};
4 +
5 +pub struct SignupArgs {
6 + pub email: String,
7 + pub password: Option<String>,
8 + pub password_confirmation: Option<String>,
9 + pub authorizations_attributes: Vec<Provider>,
10 +}
11 +
12 +#[derive(Debug, Serialize)]
13 +pub struct Provider {
14 + pub uid: String,
15 + pub provider: i8,
16 +}
17 +
18 +#[derive(Debug, Serialize)]
19 +pub struct SignupGithubParams {
20 + pub user: SignupUserGithub,
21 +}
22 +
23 +#[derive(Debug, Serialize)]
24 +pub struct SignupEmailParams {
25 + pub user: SignupUserEmail,
26 +}
27 +
28 +#[derive(Debug, Serialize)]
29 +pub struct SignupUserGithub {
30 + pub email: String,
31 + pub authorizations_attributes: Vec<Provider>,
32 +}
33 +
34 +#[derive(Debug, Serialize)]
35 +pub struct SignupUserEmail {
36 + pub email: String,
37 + pub password: String,
38 +}
39 +
40 +#[derive(Debug, Serialize, Deserialize)]
41 +pub struct SignupResult {
42 + pub status: Status,
43 + pub data: Option<Data>,
44 +}
45 +
46 +#[derive(Debug, Serialize, Deserialize)]
47 +pub struct GithubUser {
48 + pub email: Option<String>,
49 + pub name: String,
50 +}
51 +
52 +#[derive(Debug, Serialize, Deserialize)]
53 +pub struct GithubEmail {
54 + pub email: String,
55 + primary: bool,
56 + verified: bool,
57 + visibility: Option<String>,
58 +}
59 +
60 +impl Display for GithubEmail {
61 + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
62 + write!(f, "{}", self.email)
63 + }
64 +}