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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
use crate::pesel_parsing_error::PeselError;
use std::str::FromStr;

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

const PESEL_LENGTH: usize = 11;

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

impl std::fmt::Display for PeselGender {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let gender_name = match *self {
            PeselGender::Female => "female",
            PeselGender::Male => "male",
        };
        write!(f, "{}", gender_name)
    }
}

#[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:     PeselGender,        // biological gender
    checksum:   u8,                 // checksum used for validation
    is_valid:   bool,               // true if checksum == algorithmic PESEL validation?
}


impl PESEL {
    /// Tries to create new PESEL strucutre based on:
    /// - birth date (could be in the future!)
    /// - biological gender
    ///
    /// Returns Result<PESEL, PeselError>
    /// When PeselError is returned it is mainly due to the fact that provided date of birth is invalid: for example 30th of February, 31st of April etc., or date is out range for PESEL (earlier than 1800 or after 2299)
    ///
    /// Example:
    /// ```rust
    /// use pesel::pesel::{PESEL as PESEL, PeselGender};
    ///
    /// // some code here...
    ///
    /// let result = PESEL::new(1981, 05, 29, PeselGender::Female);
    /// match result {
    ///     Ok(pesel) => println!("generated PESEL: {}", pesel),
    ///     _ => println!("unable to create PESEL for specified date"),
    /// }
    /// ```
    /// Returned PESEL structure is valid (i.e. passes validation algorithm check - `new_pesel.is_valid` should always return `true`
    pub fn new(year: u16, month: u8, day: u8, pesel_gender: PeselGender) -> Result<PESEL, PeselError> {

        if ! PESEL::is_date_in_range(year as i32) {
            return Err(PeselError::new(PeselError::DoBOutOfRange));
        }
        if ! PESEL::is_valid_date( year as i32, month as u32, day as u32) {
            return Err(PeselError::new(PeselError::InvalidDoB));
        }

        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())
    }
}

impl FromStr for PESEL {
    type Err =  PeselError;

    /// 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 `PeselError` 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
    /// - birth date is incorrect (i.e. 30th of February, 31st of April...
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.len() != PESEL_LENGTH {
            return Err(PeselError::new(PeselError::SizeError));
        }
        if s.chars().any(|f| !f.is_ascii_digit()) {
            return Err(PeselError::new(PeselError::BadFormat));
        }
        // do not automatically validate PESEL struct and return Err if it doesn't pass validation check. Some PESEL numbers in Poland (still in use) have been generated incorrectly (probably database with exceptions is used).
        let checksum = s[10..11].parse::<u8>().unwrap();
        let gender  = s[9..10].parse::<u8>().unwrap();

        let yob = s[0..2].parse::<u8>().unwrap();
        let mob = s[2..4].parse::<u8>().unwrap();
        let dob = s[4..6].parse::<u8>().unwrap();

        let real_year = PESEL::calc_year_from_pesel_encoded_month_and_year(yob, mob);
        if ! PESEL::is_date_in_range(real_year) {
            return Err(PeselError::new(PeselError::DoBOutOfRange));
        }
        if ! PESEL::is_valid_date( real_year, (mob % 20) as u32, dob as u32) {
            return Err(PeselError::new(PeselError::InvalidDoB));
        }

        let calculated_checksum = PESEL::calc_checksum_from_pesel_string(&s);
        let pesel_is_valid = calculated_checksum == checksum;

        let real_gender = match gender %2 == 0 {
            true => PeselGender::Female,
            false => PeselGender::Male,
        };

        Ok(PESEL{
            raw: s.clone().to_string(),
            yob,
            mob,
            dob,
            gender: real_gender,
            checksum,
            is_valid: pesel_is_valid,
        })
    }
}

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 - checks if date is within PESEL system range
    fn is_date_in_range(year: i32) -> bool {
        match year < 1800 || year > 2299 {
            true => false,
            false => true
        }
    }

    /// Utility function - checks if date is valid
    fn is_valid_date(year: i32, month: u32, day: u32) -> bool {
        use chrono::prelude::*;
        let date = Local.ymd_opt(year, month, day);

        date != chrono::offset::LocalResult::None
    }

    /// 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
    }

    fn calc_year_from_pesel_encoded_month_and_year(year: u8, month: u8) -> i32 {
        year as i32 + match month {
            1..=12 => 1900,
            20..=32 => 2000,
            40..=52 => 2100,
            60..=72 => 2200,
            80..=92 => 1800,
            _ => 0,
        }
    }

    /// 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 + d + 9*e + 7*f + 3*g + h + 9*i + 7*j
    /// 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.
    /// Note: this value is precomputed
    pub fn is_valid(&self) -> bool {
        self.is_valid
    }

    /// Returns biological gender as PeselGender enum
    pub fn gender(&self) -> PeselGender {
        self.gender
    }

    /// Returns date of birth as chrono::Date
    pub fn date_of_birth(&self) -> chrono::Date<chrono::Local> {
        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;

        use chrono::prelude::*;
        Local.ymd_opt(year as i32, month as u32, day as u32).unwrap()
    }

    // Returns description of a biological gender of a person assigned PESEL number
    pub fn gender_name(&self) -> String {
        self.gender().to_string()
    }

    pub fn pesel_number(&self) -> String {
        self.raw.clone()
    }
}
#[cfg(test)]
mod pesel_parsing_tests {
    use std::str::FromStr;
    use crate::pesel_parsing_error::PeselError;

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

