Skip to content

Commit

Permalink
Starting user data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanMcCormickJr committed Dec 12, 2024
1 parent 5a3ca58 commit 593e96c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
76 changes: 76 additions & 0 deletions src/users.rs
Original file line number Diff line number Diff line change
@@ -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<Username, &'static str> {
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<Self, String> {
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<Self, &'static str> {
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<u8>,
}


6 changes: 6 additions & 0 deletions tests/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@

#![forbid(unsafe_code)]

use bville_recycle::rocket;

#[tokio::test]
async fn test_create_user() {

}

0 comments on commit 593e96c

Please sign in to comment.