this_me/utils/
validate_input.rs1use std::io;
2
3pub fn validate_username(username: &str) -> io::Result<()> {
4 println!("🔍 Validating username: '{}' (chars: {}, bytes: {})", username, username.chars().count(), username.len());
5
6 let username_len = username.len();
7 if username_len < 5 || username_len > 21 {
8 return Err(io::Error::new(io::ErrorKind::InvalidInput, "❌ Username must be 5-21 characters long."));
9 }
10
11 if username.starts_with('.') || username.starts_with('_') {
12 return Err(io::Error::new(io::ErrorKind::InvalidInput, "❌ Username cannot start with '.' or '_'"));
13 }
14
15 if username.ends_with('.') || username.ends_with('_') {
16 return Err(io::Error::new(io::ErrorKind::InvalidInput, "❌ Username cannot end with '.' or '_'"));
17 }
18
19 if username.contains("..") || username.contains("__") || username.contains("._") || username.contains("_.") {
20 return Err(io::Error::new(io::ErrorKind::InvalidInput, "❌ Username cannot contain consecutive '.' or '_'"));
21 }
22
23 if !username.chars().all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '_') {
24 return Err(io::Error::new(io::ErrorKind::InvalidInput, "❌ Username must only contain letters, numbers, '.' or '_'"));
25 }
26
27 let period_count = username.matches('.').count();
28 if period_count > 2 {
29 return Err(io::Error::new(io::ErrorKind::InvalidInput, "❌ Username cannot have more than 2 periods."));
30 }
31
32 Ok(())
33}
34pub fn validate_hash(hash: &str) -> io::Result<()> {
35 if hash.len() < 4 {
36 return Err(io::Error::new(io::ErrorKind::InvalidInput, "❌ Hash (password) must be at least 4 characters long."));
37 }
38 Ok(())
39}