pesel 0.1.1

PESEL validation & generation library
Documentation
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use crate::pesel_parsing_error::PESELParsingError;
use std::str::FromStr;

use rand::Rng;
use rand::prelude::ThreadRng;

const PESEL_LENGTH: usize = 11;

/// Enum to represent Male/Female
#[derive(Debug, PartialEq)]
pub enum PeselGender {
    Male,
    Female,
}

#[derive(Debug)]
pub struct PESEL {
    raw:        String,     // raw PESEL as &str
    yob:        u8,         // year of birth
    mob:        u8,         // month of birth, codes century as well (could cover 5 centuries)
    dob:        u8,         // day of birth
    gender:     u8,         // biological gender
    checksum:   u8,         // checksum used for validation
    // all fields below are used for PESEL validation check
    a:          u8,         // yob (1)
    b:          u8,         // yob (2)
    c:          u8,         // mob (1)
    d:          u8,         // mob (2)
    e:          u8,         // dob (1)
    f:          u8,         // dob (2)
    g:          u8,         // random1
    h:          u8,         // random2
    i:          u8,         // random3
    j:          u8,         // gender
}


impl PESEL {
    /// Creates new PESEL strucutre based on:
    /// - birth date (could be in the future!)
    /// - biological gender
    ///
    /// Example:
    /// ```rust
    /// use pesel::pesel::{PESEL as PESEL, PeselGender};
    ///
    /// // some code here...
    ///
    /// let new_pesel = PESEL::new(1981, 05, 29, PeselGender::Female);
    /// ```
    /// Returned PESEL structure is checked using PESEL valiation algorithm (i.e. typing `new_pesel.is_valid()` should return `true` in all cases
    pub fn new(year: u16, month: u8, day: u8, pesel_gender: PeselGender) -> PESEL {
        // TODO: what to do if dob is out of accepted range?
//        if year < 1800 && year > 2299 {
//            Err(PESELParsingError::new("date is out of range!"))
//        }
        let pesel_year = year % 100;
        let pesel_month = month + PESEL::calc_month_century_offset(year);

        let mut rng = rand::thread_rng();
        let (random1, random2, random3) = PESEL::generate_random_values(&mut rng);

        let gender = PESEL::generate_gender_digit(pesel_gender, &mut rng);

        let pesel_string =  format!("{:02}{:02}{:02}{:1}{:1}{:1}{:1}", pesel_year, pesel_month, day, random1, random2, random3, gender);

        let checksum = PESEL::calc_checksum_from_pesel_string(&pesel_string);

        PESEL::from_str(format!("{}{:1}", &pesel_string, checksum).as_str()).unwrap()
    }
}

impl FromStr for PESEL {
    type Err =  PESELParsingError;

    /// This method implements parsing 11 character long string, containing only digits into PESEL number.
    /// There are some checks performed:
    /// - length of the string provided (11 characters)
    /// - all characters have to be digits
    /// - birth year must be between 1800 and 2299
    /// - day should not exceed 31
    /// - month should be of range 1..12
    ///
    /// Important note: as this function could be used to build a PESEL structure retrieved from database - no algorithm validity check against PESEL number is performed. This is due to the fact that some PESEL numbers in use were not generated correctly (but are recognized by State as valid ones).
    ///
    /// Example of use:
    ///
    /// ```rust
    /// use std::str::FromStr;
    /// use pesel::pesel::{PESEL as PESEL, PeselGender};
    ///
    /// // some code here...
    ///
    /// let pesel_number ="44051401458".to_string();
    /// let pesel = PESEL::from_str(pesel_number.as_str());
    /// match pesel {
    /// Ok(t) => println!("{}", t),
    /// _ => panic!("invalid PESEL provided")
    /// }
    /// ```
    ///
    /// In case an error occrus `PESELParsingError` with apropriate message is being returned. This may happen when:
    /// - string is not of expected length (11 characters)
    /// - not all characters inside string are digits
    /// - year of birth is out of range
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.len() != PESEL_LENGTH {
            return Err(PESELParsingError::new("PESEL has to be of 11 chars long"));
        }
        if s.chars().any(|f| !f.is_ascii_digit()) {
            return Err(PESELParsingError::new("PESEL may only contain digits!"));
        }
        // Q: should PESEL become automatically invalidated (and thus impossible to create) if algorithm based validation fails?
        // The answer for the above question should be NO. This is due to the fact, that some people have been assigned PESEL numbers that do not go through an algorithmic validation, but from the perspective of the State - are still valid. I believe there is a database with all the exceptions stored, and making it more restrictive than it is in real life does not make any sense.
        let checksum = s[10..11].parse::<u8>().unwrap();
        let gender  = s[9..10].parse::<u8>().unwrap();

