methods to check if a name or password are valid

master
rasul 5 years ago
parent 6f35c4a8e4
commit b7bfda95e9

@ -0,0 +1,67 @@
use lazy_static::lazy_static;
use crate::game::Game;
lazy_static! {
static ref NAME_CHARS: Vec<char> =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".chars().collect();
static ref PASS_CHARS: Vec<char> =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 `~@#$%^&*()_+-=[]{};':\",./<>?"
.chars().collect();
}
pub enum PlayerCheck {
Ok(String),
Err(Vec<String>),
}
impl Game {
pub fn check_player_name<S: Into<String>>(name: S) -> PlayerCheck {
let name = name.into();
let name = name.trim();
let mut err_vec = Vec::<String>::new();
if name.len() < 2 {
err_vec.push("2 character minimum".into());
} else if name.len() > 20 {
err_vec.push("20 character maximum.".into());
}
for c in name.chars() {
if !NAME_CHARS.contains(&c) {
err_vec.push("English alphabetic characters only.".into());
break;
}
}
if !err_vec.is_empty() {
PlayerCheck::Err(err_vec)
} else {
PlayerCheck::Ok(name.into())
}
}
pub fn check_player_password<S: Into<String>>(password: S) -> PlayerCheck {
let password = password.into();
let mut err_vec = Vec::<String>::new();
if password.len() < 8 {
err_vec.push("8 character minimum.".into());
} else if password.len() > 128 {
err_vec.push("128 character maximum.".into());
}
for c in password.chars() {
if !PASS_CHARS.contains(&c) {
err_vec.push("English alphanumeric characters or symbols only.".into());
break;
}
}
if err_vec.is_empty() {
PlayerCheck::Ok(password.into())
} else {
PlayerCheck::Err(err_vec)
}
}
}

@ -1,3 +1,4 @@
mod check_player;
#[allow(clippy::module_inception)]
mod game;
mod handle_events;
@ -6,4 +7,5 @@ mod new;
mod process_recv_message;
mod state;
pub use check_player::*;
pub use game::Game;

Loading…
Cancel
Save