Skip to main content

pistonite_cu/cli/
password.rs

1macro_rules! special_chars {
2    ($c1:literal | $($c:literal)|* $(|)?) => {
3        static LEGAL_PASSWORD_ERROR_MESSAGE: &str = concat!(
4            "password contains illegal characters, only ascii alphanumeric characters and special characters in the following list are allowed: ",
5            stringify!($c1),
6            $( ", ", stringify!($c), )*
7        );
8        #[inline(always)]
9        fn special_char_legal(c: char) -> bool {
10            matches!(c, $c1 $( | $c )* )
11        }
12    }
13}
14special_chars! { '!' | '#' | '$' | '%' | '&' | '(' | ')' | '*' | '+' | ',' | '-' | '.' | '/' | ':' | ';' | '<' | '=' | '>' | '?' | '@' | '[' | ']' | '^' | '_' | '`' | '{' | '|' | '}' | '~'}
15/// Check if the password contains all "legal" characters (and is non-empty)
16pub fn password_chars_legal(s: &str) -> crate::Result<()> {
17    if s.is_empty() {
18        crate::bail!("password cannot be empty");
19    }
20    if s.chars()
21        .all(|c| c.is_ascii_alphanumeric() || special_char_legal(c))
22    {
23        return Ok(());
24    }
25    crate::bail!("{LEGAL_PASSWORD_ERROR_MESSAGE}");
26}