use crate::secondary_validation::{Validator, get_next_digit};
pub struct PolishNationalIdChecksum;
const POLISH_NATIONAL_ID_MULTIPLIERS: &[u32] = &[1, 3, 7, 9];
impl Validator for PolishNationalIdChecksum {
fn is_valid_match(&self, regex_match: &str) -> bool {
if regex_match.len() != 11 {
return false;
}
let mut chars = regex_match.chars();
let mut sum = 0;
for i in 0..10 {
let digit = match get_next_digit(&mut chars) {
Some(d) => d,
None => return false,
};
sum += digit * POLISH_NATIONAL_ID_MULTIPLIERS[i % 4];
}
let expected_check_digit = (10 - (sum % 10)) % 10;
let actual_check_digit = match get_next_digit(&mut chars) {
Some(d) => d,
None => return false,
};
if actual_check_digit != expected_check_digit {
return false;
}
true
}
}
#[cfg(test)]
mod test {
use crate::secondary_validation::*;
#[test]
fn test_valid_aba_rtn() {
let valid_ids = vec![
"12345678903",
"12345678910",
];
for id in valid_ids {
assert!(PolishNationalIdChecksum.is_valid_match(id));
}
}
#[test]
fn test_invalid_aba_rtn() {
let invalid_ids = vec![
"12345678901",
"00000000001",
"99999999999",
];
for id in invalid_ids {
assert!(!PolishNationalIdChecksum.is_valid_match(id));
}
}
}