Skip to main content

parse_fr_nir

Function parse_fr_nir 

Source
pub fn parse_fr_nir(s: &str) -> Option<String>
Expand description

Parse a France NIR (Numéro d’Inscription au Répertoire).

The NIR — also known as the INSEE number or Numéro de Sécurité Sociale — is France’s national social-security identifier and the de-facto unique healthcare identifier. Its structure is:

S YY MM DD CCC NNN KK
│ │  │  │  │   │   └─ 2-digit check key (Mod-97)
│ │  │  │  │   └───── 3-digit municipal birth-order number
│ │  │  │  └───────── 3-digit commune code
│ │  │  └──────────── 2-digit département (or "2A"/"2B" for Corsica)
│ │  └─────────────── 2-digit month of birth
│ └────────────────── 2-digit year of birth
└──────────────────── sex (1=male, 2=female, plus special values)

Total length is exactly 15 characters. The check key K satisfies K = 97 - (N mod 97), where N is the 13-digit body. For Corsica, the department letters are remapped before computing N: "2A" → "19", "2B" → "18".

Whitespace in the input is stripped before parsing, so the formal layout "1 80 12 75 123 456 42" and the compact "180127512345642" both canonicalise to the same 15-character upper-case string.

§Examples

A canonical, syntactically valid NIR round-trips:

use worker_matcher::identifiers::parse_fr_nir;

// 13-digit body with department 75 (Paris), key computed as 97 - (N mod 97).
let valid = "180127512345642";
assert_eq!(parse_fr_nir(valid), Some(valid.to_string()));

Whitespace is tolerated:

assert_eq!(
    parse_fr_nir("1 80 12 75 123 456 42"),
    Some("180127512345642".to_string()),
);

An invalid check key rejects:

assert_eq!(parse_fr_nir("180127512345699"), None);  // wrong key
assert_eq!(parse_fr_nir("12345"),           None);  // wrong length
assert_eq!(parse_fr_nir(""),                None);