passay-rs 0.1.0

A password validation library inspired by the Java Passay library.
Documentation
use crate::dictionary::Dictionary;
use crate::rule::reference::Reference;
use crate::rule::rule_result::RuleResult;

pub mod allowed_character;
pub mod allowed_regex;
pub mod character;
pub mod character_characteristics;
pub mod character_data;
pub mod character_occurrences;
mod character_sequence;
pub mod dictionary;
pub mod dictionary_substring;
mod digest_dictionary;
pub mod digest_history;
pub mod digest_source;
pub mod history;
pub mod illegal_character;
pub mod illegal_regex;
pub mod illegal_sequence;
pub mod length;
pub mod length_complexity;
pub mod message_resolver;
pub mod number_range;
mod password_utils;
pub mod password_validator;
pub mod reference;
pub mod repeat_character;
pub mod repeat_characters;
pub mod rule_result;
pub mod sequence_data;
pub mod source;
pub mod username;
pub mod whitespace;

pub trait Rule {
    fn validate(&self, password_data: &PasswordData) -> RuleResult;
    fn as_has_characters(&self) -> Option<&dyn HasCharacters> {
        None
    }
    fn as_dictionary_rule(&self) -> Option<&dyn DictionaryRuleTrait> {
        None
    }
}

pub trait HasCharacters: Rule {
    fn characters(&self) -> String;
}

pub trait DictionaryRuleTrait: Rule {
    fn dictionary(&self) -> &dyn Dictionary;
}

/// Contains password related information used by rules to perform password validation.
#[derive(Debug)]
pub struct PasswordData {
    password: String,
    username: Option<String>,
    password_references: Vec<Box<dyn Reference>>,
}

impl PasswordData {
    pub fn with_password(password: String) -> Self {
        Self {
            password,
            username: None,
            password_references: Vec::new(),
        }
    }
    pub fn with_password_and_user(password: String, username: Option<String>) -> Self {
        Self {
            password,
            username,
            password_references: Vec::new(),
        }
    }
    pub fn new(
        password: String,
        username: Option<String>,
        password_references: Vec<Box<dyn Reference>>,
    ) -> Self {
        Self {
            password,
            username,
            password_references,
        }
    }

    pub fn password(&self) -> &str {
        &self.password
    }

    pub fn password_references(&self) -> &Vec<Box<dyn Reference>> {
        &self.password_references
    }

    pub fn username(&self) -> Option<&str> {
        self.username.as_deref()
    }
}