        // Extra validity check in regards to date:
        // a) year could be: 0-99 - no need to check, as it is not possible to code anything more than 99 on 2 decimal places
        let yob = s[0..2].parse::<u8>().unwrap();
        let mob = s[2..4].parse::<u8>().unwrap();
        // b) month could be: 1-12, 21-32, 41-52, 61-72, 81-92
        if ! ((1..13).contains(&mob) || (21..33).contains(&mob) || (41..53).contains(&mob) || (61..73).contains(&mob) || (81..93).contains(&mob)) {
            return Err(PESELParsingError::new("Invalid PESEL! Only dates between 1800 and 2299 are suppored!"))
        }

        let dob = s[4..6].parse::<u8>().unwrap();
        // c) day could be max 31
        if dob > 31 {
            return Err(PESELParsingError::new("Invalid PESEL! Day exceeds 31"))
        }

        let (a, b, c, d, e, f, g, h, i, j) = PESEL::extract_pesel_factors(s);

        Ok(PESEL{
            raw: s.clone().to_string(),
            yob,
            mob,
            dob,
            gender,
            checksum,
            a,
            b,
            c,
            d,
            e,
            f,
            g,
            h,
            i,
            j,
        })
    }
}

impl std::fmt::Display for PESEL {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(f, "PESEL: {}\n\
        date of birth: {}\n\
        gender: {}\n\
        valid: {}", self.raw, self.date_of_birth(), self.gender_name(), self.is_valid())
    }
}

impl PESEL {
    /// Utility function - returns triple of random u8s (this is needed to fill some extra space being part of PESEL number
    fn generate_random_values(rng: &mut ThreadRng) -> (u8, u8, u8) {
        let random1 = rng.gen_range(0, 10) as u8;
        let random2 = rng.gen_range(0, 10) as u8;
        let random3 = rng.gen_range(0, 10) as u8;
        (random1, random2, random3)
    }

    /// Utility function - calculates offset to be added to month to code a century person has been born in
    fn calc_month_century_offset(year: u16) -> u8 {
        let century = match year {
            1800...1899 => 80,
            1900...1999 => 0,
            2000...2099 => 20,
            2100...2199 => 40,
            2200...2299 => 60,
            _ => 0,
        };
        century
    }

    /// Utility function - returns digit corresponding to biological gender.
    /// Odd - represents man
    /// Even - represents woman
    fn generate_gender_digit(pesel_gender: PeselGender, rng: &mut ThreadRng) -> u8 {
        let women = vec![0, 2, 4, 6, 8];
        let men = vec![1, 3, 5, 7, 9];
        let gender = match pesel_gender {
            PeselGender::Male => men[rng.gen_range(0, 5)] as u8,
            PeselGender::Female => women[rng.gen_range(0, 5)] as u8,
        };
        gender
    }

    /// Utility function - calculates checksum directly from PESEL string
    fn calc_checksum_from_pesel_string(pesel_string: &str) -> u8 {
        let (a, b, c, d, e, f, g, h, i, j) = PESEL::extract_pesel_factors(pesel_string);
        PESEL::calc_checksum(a, b, c, d, e, f, g, h, i, j)
    }

    /// Utility function - calculates checksum when given all the factors as parameters
    fn calc_checksum(a: u8, b: u8, c:u8, d:u8, e:u8, f:u8, g:u8, h:u8, i:u8, j:u8) -> u8 {
        let sum:u16 = 9 * a as u16 +
            7 * b as u16 +
            3 * c as u16 +
            d as u16 +
            9 * e as u16 +
            7 * f as u16 +
            3 * g as u16 +
            h as u16 +
            9 * i as u16 +
            7 * j as u16;
        (sum % 10) as u8
    }