        assert_eq!(true, pesel.is_err());
        assert_eq!(super::PeselError::new(PeselError::SizeError), pesel.err().unwrap());
    }

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

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::BadFormat), pesel.unwrap_err());
    }

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

        assert_eq!(true, pesel.is_err());
        assert_eq!(super::PeselError::new(PeselError::SizeError), pesel.err().unwrap());
    }

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

        assert_eq!(true, pesel.is_err());
        assert_eq!(super::PeselError::new(PeselError::SizeError), pesel.err().unwrap());
    }
}

#[cfg(test)]
mod pesel_base_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!("male", pesel.gender_name());
        assert_eq!(super::PeselGender::Male, pesel.gender());
    }

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

        assert_eq!("female", pesel.gender_name());
        assert_eq!(super::PeselGender::Female, pesel.gender());
    }

    #[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_from_number_that_fails_on_checksum_should_have_is_valid_set_to_false() {
        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 pesel = super::PESEL::new(1981, 06, 27, PeselGender::Female).unwrap();

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

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

        assert_eq!("female", pesel.gender_name());
        assert_eq!(PeselGender::Female, pesel.gender());
        assert_ne!("male", pesel.gender_name());
        assert_ne!(PeselGender::Male, pesel.gender());
    }

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

        assert_eq!("male", pesel.gender_name());
        assert_eq!(PeselGender::Male, pesel.gender());
        assert_ne!("female", pesel.gender_name());
        assert_ne!(PeselGender::Female, pesel.gender());
    }

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

        assert_eq!(input.to_string(), pesel.pesel_number());
    }
}

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

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

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::DoBOutOfRange), pesel.unwrap_err());
    }

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

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::InvalidDoB), pesel.unwrap_err());
    }

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

        assert_eq!("1944-05-14", pesel.date_of_birth().format("%Y-%m-%d").to_string());
    }
    #[test]
    fn generated_pesel_should_print_proper_birth_date() {
        let pesel = super::PESEL::new(1981, 06, 27, PeselGender::Female).unwrap();

        assert_eq!("1981-06-27", pesel.date_of_birth().format("%Y-%m-%d").to_string());
    }

    #[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).unwrap();

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

    #[test]
    fn creating_pesel_from_invalid_date_should_result_in_error() {
        // 1993 for sure was not a leap year...
        let pesel = super::PESEL::new(1993, 02, 29, PeselGender::Female);

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::InvalidDoB), pesel.err().unwrap());
    }

    #[test]
    fn parsing_pesel_from_invalid_date_should_result_in_error() {
        // 1993 for sure was not a leap year...
        let pesel = super::PESEL::from_str("83022998790");

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::InvalidDoB), pesel.err().unwrap());
    }

    #[test]
    fn creating_pesel_with_32nd_day_of_month_should_result_in_error() {
        let pesel = super::PESEL::new(1982, 05, 32, PeselGender::Male);

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::InvalidDoB), pesel.err().unwrap());
    }

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

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::InvalidDoB), pesel.err().unwrap());
    }

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

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::InvalidDoB), pesel.err().unwrap());
    }

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

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::InvalidDoB), pesel.err().unwrap());
    }

    #[test]
    fn creating_pesel_from_date_earlier_than_1800y_should_result_in_error() {
        let pesel = super::PESEL::new(1799, 02, 06, PeselGender::Female);

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::DoBOutOfRange), pesel.err().unwrap());
    }

    #[test]
    fn creating_pesel_from_date_after_2299y_should_result_in_error() {
        let pesel = super::PESEL::new(2300, 01, 01, PeselGender::Female);

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::DoBOutOfRange), pesel.err().unwrap());
    }

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

        assert_eq!(true, pesel.is_err());
        assert_eq!(PeselError::new(PeselError::DoBOutOfRange), pesel.err().unwrap());
    }
}