@smbcloud / cli / commits / 218e11e

Reuse email validation

Seto Elkahfi committed May 1, 2023 at 17:55 UTC 218e11e396daaf8d9afa234b0d3b86eddba26dca
2 files changed +7 -12
smbpndk-cli/src/account/login/process.rs
+2
@@ -7,6 +7,7 @@ use reqwest::Client;
7 use serde::{Deserialize, Serialize};
8 use smbpndk_model::CommandResult;
9 use smbpndk_networking::constants::BASE_URL;
10 +use smbpndk_utils::email_validation;
11 use spinners::Spinner;
12 use std::{
13 fs::{self, create_dir_all, OpenOptions},
@@ -33,6 +34,7 @@ pub async fn process_login() -> Result<CommandResult> {
34 println!("Provide your login credentials.");
35 let username = Input::<String>::with_theme(&ColorfulTheme::default())
36 .with_prompt("Username")
37 + .validate_with(|email: &String| email_validation(email))
38 .interact()
39 .unwrap();
40 let password = Password::with_theme(&ColorfulTheme::default())
smbpndk-cli/src/account/signup/process.rs
+5 -12
@@ -8,6 +8,7 @@ use reqwest::{Client, StatusCode};
8 use serde::{Deserialize, Serialize};
9 use smbpndk_model::CommandResult;
10 use smbpndk_networking::constants::BASE_URL;
11 +use smbpndk_utils::email_validation;
12 use spinners::Spinner;
13 use std::{
14 fmt::{Display, Formatter},
@@ -54,18 +55,10 @@ async fn signup_with_email(email: Option<String>) -> Result<CommandResult> {
55 email
56 } else {
57 Input::<String>::with_theme(&ColorfulTheme::default())
57 - .with_prompt("Username")
58 - .validate_with(|input: &String| -> Result<(), &str> {
59 - let email_regex = Regex::new(r"^([a-z0-9_+]([a-z0-9_+.]*[a-z0-9_+])?)@([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6})").unwrap();
60 -
61 - if email_regex.is_match(input) {
62 - Ok(())
63 - } else {
64 - Err("Username must be an email address")
65 - }
66 - })
67 - .interact()
68 - .unwrap()
58 + .with_prompt("Username")
59 + .validate_with(|email: &String| email_validation(email))
60 + .interact()
61 + .unwrap()
62 };
63
64 let password = Password::with_theme(&ColorfulTheme::default())