warlocks_cauldron/providers/spec/
pl.rs1use std::iter::zip;
2
3use super::super::{Datetime, NaiveDate, Datelike, Local, dependencies::*};
4
5
6pub struct PolandSpecProvider;
8
9impl PolandSpecProvider {
10 pub fn nip() -> String {
12 let mut nip_digits: Vec<u32> = randint(101, 999).to_string().chars()
13 .map(|c| c.to_digit(10).unwrap())
14 .chain(randints(0, 9, 6).into_iter())
15 .collect();
16
17 let nip_coefficients = vec![6, 5, 7, 2, 3, 4, 5, 6, 7];
18
19 let sum_v: u32 = zip(&nip_digits, &nip_coefficients).map(|(d, c)| d * c).sum();
20
21 let checksum_digit = sum_v % 11;
22 if checksum_digit > 9 {
23 return Self::nip();
24 }
25
26 nip_digits.push(checksum_digit);
27
28 nip_digits.into_iter().join("")
29 }
30
31 pub fn pesel(birth_date: Option<NaiveDate>, gender: Option<Gender>) -> String {
33 let birth_date = birth_date.unwrap_or_else(|| {
34 let now = Local::now().year();
35 Datetime::date(now - 100, now)
36 });
37
38 let year = birth_date.year();
39 let mut month = birth_date.month();
40 let day = birth_date.day();
41
42 if 1800 <= year && year <= 1899 {
43 month += 80;
44 } else if 2000 <= year && year <= 2099 {
45 month += 20;
46 } else if 2100 <= year && year <= 2199 {
47 month += 40
48 } else if 2200 <= year && year <= 2299 {
49 month += 60
50 }
51
52 let series_number = randint(0, 999);
53 let mut pesel_digits: Vec<u32> = format!("{year:02}{month:02}{day:02}{series_number:03}")
54 .chars().map(|c| c.to_digit(10).unwrap()).collect();
55
56 let gender_digit = get_random_element(match validate_variant(gender, None) {
57 Gender::MALE => vec![1, 3, 5, 7, 9].into_iter(),
58 Gender::FEMALE => vec![0, 2, 4, 6, 8].into_iter(),
59 _ => panic!("This gender doesnt accept!"),
60 });
61
62 pesel_digits.push(gender_digit);
63
64 let pesel_coeffs = vec![9, 7, 3, 1, 9, 7, 3, 1, 9, 7];
65
66 let sum_v: u32 = zip(&pesel_digits, &pesel_coeffs).map(|(d, c)| d * c).sum();
67
68 let checksum_digit = sum_v % 10;
69 pesel_digits.push(checksum_digit);
70
71 pesel_digits.into_iter().join("")
72 }
73
74 pub fn regon() -> String {
76 let mut regon_digits = randints(0, 9, 8);
77 let regon_coeffs = vec![8, 9, 2, 3, 4, 5, 6, 7];
78
79 let sum_v: u32 = zip(®on_digits, ®on_coeffs).map(|(d, c)| d * c).sum();
80
81 let mut checksum_digit = sum_v % 11;
82 if checksum_digit > 9 {
83 checksum_digit = 0;
84 }
85
86 regon_digits.push(checksum_digit);
87
88 regon_digits.into_iter().join("")
89 }
90}