1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
use crate::error::TypeError;
use derive_more::Display;
use regex::Regex;
use serde::{Deserialize, Serialize};
/// This crate provides functionality to parse and validate raw passwords with different levels of strength.
///
/// # Example
///
/// ```
/// use custom_type::RawPassword;
///
/// let weak_password = RawPassword::parse_weak("weakpass").unwrap();
/// println!("{}", weak_password);
///
/// let medium_password = RawPassword::parse_medium("Medium1").unwrap();
/// println!("{}", medium_password);
///
/// let strict_password = RawPassword::parse_strict("Strong1!").unwrap();
/// println!("{}", strict_password);
///
/// ```
///
/// # Features
///
/// - Parse and validate passwords with different strength levels (weak, medium, strict).
/// - Custom error type `TypeError` for handling invalid passwords.
/// ### RawPassword : Parse `impl ToString` Into a Valid Password
/// Provides methods to parse and validate passwords with different strength criteria.
#[derive(Debug, PartialEq, Display, Serialize, Deserialize)]
pub struct RawPassword(String);
impl RawPassword {
/// Parses a given string into a weak password.
///
/// A weak password must be at least 8 characters long.
///
/// # Arguments
///
/// * `password` - A string slice that holds the password to be parsed.
///
/// # Returns
///
/// * `Ok(Self)` if the password meets the weak criteria.
/// * `Err(TypeError::ParseError)` if the password is invalid.
///
/// # Examples
///
/// ```
/// use custom_type::RawPassword;
///
/// let password = RawPassword::parse_weak("validpass");
/// assert!(password.is_ok());
///
/// let invalid_password = RawPassword::parse_weak("short");
/// assert!(invalid_password.is_err());
/// ```
pub fn parse_weak(password: impl ToString) -> Result<Self, TypeError> {
let password_str = password.to_string();
if password_str.len() >= 8 {
Ok(Self(password_str))
} else {
Err(TypeError::ParseError(String::from(
"Weak password: must be at least 8 characters long",
)))
}
}
/// Parses a given string into a medium password.
///
/// A medium password must be at least 8 characters long and contain both letters and digits.
///
/// # Arguments
///
/// * `password` - A string slice that holds the password to be parsed.
///
/// # Returns
///
/// * `Ok(Self)` if the password meets the medium criteria.
/// * `Err(TypeError::ParseError)` if the password is invalid.
///
/// # Examples
///
/// ```
/// use custom_type::RawPassword;
///
/// let password = RawPassword::parse_medium("valid123");
/// assert!(password.is_ok());
///
/// let invalid_password = RawPassword::parse_medium("noDigits");
/// assert!(invalid_password.is_err());
/// ```
pub fn parse_medium(password: impl ToString) -> Result<Self, TypeError> {
let password_str = password.to_string();
let re_digit = Regex::new(r"\d").unwrap();
let re_alpha = Regex::new(r"[a-zA-Z]").unwrap();
if password_str.len() >= 8
&& re_digit.is_match(&password_str)
&& re_alpha.is_match(&password_str)
{
Ok(Self(password_str))
} else {
Err(TypeError::ParseError(String::from("Medium password: must be at least 8 characters long and contain both letters and digits")))
}
}
/// Parses a given string into a strict password.
///
/// A strict password must be at least 8 characters long and contain uppercase, lowercase, digits, and special characters.
///
/// # Arguments
///
/// * `password` - A string slice that holds the password to be parsed.
///
/// # Returns
///
/// * `Ok(Self)` if the password meets the strict criteria.
/// * `Err(TypeError::ParseError)` if the password is invalid.
///
/// # Examples
///
/// ```
/// use custom_type::RawPassword;
///
/// let password = RawPassword::parse_strict("Valid123!");
/// assert!(password.is_ok());
///
/// let invalid_password = RawPassword::parse_strict("NoDigits!");
/// assert!(invalid_password.is_err());
/// ```
pub fn parse_strict(password: impl ToString) -> Result<Self, TypeError> {
let password_str = password.to_string();
let re_upper = Regex::new(r"[A-Z]").unwrap();
let re_lower = Regex::new(r"[a-z]").unwrap();
let re_digit = Regex::new(r"\d").unwrap();
let re_special = Regex::new(r"[^a-zA-Z\d]").unwrap();
if password_str.len() >= 8
&& re_upper.is_match(&password_str)
&& re_lower.is_match(&password_str)
&& re_digit.is_match(&password_str)
&& re_special.is_match(&password_str)
{
Ok(Self(password_str))
} else {
Err(TypeError::ParseError(String::from("Strict password: must be at least 8 characters long and contain uppercase, lowercase, digits, and special characters")))
}
}
}
/// ======================================================================
/// ========================= Unit Test
/// ======================================================================
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_weak() {
assert_eq!(
RawPassword::parse_weak("short".to_string()),
Err(TypeError::ParseError(String::from(
"Weak password: must be at least 8 characters long"
)))
);
assert_eq!(
RawPassword::parse_weak("validpass".to_string()),
Ok(RawPassword("validpass".to_string()))
);
}
#[test]
fn test_parse_medium() {
assert_eq!(
RawPassword::parse_medium("short".to_string()),
Err(TypeError::ParseError(String::from("Medium password: must be at least 8 characters long and contain both letters and digits")))
);
assert_eq!(
RawPassword::parse_medium("noDigits".to_string()),
Err(TypeError::ParseError(String::from("Medium password: must be at least 8 characters long and contain both letters and digits")))
);
assert_eq!(
RawPassword::parse_medium("valid123".to_string()),
Ok(RawPassword("valid123".to_string()))
);
}
#[test]
fn test_parse_strict() {
assert_eq!(
RawPassword::parse_strict("short".to_string()),
Err(TypeError::ParseError(String::from("Strict password: must be at least 8 characters long and contain uppercase, lowercase, digits, and special characters")))
);
assert_eq!(
RawPassword::parse_strict("NoDigits!".to_string()),
Err(TypeError::ParseError(String::from("Strict password: must be at least 8 characters long and contain uppercase, lowercase, digits, and special characters")))
);
assert_eq!(
RawPassword::parse_strict("noupper1!".to_string()),
Err(TypeError::ParseError(String::from("Strict password: must be at least 8 characters long and contain uppercase, lowercase, digits, and special characters")))
);
assert_eq!(
RawPassword::parse_strict("VALID123".to_string()),
Err(TypeError::ParseError(String::from("Strict password: must be at least 8 characters long and contain uppercase, lowercase, digits, and special characters")))
);
assert_eq!(
RawPassword::parse_strict("Valid123!".to_string()),
Ok(RawPassword("Valid123!".to_string()))
);
}
}