    /// Utility function - extracts all factors (a..j) from a string representing PESEL
    fn extract_pesel_factors(pesel_string: &str) -> (u8, u8, u8, u8, u8, u8, u8, u8, u8, u8) {
        let mut all_chars = pesel_string.chars();
        let a = all_chars.next().unwrap().to_digit(10).unwrap() as u8;
        let b = all_chars.next().unwrap().to_digit(10).unwrap() as u8;
        let c = all_chars.next().unwrap().to_digit(10).unwrap() as u8;
        let d = all_chars.next().unwrap().to_digit(10).unwrap() as u8;
        let e = all_chars.next().unwrap().to_digit(10).unwrap() as u8;
        let f = all_chars.next().unwrap().to_digit(10).unwrap() as u8;
        let g = all_chars.next().unwrap().to_digit(10).unwrap() as u8;
        let h = all_chars.next().unwrap().to_digit(10).unwrap() as u8;
        let i = all_chars.next().unwrap().to_digit(10).unwrap() as u8;
        let j = all_chars.next().unwrap().to_digit(10).unwrap() as u8;

        (a, b, c, d, e, f, g, h, i, j)
    }

    /// Checks if PESEL number is properly generated - i.e. if algorithmic check on all fields is equal to checksum (which is a part of PESEL number)
    ///
    /// PESEL validation algorithm is as follows:
    /// 1. PESEL number is 11 digits, last one is checksum. This gives 10 digits.
    /// 2. The digits are usually called a, b, c, d, e, f, g, h, i, j
    /// 3. First step is to calculate special sum of all digits except checksum as follows:
    ///     9 * a + 7 * b + 3 * c + 1....
    /// 4. The sum calculated above modulo 10 should be equal to checksum
    ///
    /// Please note that some PESEL numbers that are in use in Poland are not properly generated, and thus this check may fail for a PESEL number that is officially used.
    pub fn is_valid(&self) -> bool {
        let calculated_checksum = PESEL::calc_checksum(self.a, self.b, self.c, self.d, self.e, self.f, self.g, self.h, self.i, self.j);

        self.checksum == calculated_checksum
    }

    /// Returns information on biological gender of a person with specified PESEL number assgigned.
    ///
    /// This function returns true in case PESEL number is assigned to biological female
    pub fn is_male(&self) -> bool {
        self.gender % 2 != 0
    }

    /// Returns information on biological gender of a person with specified PESEL number assigned.
    ///
    /// This function returns true in case PESEL number is assigned to biological female
    pub fn is_female(&self) -> bool {
        self.gender % 2 == 0
    }

    // TODO: test it!
    /// Returns biological gender as PeselGender enum
    pub fn gender_type(&self) -> PeselGender {
        match self.gender % 2 == 0 {
            true => PeselGender::Female,
            false => PeselGender::Male
        }
    }

    /// Returns fully formatted date of birth.
    /// The date is returned in YYYY-MM-DD format
    pub fn date_of_birth(&self) -> String {
        let century:u16 = match self.mob {
            0...12 => 1900,
            20...32 => 2000,
            40...52 => 2100,
            60...72 => 2200,
            80...92 => 1800,
            _ => panic!("invalid PESEL")
        };
        let year :u16 = self.yob as u16 + century;
        let month = self.mob;
        let day = self.dob;

        format!("{}-{:02}-{:02}", year, month, day)
    }

    // Returns description of a biological gender of a person assigned PESEL number
    pub fn gender_name(&self) -> String {
        match self.is_female() {
            true => format!("female"),
            false => format!("male")
        }
    }
}

#[cfg(test)]
mod pesel_validator_tests {
    use std::str::FromStr;
    use crate::pesel::PeselGender;

    #[test]
    fn building_pesel_from_string() {
        let pesel_input = "44051401458";

        let pesel = super::PESEL::from_str(pesel_input).unwrap();
        assert_eq!( pesel.raw, pesel_input);
        assert_eq!( pesel.yob, 44);
        assert_eq!( pesel.mob, 05);
        assert_eq!( pesel.dob, 14);
    }

