@smbcloud / cli / commits / c0deb49

Fix error code serialization

Seto Elkahfi committed Sep 3, 2023 at 16:11 UTC c0deb49871f38ab40948b58574801cc556e666f0
5 files changed +100 -67
Cargo.lock
+41 -18
@@ -81,7 +81,7 @@ checksum = "b84f9ebcc6c1f5b8cb160f6990096a5c127f423fcb6e1ccc46c370cbdfb75dfc"
81 dependencies = [
82 "proc-macro2",
83 "quote",
84 - "syn",
84 + "syn 1.0.107",
85 ]
86
87 [[package]]
@@ -167,7 +167,7 @@ dependencies = [
167 "proc-macro-error",
168 "proc-macro2",
169 "quote",
170 - "syn",
170 + "syn 1.0.107",
171 ]
172
173 [[package]]
@@ -252,7 +252,7 @@ dependencies = [
252 "proc-macro2",
253 "quote",
254 "scratch",
255 - "syn",
255 + "syn 1.0.107",
256 ]
257
258 [[package]]
@@ -269,7 +269,7 @@ checksum = "0b75aed41bb2e6367cae39e6326ef817a851db13c13e4f3263714ca3cfb8de56"
269 dependencies = [
270 "proc-macro2",
271 "quote",
272 - "syn",
272 + "syn 1.0.107",
273 ]
274
275 [[package]]
@@ -736,7 +736,7 @@ checksum = "0f1b8c13cb1f814b634a96b2c725449fe7ed464a7b8781de8688be5ffbd3f305"
736 dependencies = [
737 "proc-macro2",
738 "quote",
739 - "syn",
739 + "syn 1.0.107",
740 ]
741
742 [[package]]
@@ -858,7 +858,7 @@ checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c"
858 dependencies = [
859 "proc-macro2",
860 "quote",
861 - "syn",
861 + "syn 1.0.107",
862 ]
863
864 [[package]]
@@ -963,7 +963,7 @@ dependencies = [
963 "proc-macro-error-attr",
964 "proc-macro2",
965 "quote",
966 - "syn",
966 + "syn 1.0.107",
967 "version_check",
968 ]
969
@@ -980,18 +980,18 @@ dependencies = [
980
981 [[package]]
982 name = "proc-macro2"
983 -version = "1.0.51"
983 +version = "1.0.66"
984 source = "registry+https://github.com/rust-lang/crates.io-index"
985 -checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
985 +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
986 dependencies = [
987 "unicode-ident",
988 ]
989
990 [[package]]
991 name = "quote"
992 -version = "1.0.23"
992 +version = "1.0.33"
993 source = "registry+https://github.com/rust-lang/crates.io-index"
994 -checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
994 +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
995 dependencies = [
996 "proc-macro2",
997 ]
@@ -1181,7 +1181,7 @@ checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
1181 dependencies = [
1182 "proc-macro2",
1183 "quote",
1184 - "syn",
1184 + "syn 1.0.107",
1185 ]
1186
1187 [[package]]
@@ -1195,6 +1195,17 @@ dependencies = [
1195 "serde",
1196 ]
1197
1198 +[[package]]
1199 +name = "serde_repr"
1200 +version = "0.1.16"
1201 +source = "registry+https://github.com/rust-lang/crates.io-index"
1202 +checksum = "8725e1dfadb3a50f7e5ce0b1a540466f6ed3fe7a0fca2ac2b8b831d31316bd00"
1203 +dependencies = [
1204 + "proc-macro2",
1205 + "quote",
1206 + "syn 2.0.30",
1207 +]
1208 +
1209 [[package]]
1210 name = "serde_spanned"
1211 version = "0.6.1"
@@ -1278,6 +1289,7 @@ dependencies = [
1289 "reqwest",
1290 "serde",
1291 "serde_json",
1292 + "serde_repr",
1293 "smbpndk-model",
1294 "smbpndk-networking",
1295 "smbpndk-networking-project",
@@ -1395,7 +1407,7 @@ dependencies = [
1407 "proc-macro2",
1408 "quote",
1409 "rustversion",
1398 - "syn",
1410 + "syn 1.0.107",
1411 ]
1412
1413 [[package]]
@@ -1409,6 +1421,17 @@ dependencies = [
1421 "unicode-ident",
1422 ]
1423
1424 +[[package]]
1425 +name = "syn"
1426 +version = "2.0.30"
1427 +source = "registry+https://github.com/rust-lang/crates.io-index"
1428 +checksum = "0ddc1f908d32ec46858c2d3b3daa00cc35bf4b6841ce4355c7bb3eedf2283a68"
1429 +dependencies = [
1430 + "proc-macro2",
1431 + "quote",
1432 + "unicode-ident",
1433 +]
1434 +
1435 [[package]]
1436 name = "tempfile"
1437 version = "3.3.0"
@@ -1449,7 +1472,7 @@ checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e"
1472 dependencies = [
1473 "proc-macro2",
1474 "quote",
1452 - "syn",
1475 + "syn 1.0.107",
1476 ]
1477
1478 [[package]]
@@ -1543,7 +1566,7 @@ checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8"
1566 dependencies = [
1567 "proc-macro2",
1568 "quote",
1546 - "syn",
1569 + "syn 1.0.107",
1570 ]
1571
1572 [[package]]
@@ -1631,7 +1654,7 @@ checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
1654 dependencies = [
1655 "proc-macro2",
1656 "quote",
1634 - "syn",
1657 + "syn 1.0.107",
1658 ]
1659
1660 [[package]]
@@ -1802,7 +1825,7 @@ dependencies = [
1825 "once_cell",
1826 "proc-macro2",
1827 "quote",
1805 - "syn",
1828 + "syn 1.0.107",
1829 "wasm-bindgen-shared",
1830 ]
1831
@@ -1836,7 +1859,7 @@ checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6"
1859 dependencies = [
1860 "proc-macro2",
1861 "quote",
1839 - "syn",
1862 + "syn 1.0.107",
1863 "wasm-bindgen-backend",
1864 "wasm-bindgen-shared",
1865 ]
smbpndk-cli/Cargo.toml
+2 -1
@@ -34,7 +34,8 @@ pre-commit-hooks = "0.1"
34 regex = "1.3.1"
35 reqwest = { version = "0.11", features = ["json"] }
36 serde = { version = "1", features = ["derive"] }
37 -serde_json = "1.0.82"
37 +serde_json = "1"
38 +serde_repr = "0.1"
39 smbpndk-model = { version = "0.3.0", path = "../smbpndk-model" }
40 smbpndk-networking = { version = "0.3.0", path = "../smbpndk-networking" }
41 smbpndk-networking-project = { version = "0.3.0", path = "../smbpndk-networking-project" }
smbpndk-cli/src/account/lib.rs
+23 -17
@@ -1,17 +1,19 @@
1 use anyhow::{anyhow, Result};
2 use console::style;
3 -use log::debug;
3 +use log::{debug, info};
4 use regex::Regex;
5 use reqwest::{Client, StatusCode};
6 use serde::{Deserialize, Serialize};
7
8 +use serde_repr::Deserialize_repr;
9 use spinners::Spinner;
10 use std::{
11 env,
12 + fmt::{Display, Formatter},
13 fs,
14 io::{BufRead, BufReader, Write},
15 net::{TcpListener, TcpStream},
14 - sync::mpsc::{self, Receiver, Sender}, fmt::{Display, Formatter},
16 + sync::mpsc::{self, Receiver, Sender},
17 };
18 use url_builder::URLBuilder;
19
@@ -27,7 +29,8 @@ pub struct SmbAuthorization {
29 pub error_code: Option<ErrorCode>,
30 }
31
30 -#[derive(Debug, Serialize, Deserialize)]
32 +#[derive(Debug, serde_repr::Serialize_repr, Deserialize_repr, PartialEq)]
33 +#[repr(u32)]
34 pub enum ErrorCode {
35 EmailNotFound = 1000,
36 EmailUnverified = 1001,
@@ -91,6 +94,7 @@ pub async fn authorize_github() -> Result<SmbAuthorization> {
94 match rx.recv() {
95 Ok(code) => {
96 debug!("Got code from channel: {:#?}", &code);
97 + //Err(anyhow!("Failed to get code from channel."))
98 process_connect_github(code).await
99 }
100 Err(e) => {
@@ -135,7 +139,10 @@ fn handle_connection(mut stream: TcpStream, tx: Sender<String>) {
139 }
140 None => {
141 debug!("Code not found.");
138 - ("HTTP/1.1 404 NOT FOUND", "./smbpndk-cli/src/account/404.html")
142 + (
143 + "HTTP/1.1 404 NOT FOUND",
144 + "./smbpndk-cli/src/account/404.html",
145 + )
146 }
147 };
148
@@ -162,29 +169,29 @@ pub async fn process_connect_github(code: String) -> Result<SmbAuthorization> {
169 .bold()
170 .to_string(),
171 );
172 + println!("Response: {:#?}", &response);
173 match response.status() {
174 StatusCode::OK => {
175 // Account authorized and token received
168 - spinner.stop_and_persist("✅", "Finished authorizing your GitHub account!".into());
169 - debug!("Response: {:#?}", &response);
176 + spinner.stop_and_persist("✅", "You're logged in with your GitHub account!".into());
177 let result: SmbAuthorization = response.json().await?;
171 - debug!("Result: {:#?}", &result);
178 + println!("Result: {:#?}", &result);
179 Ok(result)
173 - },
180 + }
181 StatusCode::NOT_FOUND => {
182 // Account not found and we show signup option
176 - spinner.stop_and_persist("✅", "Finished requesting GitHub token!".into());
177 - let result: SmbAuthorization = response.json().await?;
178 - debug!("Result: {:#?}", &result);
183 + spinner.stop_and_persist("🥲", "Account not found. Please signup!".into());
184 + let result = response.json().await?;
185 + println!("Result: {:#?}", &result);
186 Ok(result)
180 - },
187 + }
188 StatusCode::UNPROCESSABLE_ENTITY => {
189 // Account found but email not verified
183 - spinner.stop_and_persist("✅", "Finished requesting GitHub token!".into());
190 + spinner.stop_and_persist("🥹", "Unverified email!".into());
191 let result: SmbAuthorization = response.json().await?;
185 - debug!("Result: {:#?}", &result);
192 + println!("Result: {:#?}", &result);
193 Ok(result)
187 - },
194 + }
195 _ => {
196 // Other errors
197 let error = anyhow!("Error while authorizing token.");
@@ -195,8 +202,7 @@ pub async fn process_connect_github(code: String) -> Result<SmbAuthorization> {
202
203 fn build_authorize_smb_url() -> String {
204 let mut url_builder = smb_base_url_builder();
198 - url_builder
199 - .add_route("v1/authorize");
205 + url_builder.add_route("v1/authorize");
206 url_builder.build()
207 }
208
smbpndk-cli/src/account/login/process.rs
+23 -19
@@ -1,11 +1,13 @@
1 use crate::account::{
2 - lib::{authorize_github, SmbAuthorization, ErrorCode, GithubInfo, smb_base_url_builder},
2 + lib::{authorize_github, smb_base_url_builder, ErrorCode, GithubInfo, SmbAuthorization},
3 model::{Data, Status, User},
4 - signup::{SignupMethod, GithubEmail, do_signup, SignupGithubParams, SignupUserGithub, Provider},
4 + signup::{
5 + do_signup, GithubEmail, Provider, SignupGithubParams, SignupMethod, SignupUserGithub,
6 + },
7 };
8 use anyhow::{anyhow, Result};
9 use console::{style, Term};
8 -use dialoguer::{theme::ColorfulTheme, Input, Password, Select, Confirm};
10 +use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password, Select};
11 use log::debug;
12 use reqwest::Client;
13 use serde::{Deserialize, Serialize};
@@ -51,9 +53,7 @@ pub async fn process_login() -> Result<CommandResult> {
53
54 async fn login_with_github() -> Result<CommandResult> {
55 match authorize_github().await {
54 - Ok(result) => {
55 - process_authorization(result).await
56 - }
56 + Ok(result) => process_authorization(result).await,
57 Err(err) => {
58 let error = anyhow!("Failed to authorize your GitHub account. {}", err);
59 Err(error)
@@ -61,18 +61,19 @@ async fn login_with_github() -> Result<CommandResult> {
61 }
62 }
63
64 -async fn process_authorization(auth: SmbAuthorization) -> Result<CommandResult> {
64 +async fn process_authorization(auth: SmbAuthorization) -> Result<CommandResult> {
65 // Logged in with GitHub
66 if let Some(user) = auth.user {
67 let spinner = Spinner::new(
68 spinners::Spinners::SimpleDotsScrolling,
69 style("Logging you in...").green().bold().to_string(),
70 );
71 + // We're logged in with GitHub, but not with SMB.
72 return Ok(CommandResult {
73 spinner,
74 symbol: "✅".to_owned(),
75 msg: "You are logged in!".to_owned(),
75 - })
76 + });
77 }
78
79 // What to do if not logged in with GitHub?
@@ -81,19 +82,19 @@ async fn process_authorization(auth: SmbAuthorization) -> Result<CommandResult>
82 match error_code {
83 ErrorCode::EmailNotFound => {
84 return create_new_account(auth.user_email, auth.user_info).await
84 - },
85 - ErrorCode::EmailUnverified => {
86 - return send_email_verification(auth.user_email).await
85 }
86 + ErrorCode::EmailUnverified => return send_email_verification(auth.user_email).await,
87 }
88 }
89
91 - let error = anyhow!("Failed to login with GitHub.");
90 + let error: anyhow::Error = anyhow!("Failed to login with GitHub.");
91 Err(error)
93 -
92 }
93
96 -async fn create_new_account(user_email: Option<GithubEmail>, user_info: Option<GithubInfo>) -> Result<CommandResult> {
94 +async fn create_new_account(
95 + user_email: Option<GithubEmail>,
96 + user_info: Option<GithubInfo>,
97 +) -> Result<CommandResult> {
98 let confirm = Confirm::with_theme(&ColorfulTheme::default())
99 .with_prompt("Do you want to create a new account?")
100 .interact()
@@ -108,19 +109,22 @@ async fn create_new_account(user_email: Option<GithubEmail>, user_info: Option<G
109 return Ok(CommandResult {
110 spinner,
111 symbol: "✅".to_owned(),
111 - msg: "You are logged in!".to_owned(),
112 - })
112 + msg: "Please accept to link your GitHub account.".to_owned(),
113 + });
114 }
115
116 if let (Some(email), Some(info)) = (user_email, user_info) {
117 let params = SignupGithubParams {
118 user: SignupUserGithub {
119 email: email.email,
119 - authorizations_attributes: vec![Provider { uid: info.id.to_string(), provider: 0 }],
120 - }
120 + authorizations_attributes: vec![Provider {
121 + uid: info.id.to_string(),
122 + provider: 0,
123 + }],
124 + },
125 };
126
123 - return do_signup(&params).await
127 + return do_signup(&params).await;
128 }
129
130 Err(anyhow!("Shouldn't be here."))
smbpndk-cli/src/account/signup/process.rs
+11 -12
@@ -1,3 +1,4 @@
1 +use super::SignupMethod;
2 use crate::account::{
3 lib::authorize_github,
4 model::{Data, Status},
@@ -13,19 +14,18 @@ use smbpndk_networking::constants::BASE_URL;
14 use smbpndk_utils::email_validation;
15 use spinners::Spinner;
16 use std::fmt::{Display, Formatter};
16 -use super::SignupMethod;
17
18 pub struct SignupArgs {
19 pub email: String,
20 pub password: Option<String>,
21 pub password_confirmation: Option<String>,
22 - pub authorizations_attributes: Vec<Provider>
22 + pub authorizations_attributes: Vec<Provider>,
23 }
24
25 #[derive(Debug, Serialize)]
26 pub struct Provider {
27 pub uid: String,
28 - pub provider: i8
28 + pub provider: i8,
29 }
30
31 #[derive(Debug, Serialize)]
@@ -41,7 +41,7 @@ pub struct SignupEmailParams {
41 #[derive(Debug, Serialize)]
42 pub struct SignupUserGithub {
43 pub email: String,
44 - pub authorizations_attributes: Vec<Provider>
44 + pub authorizations_attributes: Vec<Provider>,
45 }
46
47 #[derive(Debug, Serialize)]
@@ -147,7 +147,7 @@ struct GithubUser {
147 name: String,
148 }
149
150 -#[derive(Debug, Serialize, Deserialize, Clone)]
150 +#[derive(Debug, Serialize, Deserialize)]
151 pub struct GithubEmail {
152 pub email: String,
153 primary: bool,
@@ -164,10 +164,7 @@ impl Display for GithubEmail {
164 pub async fn do_signup<T: Serialize + ?Sized>(args: &T) -> Result<CommandResult> {
165 let spinner = Spinner::new(
166 spinners::Spinners::BouncingBall,
167 - style("Signing you up...")
168 - .green()
169 - .bold()
170 - .to_string(),
167 + style("Signing you up...").green().bold().to_string(),
168 );
169
170 let response = Client::new()
@@ -177,9 +174,11 @@ pub async fn do_signup<T: Serialize + ?Sized>(args: &T) -> Result<CommandResult>
174 .await?;
175
176 match response.status() {
180 - StatusCode::OK => {
181 - Ok(CommandResult { spinner, symbol: "Yes".to_owned(), msg: "Success. Check email".to_owned() })
182 - }
177 + StatusCode::OK => Ok(CommandResult {
178 + spinner,
179 + symbol: "Yes".to_owned(),
180 + msg: "Success. Check email".to_owned(),
181 + }),
182 StatusCode::UNPROCESSABLE_ENTITY => {
183 let result: SignupResult = response.json().await?;
184 let error = anyhow!("Failed to signup: {}", result.status.message);