From 593e96cc22fdecdb770e52e2ccccd4b93f822cdf Mon Sep 17 00:00:00 2001 From: "Jonathan A. McCormick, Jr." <67705789+JonathanMcCormickJr@users.noreply.github.com> Date: Thu, 12 Dec 2024 16:07:37 -0600 Subject: [PATCH] Starting user data structure --- Cargo.lock | 1 + Cargo.toml | 1 + src/lib.rs | 1 + src/users.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/users.rs | 6 ++++ 5 files changed, 85 insertions(+) create mode 100644 src/users.rs diff --git a/Cargo.lock b/Cargo.lock index 41e1f66..c3edddf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -170,6 +170,7 @@ version = "0.1.0" dependencies = [ "dotenvy", "glob", + "regex", "rocket", "sqlx", "tokio", diff --git a/Cargo.toml b/Cargo.toml index e7f17e1..7e4a8df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] dotenvy = "0.15" +regex = "1.11" rocket = { version = "0.5.1", features = ["tls",] } sqlx = { version = "0.8.2", features = ["runtime-tokio-native-tls", "mysql"] } tokio = { version = "1.42.0", features = ["full"] } diff --git a/src/lib.rs b/src/lib.rs index 595db9e..ec7509e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ use std::env; pub mod db_initializer; pub mod db_utils; +pub mod users; #[get("/")] pub fn map_root() -> &'static str { diff --git a/src/users.rs b/src/users.rs new file mode 100644 index 0000000..101bb66 --- /dev/null +++ b/src/users.rs @@ -0,0 +1,76 @@ +// src/users.rs + +#![forbid(unsafe_code)] + +use bville_recycle::rocket; +use regex::Regex; + +pub struct Username { + // Username may only contain uppercase letters, lowercase letters, numbers, and underscores + pub username: String, +} + +impl Username { + pub fn new(username: String) -> Result { + if username.chars().all(|c| c.is_alphanumeric() || c == '_') { + if username.len() <= 32 { + Ok(Username { username }) + } else { + Err("Username must be 32 characters or less") + } + } else { + Err("Username may only contain uppercase letters, lowercase letters, numbers, and underscores") + } + } +} + +pub struct Email { + // Email must be a valid email address + pub email: String, +} + +impl Email { + pub fn new(email: String) -> Result { + let email_regex = Regex::new(r"^[a-zA-Z\u0080-\uFFFF0-9._%+-]+@[a-zA-Z\u0080-\uFFFF0-9.-]+\.[a-zA-Z\u0080-\uFFFF]{1,}$").unwrap(); + + if email_regex.is_match(email) { + Ok(Self { + email: email.to_string(), + }) + } else { + Err(format!("'{}' is not a valid email address", email)) + } + } +} + +pub struct Password { + // Password must be at least 16 characters long + pub password: String, +} + +impl Password { + pub fn new(password: String) -> Result { + if password.len() >= 16 { + if password.contains() { + + } else { + Err("Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character") + } + Ok(Self { + password: password.to_string(), + }) + } else { + Err("Password must be at least 16 characters long") + } + } +} + +pub struct User { + pub id: u32, + pub username: Username, + pub email: Email, + pub password: Password, + pub second_factor: Vec, +} + + diff --git a/tests/users.rs b/tests/users.rs index 4ecd227..ec10767 100644 --- a/tests/users.rs +++ b/tests/users.rs @@ -2,3 +2,9 @@ #![forbid(unsafe_code)] +use bville_recycle::rocket; + +#[tokio::test] +async fn test_create_user() { + +}