    #[test]
    fn check_if_is_male() {
        let pesel = super::PESEL::from_str("44051401458").unwrap();

        assert_eq!(false, pesel.is_female());
        assert_eq!(true, pesel.is_male());
        assert_eq!("male", pesel.gender_name());
        assert_eq!(super::PeselGender::Male, pesel.gender_type());
    }

    #[test]
    fn check_if_is_female() {
        let pesel = super::PESEL::from_str("44051401468").unwrap();

        assert_eq!(false, pesel.is_male());
        assert_eq!(true, pesel.is_female());
        assert_eq!("female", pesel.gender_name());
        assert_eq!(super::PeselGender::Female, pesel.gender_type());
    }

    #[test]
    fn zero_length_string_should_fail() {
        let pesel = super::PESEL::from_str("");

        assert_eq!(true, pesel.is_err());
        // TODO: implement std::cmp::PartialEq, for comparing like below
//        assert_eq!(super::PESELParsingError::new("PESEL has to be of 11 chars long"), pesel);
    }

    #[test]
    fn proper_pesel_should_be_validated() {
        let pesel = super::PESEL::from_str("44051401458").unwrap();

        assert_eq!(true, pesel.is_valid());
    }

    #[test]
    fn invalid_pesel_should_not_be_validated() {
        let pesel = super::PESEL::from_str("44051401459").unwrap();

        assert_eq!(false, pesel.is_valid());
    }

    #[test]
    fn pesel_may_only_contain_digits() {
        let pesel = super::PESEL::from_str("4405140145a");

        assert_eq!(true, pesel.is_err());
        // TODO: check if Error contains expected message
//        assert_eq!((pesel.expect_err("PESEL may only contain digits!"));
    }

    #[test]
    fn pesel_should_have_proper_century_coded() {
        let pesel = super::PESEL::from_str("44951201458");

        assert_eq!(true, pesel.is_err());
        // TODO: check if Error contains expected message
    }

    #[test]
    fn birth_day_should_not_exceed_31() {
        let pesel = super::PESEL::from_str("44053201458");

        assert_eq!(true, pesel.is_err());
        // TODO: check if Error contains expected message
    }

    #[test]
    fn birth_date_should_be_returned_as_ddmmyyyy() {
        let pesel = super::PESEL::from_str("44051401458").unwrap();

        assert_eq!("1944-05-14", pesel.date_of_birth());
    }

    #[test]
    fn additional_test() {
        let pesel = super::PESEL::from_str("44051401459");
        let result = match pesel {
            Ok(t) => Some(t),
            Err(_e) => None,
        };

        assert_eq!(false, result.unwrap().is_valid());
    }

    #[test]
    fn generated_pesel_should_be_valid() {
        let should_be_female = true;
        let pesel = super::PESEL::new(1981, 06, 27, PeselGender::Female);

        println!("pesel.checksum: {}", pesel.checksum);
        assert_eq!(true, pesel.is_valid());
        assert_eq!(should_be_female, pesel.is_female());
//        assert_eq!(false, pesel.is_male());
//        assert_eq!(true, pesel.is_female());
    }

    #[test]
    fn generated_pesel_should_have_proper_gender_set() {
        let should_be_female = true;
        let pesel = super::PESEL::new(1981, 06, 27, PeselGender::Female);

        assert_eq!(should_be_female, pesel.is_female());
        assert_eq!("female", pesel.gender_name());
    }

    #[test]
    fn generated_pesel_should_have_proper_gender_set2() {
        let should_be_female = false;
        let pesel = super::PESEL::new(1981, 06, 27, PeselGender::Male);

        assert_eq!(should_be_female, pesel.is_female());
        assert_eq!("male", pesel.gender_name());
    }

    #[test]
    fn generated_pesel_should_print_proper_birth_date() {
        let pesel = super::PESEL::new(1981, 06, 27, PeselGender::Female);

        assert_eq!("1981-06-27", pesel.date_of_birth());
    }

    #[test]
    fn check_for_add_with_overflow() {
        // This test is very specific. It makes sure, that generated pesel, containing many high values (digits) will not result in overflow when calculating checksum
        let year = 2299;
        let month = 12;
        let day = 31;

        let pesel = super::PESEL::new(year, month, day, PeselGender::Male);

        assert_eq!(true, pesel.is_valid());
    }
}