worker-matcher 0.3.0

Worker matcher for healthcare information exchange: deterministic and probabilistic matching with multinational national identifiers (UK NHS / FR NIR / ES TSI / IE IHI / UK NI H&C / US SSN), E.164 phone normalisation, address parsing, nickname dictionary, email scoring, and explainable per-field breakdowns.
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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
//! National healthcare identifier parsing and validation.
//!
//! This module exposes parsers for the national-level healthcare identifiers
//! that the crate compares deterministically and probabilistically.
//!
//! Function names follow the convention `parse_<cc>_<scheme>`, where `<cc>`
//! is the ISO 3166-1 alpha-2 country code (lower-cased) and `<scheme>` is
//! the short identifier name. This keeps related schemes alphabetised within
//! a country, and makes new countries easy to slot in.
//!
//! | Jurisdiction | Identifier | Parser |
//! |---|---|---|
//! | United Kingdom — England, Wales, Isle of Man | NHS Number | [`parse_uk_nhs_number`] |
//! | France | NIR (*Numéro d'Inscription au Répertoire*) | [`parse_fr_nir`] |
//! | España (Spain) | TSI (*Tarjeta Sanitaria Individual*) / CIP-SNS | [`parse_es_tsi`] |
//! | Éire (Ireland) | IHI (Individual Health Identifier) | [`parse_ie_ihi`] |
//! | United Kingdom — Northern Ireland | H&C Number (Health and Care Number) | [`parse_uk_hc_number`] |
//! | United Kingdom — Scotland | CHI (Community Health Index) | [`parse_uk_chi_number`] |
//! | United Kingdom | NINO (National Insurance Number) | [`parse_uk_nino`] |
//! | United States | SSN (Social Security Number) | [`parse_us_ssn`] |
//! | Germany | KVNR (Krankenversichertennummer) | [`parse_de_kvnr`] |
//! | Italy | *Codice Fiscale* (CF) | [`parse_it_cf`] |
//! | Netherlands | BSN (*Burgerservicenummer*) | [`parse_nl_bsn`] |
//! | Sweden | *Workernummer* | [`parse_se_workernummer`] |
//! | Australia | IHI (Individual Healthcare Identifier) | [`parse_au_ihi`] |
//! | Belgium | National Number (*Rijksregisternummer*) | [`parse_be_nn`] |
//! | Bulgaria | EGN (*Edinen grazhdanski nomer*) | [`parse_bg_egn`] |
//! | Czech Republic | *Rodné číslo* | [`parse_cz_rc`] |
//! | Denmark | CPR (*Centrale Workerregister*) | [`parse_dk_cpr`] |
//! | Estonia | *Isikukood* | [`parse_ee_ik`] |
//! | España (Spain) | DNI/NIE | [`parse_es_dni`] |
//! | Finland | HETU (*Henkilötunnus*) | [`parse_fi_hetu`] |
//! | Croatia | OIB (*Osobni identifikacijski broj*) | [`parse_hr_oib`] |
//! | Iceland | *Kennitala* | [`parse_is_kt`] |
//! | Lithuania | *Asmens kodas* | [`parse_lt_ak`] |
//! | Latvia | *Workeras kods* | [`parse_lv_pk`] |
//! | Malta | National ID | [`parse_mt_id`] |
//! | Norway | *Fødselsnummer* | [`parse_no_fnr`] |
//! | Poland | PESEL | [`parse_pl_pesel`] |
//! | Romania | CNP (*Cod Numeric Workeral*) | [`parse_ro_cnp`] |
//! | Slovenia | EMŠO (*Enotna Matična Številka Občana*) | [`parse_si_emso`] |
//! | Slovakia | *Rodné číslo* | [`parse_sk_rc`] |
//! | Greece | DSS investor share | [`parse_gr_dss`] |
//! | Liechtenstein | National Identity Card Number | [`parse_li_id`] |
//! | Netherlands | National Identity Card Number | [`parse_nl_id`] |
//! | Poland | NIP (*Numer Identyfikacji Podatkowej*) | [`parse_pl_nip`] |
//! | Portugal | NIF (*Número de Identificação Fiscal*) | [`parse_pt_nif`] |
//! | Brazil | CPF (*Cadastro de Pessoas Físicas*) | [`parse_br_cpf`] |
//! | China | RRN (*居民身份证*) 18-digit | [`parse_cn_rrn`] |
//! | India | Aadhaar | [`parse_in_aadhaar`] |
//! | Japan | My Number (*個人番号*) | [`parse_jp_my_number`] |
//! | Mexico | CURP (*Clave Única de Registro de Población*) | [`parse_mx_curp`] |
//! | New Zealand | NHI (National Health Index) — original 7-char form | [`parse_nz_nhi`] |
//! | South Africa | ID Number | [`parse_za_id`] |
//!
//! ## Passport-number format validators
//!
//! Passport book numbers are not stable across renewals, and a worker
//! may hold passports from several countries simultaneously — see
//! [`crate::PassportBook`] for the canonical multi-country, multi-book,
//! time-varying model used by the matcher. The following per-country
//! parsers are pure **format validators** that consumers can call before
//! constructing a `PassportBook` (or as a smell test in their own
//! ingestion code). They do NOT have a corresponding `Worker` field;
//! they exist so a country-specific passport number can be canonicalised
//! and rejected at the system boundary.
//!
//! | Jurisdiction | Format | Parser |
//! |---|---|---|
//! | Cyprus | `E` + 6 digits (pre-2010) or `K` + 8 digits | [`parse_cy_passport`] |
//! | Czech Republic | 8 to 12 digits | [`parse_cz_passport`] |
//! | Liechtenstein | 1 letter + 5 digits | [`parse_li_passport`] |
//! | Lithuania | 8 digits | [`parse_lt_passport`] |
//! | Malta | 7 digits | [`parse_mt_passport`] |
//! | Netherlands | same shape as the NL ID card | [`parse_nl_passport`] |
//! | Portugal | 1 letter + 6 digits | [`parse_pt_passport`] |
//! | Romania | 2 letters + 6 digits | [`parse_ro_passport`] |
//! | Slovakia | 2 letters + 7 digits | [`parse_sk_passport`] |
//!
//! Each parser takes a `&str` and returns `Option<String>`:
//!
//! - `Some(canonical)` — the input parses for the identifier scheme. The
//!   returned string is a canonical form (whitespace stripped, letters
//!   uppercased) suitable for byte-equality comparison.
//! - `None` — the input fails the scheme's structural or check-digit test.
//!
//! Two inputs that represent the same identifier in different textual
//! layouts always canonicalise to the same string. Consumers compare the
//! canonical forms for equality; the matching engine does exactly this.
//!
//! ## Design notes
//!
//! - Parsing is **format-only** unless the scheme has an integral check
//!   digit (NIR has a Modulus-97 key; H&C / NHS structurally accept any
//!   10-digit number through the `nhs-number` crate's `FromStr`).
//! - These parsers do not consult external registries; they verify only
//!   what can be derived from the identifier's own structure.
//! - Country-specific semantic ranges (e.g. valid French department codes,
//!   valid Spanish autonomous-community prefixes) are deliberately NOT
//!   enforced to avoid rejecting edge-case-but-legitimate values.
//!
//! ## Example
//!
//! ```
//! use worker_matcher::identifiers;
//!
//! // UK NHS Number — accepts the canonical "XXX XXX XXXX" layout.
//! assert_eq!(
//!     identifiers::parse_uk_nhs_number("943 476 5919"),
//!     Some("9434765919".to_string()),
//! );
//!
//! // Anything that does not match the NHS layout returns None.
//! assert_eq!(identifiers::parse_uk_nhs_number("not-a-number"), None);
//! ```

use nhs_number::NHSNumber;
use std::str::FromStr;

/// Parse a United Kingdom NHS Number (England, Wales, Isle of Man).
///
/// Wraps [`nhs_number::NHSNumber::from_str`], which accepts the 10-digit
/// compact layout (`"9434765919"`) and the spaced layout (`"943 476 5919"`).
/// On success, the canonical 10-digit form is returned.
///
/// The NHS Number applies to England, Wales, and the Isle of Man. Northern
/// Ireland uses a separate H&C Number that follows the same Modulus-11
/// algorithm — see [`parse_uk_hc_number`].
///
/// # Examples
///
/// ```
/// use worker_matcher::identifiers::parse_uk_nhs_number;
///
/// assert_eq!(parse_uk_nhs_number("9434765919"),   Some("9434765919".to_string()));
/// assert_eq!(parse_uk_nhs_number("943 476 5919"), Some("9434765919".to_string()));
/// assert_eq!(parse_uk_nhs_number("ABCDEFGHIJ"),   None);
/// assert_eq!(parse_uk_nhs_number("123"),          None);
/// ```
pub fn parse_uk_nhs_number(s: &str) -> Option<String> {
    let parsed = NHSNumber::from_str(s).ok()?;
    let mut canonical = String::with_capacity(10);
    for &d in &parsed.digits {
        canonical.push(char::from_digit(d as u32, 10)?);
    }
    Some(canonical)
}

/// 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:
///
/// ```text
/// 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:
///
/// ```
/// # use worker_matcher::identifiers::parse_fr_nir;
/// assert_eq!(
///     parse_fr_nir("1 80 12 75 123 456 42"),
///     Some("180127512345642".to_string()),
/// );
/// ```
///
/// An invalid check key rejects:
///
/// ```
/// # use worker_matcher::identifiers::parse_fr_nir;
/// assert_eq!(parse_fr_nir("180127512345699"), None);  // wrong key
/// assert_eq!(parse_fr_nir("12345"),           None);  // wrong length
/// assert_eq!(parse_fr_nir(""),                None);
/// ```
pub fn parse_fr_nir(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| !c.is_whitespace())
        .collect::<String>()
        .to_uppercase();

    if !cleaned.is_ascii() || cleaned.len() != 15 {
        return None;
    }

    let dept = &cleaned[5..7];
    let numeric_body = match dept {
        "2A" => format!("{}19{}", &cleaned[0..5], &cleaned[7..13]),
        "2B" => format!("{}18{}", &cleaned[0..5], &cleaned[7..13]),
        _ => cleaned[0..13].to_string(),
    };

    if !numeric_body.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }
    let key_str = &cleaned[13..15];
    if !key_str.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }

    let n: u64 = numeric_body.parse().ok()?;
    let key: u64 = key_str.parse().ok()?;

    if 97 - (n % 97) == key {
        Some(cleaned)
    } else {
        None
    }
}

/// Parse a España (Spain) TSI (*Tarjeta Sanitaria Individual*) / CIP-SNS identifier.
///
/// Spain's healthcare identification is fragmented across 17 autonomous
/// communities, each of which issues its own TSI card with a region-specific
/// format. The national-level *Código de Identificación Workeral del Sistema
/// Nacional de Salud* (CIP-SNS) provides a uniform 16-character code with
/// the canonical structure `LLLLDDDDDDXXXXXX` (4 letters + 6 digits + 6
/// alphanumerics), but regional formats are also encountered in practice.
///
/// To accept the full population of legitimate identifiers without
/// privileging any region, this parser is **format-only** and lenient:
///
/// 1. Whitespace and ASCII hyphens are stripped.
/// 2. Letters are uppercased.
/// 3. The remaining string must contain only ASCII alphanumerics.
/// 4. The length must be in `10..=20`.
///
/// No check-digit calculation is performed because the schemes vary by
/// community. A consumer that needs stronger validation should layer a
/// community-specific check on top of this canonical form.
///
/// # Examples
///
/// ```
/// use worker_matcher::identifiers::parse_es_tsi;
///
/// // 16-character CIP-SNS national code:
/// assert_eq!(
///     parse_es_tsi("ABCD123456XY1234"),
///     Some("ABCD123456XY1234".to_string()),
/// );
///
/// // Whitespace and hyphens are stripped, letters uppercased:
/// assert_eq!(
///     parse_es_tsi("abcd 123 456-xy1234"),
///     Some("ABCD123456XY1234".to_string()),
/// );
///
/// // Too short, too long, or containing non-alphanumerics rejects:
/// assert_eq!(parse_es_tsi("ABC123"),                 None);  // 6 chars
/// assert_eq!(parse_es_tsi("ABCDEF123456XY12345678"), None);  // 22 chars
/// assert_eq!(parse_es_tsi("ABC@123!XYZ"),            None);  // bad chars
/// ```
pub fn parse_es_tsi(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| !c.is_whitespace() && *c != '-')
        .collect::<String>()
        .to_uppercase();

    if !cleaned.is_ascii() {
        return None;
    }
    if !cleaned.chars().all(|c| c.is_ascii_alphanumeric()) {
        return None;
    }
    if !(10..=20).contains(&cleaned.len()) {
        return None;
    }
    Some(cleaned)
}

/// Parse an Éire (Ireland) IHI (Individual Health Identifier).
///
/// Under the Health Identifiers Act 2014, every individual receiving
/// healthcare in Ireland is assigned a 7-digit IHI by the Health Identifiers
/// Service. The IHI is the unique national healthcare identifier in the
/// Republic of Ireland.
///
/// Parsing rules:
///
/// 1. All non-digit characters are stripped (spaces, hyphens, etc.).
/// 2. The remaining string must contain exactly 7 ASCII digits.
///
/// No check-digit algorithm is enforced (none is publicly specified). The
/// canonical form is the 7-digit string.
///
/// # Examples
///
/// ```
/// use worker_matcher::identifiers::parse_ie_ihi;
///
/// assert_eq!(parse_ie_ihi("1234567"),    Some("1234567".to_string()));
/// assert_eq!(parse_ie_ihi("123 4567"),   Some("1234567".to_string()));
/// assert_eq!(parse_ie_ihi("123-45-67"),  Some("1234567".to_string()));
///
/// assert_eq!(parse_ie_ihi("12345"),      None);   // too short
/// assert_eq!(parse_ie_ihi("12345678"),   None);   // too long
/// assert_eq!(parse_ie_ihi("ABCDEFG"),    None);   // not digits
/// ```
pub fn parse_ie_ihi(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() == 7 {
        Some(digits)
    } else {
        None
    }
}

/// Parse a United Kingdom Northern Ireland H&C (Health and Care) Number.
///
/// The H&C Number is Northern Ireland's national healthcare identifier,
/// issued by HSC (Health and Social Care). Structurally it is a 10-digit
/// number with a Modulus-11 check digit — the same algorithm used by the
/// UK NHS Number.
///
/// This parser delegates to the same logic as [`parse_uk_nhs_number`]: it
/// accepts either the compact 10-digit form or the spaced
/// `"XXX XXX XXXX"` form and returns the canonical 10-digit string.
///
/// The two parsers are intentionally separate so that callers track *which*
/// scheme an identifier belongs to: a number that parses successfully as
/// both an NHS Number and an H&C Number still refers to two distinct people
/// in two distinct registries.
///
/// # Examples
///
/// ```
/// use worker_matcher::identifiers::parse_uk_hc_number;
///
/// assert_eq!(parse_uk_hc_number("9434765919"),   Some("9434765919".to_string()));
/// assert_eq!(parse_uk_hc_number("943 476 5919"), Some("9434765919".to_string()));
/// assert_eq!(parse_uk_hc_number("not-a-number"), None);
/// ```
pub fn parse_uk_hc_number(s: &str) -> Option<String> {
    parse_uk_nhs_number(s)
}

/// Parse a United States Social Security Number (SSN).
///
/// The SSN is the United States' de-facto national identifier — a 9-digit
/// number assigned by the Social Security Administration, conventionally
/// formatted as `"AAA-GG-SSSS"`:
///
/// ```text
/// AAA  - GG - SSSS
/// │      │    └──── Serial Number (4 digits, 0001..=9999)
/// │      └───────── Group Number  (2 digits, 01..=99)
/// └──────────────── Area Number   (3 digits, 001..=665, 667..=899)
/// ```
///
/// Parsing rules:
///
/// 1. Keep only ASCII digits (strip whitespace, hyphens, periods,
///    parentheses, …).
/// 2. Reject unless the result has exactly 9 digits.
/// 3. Reject structurally-impossible area numbers (`000`, `666`, and
///    `900..=999`). These have never been assigned by SSA.
/// 4. Reject group `00`.
/// 5. Reject serial `0000`.
///
/// Since SSA introduced randomised assignment in June 2011, the area
/// number no longer encodes the state of issuance, so no geographic
/// validation is attempted. The canonical form is the 9-digit compact
/// string `"AAAGGSSSS"`.
///
/// # Examples
///
/// Three textual layouts of the same SSN canonicalise identically:
///
/// ```
/// use worker_matcher::identifiers::parse_us_ssn;
///
/// assert_eq!(parse_us_ssn("123-45-6789"), Some("123456789".to_string()));
/// assert_eq!(parse_us_ssn("123 45 6789"), Some("123456789".to_string()));
/// assert_eq!(parse_us_ssn("123456789"),   Some("123456789".to_string()));
/// ```
///
/// Structurally-invalid values are rejected:
///
/// ```
/// # use worker_matcher::identifiers::parse_us_ssn;
/// assert_eq!(parse_us_ssn("000-12-3456"), None); // area 000 never issued
/// assert_eq!(parse_us_ssn("666-12-3456"), None); // area 666 never issued
/// assert_eq!(parse_us_ssn("900-12-3456"), None); // area 900..=999 never issued
/// assert_eq!(parse_us_ssn("123-00-4567"), None); // group 00 invalid
/// assert_eq!(parse_us_ssn("123-45-0000"), None); // serial 0000 invalid
/// assert_eq!(parse_us_ssn("12345"),       None); // too short
/// assert_eq!(parse_us_ssn("ABCDEFGHI"),   None); // not digits
/// assert_eq!(parse_us_ssn(""),            None);
/// ```
pub fn parse_us_ssn(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 9 {
        return None;
    }
    let area: u32 = digits[0..3].parse().ok()?;
    let group: u32 = digits[3..5].parse().ok()?;
    let serial: u32 = digits[5..9].parse().ok()?;
    if area == 0 || area == 666 || area >= 900 {
        return None;
    }
    if group == 0 {
        return None;
    }
    if serial == 0 {
        return None;
    }
    Some(digits)
}

/// Parse a Germany KVNR (*Krankenversichertennummer*).
///
/// The KVNR is the lifelong health-insurance number printed on the
/// German electronic health card (*elektronische Gesundheitskarte*).
/// Structure: 10 characters total, one uppercase letter followed by 9
/// digits. The final digit is a Mod-10 check digit.
///
/// Check-digit algorithm:
///
/// 1. Map the leading letter to a two-digit ordinal (`A=01`, `B=02`,
///    …, `Z=26`).
/// 2. Concatenate that 2-digit value with positions 2..=9 of the KVNR
///    (the 8 digits before the check digit) → a 10-digit string.
/// 3. Multiply each of those 10 digits by alternating weights
///    `1, 2, 1, 2, 1, 2, 1, 2, 1, 2`.
/// 4. For products `≥ 10`, replace with the digit sum (max product is
///    `9 × 2 = 18`, so subtract 9 to digit-sum).
/// 5. Sum all results; the check digit is `sum mod 10`.
///
/// Whitespace is stripped before parsing. The canonical form is the
/// 10-character uppercase string.
///
/// # Examples
///
/// ```
/// use worker_matcher::identifiers::parse_de_kvnr;
///
/// // Constructed valid KVNR (A=01; alternating Mod-10 yields check digit 0).
/// assert_eq!(parse_de_kvnr("A123456780"), Some("A123456780".to_string()));
/// assert_eq!(parse_de_kvnr("a123456780"), Some("A123456780".to_string()));  // lowercase letter accepted
///
/// // Wrong check digit:
/// assert_eq!(parse_de_kvnr("A123456789"), None);
///
/// // Wrong length / shape:
/// assert_eq!(parse_de_kvnr("1234567890"), None);    // no letter
/// assert_eq!(parse_de_kvnr("A12345"),     None);
/// assert_eq!(parse_de_kvnr(""),           None);
/// ```
pub fn parse_de_kvnr(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| !c.is_whitespace())
        .collect::<String>()
        .to_uppercase();
    if !cleaned.is_ascii() || cleaned.len() != 10 {
        return None;
    }
    let mut chars = cleaned.chars();
    let first = chars.next()?;
    if !first.is_ascii_alphabetic() {
        return None;
    }
    let digit_chars: Vec<char> = chars.collect();
    if !digit_chars.iter().all(|c| c.is_ascii_digit()) {
        return None;
    }
    let letter_ord = (first as u32) - ('A' as u32) + 1;
    let mut combined: Vec<u32> = vec![letter_ord / 10, letter_ord % 10];
    for c in &digit_chars[..8] {
        combined.push(c.to_digit(10)?);
    }
    let mut total: u32 = 0;
    for (i, d) in combined.iter().enumerate() {
        let weight = if i % 2 == 0 { 1 } else { 2 };
        let product = d * weight;
        total += if product >= 10 { product - 9 } else { product };
    }
    let expected = digit_chars[8].to_digit(10)?;
    if total % 10 == expected {
        Some(cleaned)
    } else {
        None
    }
}

/// Per-position lookup table for the Italy *Codice Fiscale* check
/// character.
///
/// "Odd" positions are the 1st, 3rd, 5th, …, 15th characters
/// (1-indexed); they map per a specific table that intentionally
/// scatters values so single-character typos are likely to shift the
/// resulting check character.
fn cf_odd_value(c: char) -> Option<u32> {
    Some(match c {
        '0' | 'A' => 1,
        '1' | 'B' => 0,
        '2' | 'C' => 5,
        '3' | 'D' => 7,
        '4' | 'E' => 9,
        '5' | 'F' => 13,
        '6' | 'G' => 15,
        '7' | 'H' => 17,
        '8' | 'I' => 19,
        '9' | 'J' => 21,
        'K' => 2,
        'L' => 4,
        'M' => 18,
        'N' => 20,
        'O' => 11,
        'P' => 3,
        'Q' => 6,
        'R' => 8,
        'S' => 12,
        'T' => 14,
        'U' => 16,
        'V' => 10,
        'W' => 22,
        'X' => 25,
        'Y' => 24,
        'Z' => 23,
        _ => return None,
    })
}

/// "Even" positions (2nd, 4th, …, 14th, 1-indexed) for the Italy
/// *Codice Fiscale* check character. Numeric values map to their digit
/// value; letters map to `A=0`, `B=1`, …, `Z=25`.
fn cf_even_value(c: char) -> Option<u32> {
    Some(match c {
        '0' | 'A' => 0,
        '1' | 'B' => 1,
        '2' | 'C' => 2,
        '3' | 'D' => 3,
        '4' | 'E' => 4,
        '5' | 'F' => 5,
        '6' | 'G' => 6,
        '7' | 'H' => 7,
        '8' | 'I' => 8,
        '9' | 'J' => 9,
        'K' => 10,
        'L' => 11,
        'M' => 12,
        'N' => 13,
        'O' => 14,
        'P' => 15,
        'Q' => 16,
        'R' => 17,
        'S' => 18,
        'T' => 19,
        'U' => 20,
        'V' => 21,
        'W' => 22,
        'X' => 23,
        'Y' => 24,
        'Z' => 25,
        _ => return None,
    })
}

/// Parse an Italy *Codice Fiscale* (CF).
///
/// The CF is a 16-character alphanumeric identifier issued by the
/// Italian tax authority and used as the de-facto national healthcare
/// identifier. It encodes a coded form of the holder's name, date of
/// birth, sex, and commune of birth, followed by a Mod-26 check
/// character.
///
/// Check-character algorithm:
///
/// 1. For each of the first 15 characters, compute a numeric value
///    using two lookup tables — "odd" positions (1, 3, 5, …, 15;
///    1-indexed) use the scattered table; "even" positions (2, 4, …,
///    14) map digits and letters to their natural value.
/// 2. Sum the 15 values, take mod 26.
/// 3. Map `0..=25` to `A..=Z`. The result MUST equal the 16th
///    character.
///
/// Whitespace is stripped and letters are uppercased before parsing.
/// The canonical form is the 16-character uppercase string.
///
/// # Examples
///
/// ```
/// use worker_matcher::identifiers::parse_it_cf;
///
/// // Synthetic CF with verified check character (sum 122, mod 26 = 18, 18→'S').
/// assert_eq!(
///     parse_it_cf("RSSMRA85T10A562S"),
///     Some("RSSMRA85T10A562S".to_string()),
/// );
/// // Lowercase and whitespace tolerated:
/// assert_eq!(
///     parse_it_cf("rss mra 85t 10a 562s"),
///     Some("RSSMRA85T10A562S".to_string()),
/// );
///
/// // Wrong check character:
/// assert_eq!(parse_it_cf("RSSMRA85T10A562X"), None);
///
/// // Wrong length:
/// assert_eq!(parse_it_cf("RSSMRA85T10A562"),  None);
/// assert_eq!(parse_it_cf("RSSMRA85T10A562SS"), None);
/// // Non-alphanumeric content:
/// assert_eq!(parse_it_cf("RSSMRA85T10A562!"), None);
/// assert_eq!(parse_it_cf(""),                  None);
/// ```
pub fn parse_it_cf(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| !c.is_whitespace())
        .collect::<String>()
        .to_uppercase();
    if !cleaned.is_ascii() || cleaned.len() != 16 {
        return None;
    }
    if !cleaned.chars().all(|c| c.is_ascii_alphanumeric()) {
        return None;
    }
    let chars: Vec<char> = cleaned.chars().collect();
    let mut total: u32 = 0;
    for (i, c) in chars.iter().take(15).enumerate() {
        // 1-indexed position parity: index 0 is position 1 (odd).
        let value = if i % 2 == 0 {
            cf_odd_value(*c)?
        } else {
            cf_even_value(*c)?
        };
        total += value;
    }
    let expected_check = (b'A' + (total % 26) as u8) as char;
    if chars[15] == expected_check {
        Some(cleaned)
    } else {
        None
    }
}

/// Parse a Netherlands BSN (*Burgerservicenummer*).
///
/// The BSN is a 9-digit citizen-service number used by all Dutch
/// authorities, including healthcare providers. It carries an
/// "11-test" check rule originally derived from the bank account
/// number validation.
///
/// Check rule (the "11-test"):
///
/// `9·d₁ + 8·d₂ + 7·d₃ + 6·d₄ + 5·d₅ + 4·d₆ + 3·d₇ + 2·d₈ − d₉ ≡ 0 (mod 11)`
///
/// Non-digit characters are stripped before validation (so spaces or
/// hyphens used for readability are tolerated). The all-zero string
/// `000000000` is rejected even though it satisfies the arithmetic.
/// The canonical form is the 9-digit compact string.
///
/// # Examples
///
/// ```
/// use worker_matcher::identifiers::parse_nl_bsn;
///
/// // 111222333: 9·1 + 8·1 + 7·1 + 6·2 + 5·2 + 4·2 + 3·3 + 2·3 − 3 = 66; 66 mod 11 = 0.
/// assert_eq!(parse_nl_bsn("111222333"), Some("111222333".to_string()));
/// assert_eq!(parse_nl_bsn("111 222 333"), Some("111222333".to_string()));
///
/// // Wrong check (final digit changed):
/// assert_eq!(parse_nl_bsn("111222334"), None);
///
/// // Wrong length, non-digits, all-zeros, empty:
/// assert_eq!(parse_nl_bsn("12345"),     None);
/// assert_eq!(parse_nl_bsn("ABCDEFGHI"), None);
/// assert_eq!(parse_nl_bsn("000000000"), None);
/// assert_eq!(parse_nl_bsn(""),          None);
/// ```
pub fn parse_nl_bsn(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 9 {
        return None;
    }
    if digits.chars().all(|c| c == '0') {
        return None;
    }
    let weights: [i32; 9] = [9, 8, 7, 6, 5, 4, 3, 2, -1];
    let mut sum: i32 = 0;
    for (i, c) in digits.chars().enumerate() {
        sum += (c.to_digit(10)? as i32) * weights[i];
    }
    if sum % 11 == 0 { Some(digits) } else { None }
}

/// Parse a Sweden *Workernummer*.
///
/// The Swedish workeral identity number is the national identifier
/// used for taxation, healthcare, banking, and similar purposes. It
/// comes in two textual layouts:
///
/// - 10-digit form: `YYMMDDNNNC` (or with a `-` / `+` separator
///   between the date and the serial, e.g. `460324-3850`). The `+`
///   separator indicates the holder is over 100 years old.
/// - 12-digit form: `YYYYMMDDNNNC` (or `19460324-3850`).
///
/// `Y`/`M`/`D` are the birth-date digits, `NNN` is a 3-digit serial
/// (odd = male, even = female under the historical convention), and
/// `C` is the Luhn check digit computed over the 10 digits of the
/// 10-digit form.
///
/// Non-digit characters are stripped before validation. The Luhn
/// check uses left-to-right weights `2, 1, 2, 1, 2, 1, 2, 1, 2, 1`;
/// products `≥ 10` are reduced by digit-sum; the total mod 10 must be
/// `0`.
///
/// The canonical form preserves the input length: 10-digit input
/// returns a 10-character string; 12-digit input returns a 12-character
/// string. Records using mixed layouts will not match deterministically
/// on this field, but they will still produce the correct Luhn
/// validation.
///
/// # Examples
///
/// ```
/// use worker_matcher::identifiers::parse_se_workernummer;
///
/// // Synthetic 10-digit workernummer with verified Luhn (sum 40, mod 10 = 0).
/// assert_eq!(
///     parse_se_workernummer("4603243850"),
///     Some("4603243850".to_string()),
/// );
/// assert_eq!(
///     parse_se_workernummer("460324-3850"),
///     Some("4603243850".to_string()),
/// );
///
/// // 12-digit form canonicalises with the century preserved.
/// assert_eq!(
///     parse_se_workernummer("19460324-3850"),
///     Some("194603243850".to_string()),
/// );
///
/// // Wrong Luhn:
/// assert_eq!(parse_se_workernummer("4603243851"), None);
///
/// // Wrong length, non-digits, empty:
/// assert_eq!(parse_se_workernummer("12345"),       None);
/// assert_eq!(parse_se_workernummer("ABCDEFGHIJ"),  None);
/// assert_eq!(parse_se_workernummer(""),            None);
/// ```
pub fn parse_se_workernummer(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    let luhn_digits: &str = match digits.len() {
        10 => &digits,
        12 => &digits[2..],
        _ => return None,
    };
    let mut sum: u32 = 0;
    for (i, c) in luhn_digits.chars().enumerate() {
        let d = c.to_digit(10)?;
        let weight = if i % 2 == 0 { 2 } else { 1 };
        let product = d * weight;
        sum += if product >= 10 { product - 9 } else { product };
    }
    if sum.is_multiple_of(10) {
        Some(digits)
    } else {
        None
    }
}

/// Parse an Australia IHI (Individual Healthcare Identifier).
///
/// The IHI is the unique 16-digit identifier issued by the Healthcare
/// Identifiers Service (HI Service) of the Australian Digital Health
/// Agency. It conforms to ISO/IEC 7812-1 with a Luhn check digit.
///
/// Non-digit characters are stripped before validation. The Luhn
/// check uses left-to-right weights `2, 1, 2, 1, …` over all 16
/// digits (the rightmost digit is the check); products `≥ 10` are
/// reduced by digit-sum; the total mod 10 must be `0`. The structural
/// convention that real IHIs begin with `800360` is **not** enforced
/// here so test and migration data with other prefixes parse cleanly.
///
/// # Examples
///
/// ```
/// use worker_matcher::identifiers::parse_au_ihi;
///
/// // Synthetic 16-digit IHI with verified Luhn.
/// assert_eq!(
///     parse_au_ihi("8003601234567894"),
///     Some("8003601234567894".to_string()),
/// );
/// assert_eq!(
///     parse_au_ihi("8003 6012 3456 7894"),
///     Some("8003601234567894".to_string()),
/// );
///
/// // Wrong Luhn / wrong length / non-digits:
/// assert_eq!(parse_au_ihi("8003601234567890"), None);
/// assert_eq!(parse_au_ihi("12345"),            None);
/// assert_eq!(parse_au_ihi("ABCDEFGHIJKLMNOP"), None);
/// assert_eq!(parse_au_ihi(""),                 None);
/// ```
pub fn parse_au_ihi(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 16 {
        return None;
    }
    let mut sum: u32 = 0;
    for (i, c) in digits.chars().enumerate() {
        let d = c.to_digit(10)?;
        let weight = if i % 2 == 0 { 2 } else { 1 };
        let product = d * weight;
        sum += if product >= 10 { product - 9 } else { product };
    }
    if sum.is_multiple_of(10) {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Scotland CHI (Community Health Index) Number.
///
/// The CHI Number is the unique worker identifier used by NHS
/// Scotland. Structure: 10 digits formatted `DDMMYYSSSC`, where
/// `DDMMYY` is the holder's date of birth, `SSS` is a 3-digit
/// sequence with the third digit encoding sex (odd = male, even =
/// female), and `C` is a Mod-11 check digit computed in the same
/// fashion as the UK NHS Number.
///
/// Check rule (Mod-11):
///
/// 1. Multiply each of the first 9 digits by the weights
///    `10, 9, 8, 7, 6, 5, 4, 3, 2`.
/// 2. Sum, take mod 11.
/// 3. The check digit is `(11 − (sum mod 11)) mod 11`. A computed
///    check of `10` indicates an invalid identifier and is rejected.
///
/// Non-digit characters are stripped before validation. The canonical
/// form is the 10-digit compact string. Although the NHS Number and
/// the CHI Number share the same Mod-11 algorithm, the two are
/// **scheme-local** in this crate and never cross-match (per spec
/// FR-13 / §12.1).
///
/// # Examples
///
/// ```
/// use worker_matcher::identifiers::parse_uk_chi_number;
///
/// // Synthetic CHI with verified Mod-11 (sum 74, check = 3).
/// assert_eq!(
///     parse_uk_chi_number("0101701233"),
///     Some("0101701233".to_string()),
/// );
/// assert_eq!(
///     parse_uk_chi_number("010 170 1233"),
///     Some("0101701233".to_string()),
/// );
///
/// // Wrong check / length / non-digits:
/// assert_eq!(parse_uk_chi_number("0101701234"), None);
/// assert_eq!(parse_uk_chi_number("12345"),      None);
/// assert_eq!(parse_uk_chi_number("ABCDEFGHIJ"), None);
/// assert_eq!(parse_uk_chi_number(""),           None);
/// ```
pub fn parse_uk_chi_number(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 10 {
        return None;
    }
    let chars: Vec<u32> = digits
        .chars()
        .map(|c| c.to_digit(10).expect("filtered to digits"))
        .collect();
    let weights = [10u32, 9, 8, 7, 6, 5, 4, 3, 2];
    let sum: u32 = chars
        .iter()
        .take(9)
        .zip(weights.iter())
        .map(|(d, w)| d * w)
        .sum();
    let check = (11 - (sum % 11)) % 11;
    if check == 10 {
        return None;
    }
    if check == chars[9] {
        Some(digits)
    } else {
        None
    }
}

// ----------------------------------------------------------------------------
// Additional national workeral identifiers (T-27).
//
// Each parser canonicalises whitespace + (where applicable) case, and verifies
// the scheme's check digit / check character. Parsers return Option<String>;
// `Some(canonical)` is suitable for byte-equality comparison.
// ----------------------------------------------------------------------------

/// Parse a Belgium *Rijksregisternummer* (National Number).
///
/// 11 digits: `YYMMDD` + 3-digit serial + 2-digit Mod-97 check.
/// Pre-2000 births: check = `97 − (first-9-digits mod 97)`.
/// 2000-and-later births: a `"2"` is prepended before the modulo step.
/// The parser tries both and accepts either.
///
/// ```
/// use worker_matcher::identifiers::parse_be_nn;
/// assert_eq!(parse_be_nn("80010100107"), Some("80010100107".to_string()));
/// assert_eq!(parse_be_nn("80.01.01-001.07"), Some("80010100107".to_string()));
/// assert_eq!(parse_be_nn("80010100100"), None);   // wrong check
/// assert_eq!(parse_be_nn("12345"), None);         // wrong length
/// ```
pub fn parse_be_nn(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 11 {
        return None;
    }
    let body: u64 = digits[..9].parse().ok()?;
    let check: u64 = digits[9..11].parse().ok()?;
    let pre2000 = 97 - body % 97;
    let post2000_body: u64 = format!("2{}", &digits[..9]).parse().ok()?;
    let post2000 = 97 - post2000_body % 97;
    if check == pre2000 || check == post2000 {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Bulgaria EGN (*Edinen grazhdanski nomer*).
///
/// 10 digits: `YYMMDD` (with month-offset for century) + 3-digit area/serial
/// + 1 check digit. Check uses weights `[2,4,8,5,10,9,7,3,6]` mod 11 (10 → 0).
///
/// ```
/// use worker_matcher::identifiers::parse_bg_egn;
/// assert_eq!(parse_bg_egn("8001010013"), Some("8001010013".to_string()));
/// assert_eq!(parse_bg_egn("8001010014"), None);
/// assert_eq!(parse_bg_egn(""), None);
/// ```
pub fn parse_bg_egn(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 10 {
        return None;
    }
    let weights: [u32; 9] = [2, 4, 8, 5, 10, 9, 7, 3, 6];
    let mut sum: u32 = 0;
    for (i, c) in digits.chars().take(9).enumerate() {
        sum += c.to_digit(10)? * weights[i];
    }
    let expected = if sum % 11 == 10 { 0 } else { sum % 11 };
    if digits.chars().nth(9)?.to_digit(10)? == expected {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Czech Republic *Rodné číslo*.
///
/// 9 or 10 digits. The 10-digit form (post-1953) is divisible by 11 (with
/// the edge case that mod-11 = 10 collapses to a trailing 0; the resulting
/// 10-digit number may NOT be divisible by 11). The 9-digit form (pre-1954)
/// is accepted as-is.
///
/// ```
/// use worker_matcher::identifiers::parse_cz_rc;
/// assert_eq!(parse_cz_rc("8001150014"), Some("8001150014".to_string()));
/// assert_eq!(parse_cz_rc("800115001"), Some("800115001".to_string())); // 9-digit pre-1954
/// assert_eq!(parse_cz_rc("8001150015"), None);
/// ```
pub fn parse_cz_rc(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    match digits.len() {
        9 => Some(digits),
        10 => {
            let n: u64 = digits.parse().ok()?;
            // Standard rule: 10-digit RČ is divisible by 11. Edge case:
            // when first-9-digit mod 11 = 10, the trailing 0 is used and
            // the full number's mod 11 is 10, not 0.
            let head: u64 = digits[..9].parse().ok()?;
            let tail = digits.chars().last()?.to_digit(10)?;
            if n.is_multiple_of(11) || (head % 11 == 10 && tail == 0) {
                Some(digits)
            } else {
                None
            }
        }
        _ => None,
    }
}

/// Parse a Denmark CPR (*Centrale Workerregister*).
///
/// 10 digits `DDMMYYNNNN`. Format-only validation; the historical Modulus-11
/// check was abandoned in 2007.
///
/// ```
/// use worker_matcher::identifiers::parse_dk_cpr;
/// assert_eq!(parse_dk_cpr("1501801234"), Some("1501801234".to_string()));
/// assert_eq!(parse_dk_cpr("150180-1234"), Some("1501801234".to_string()));
/// assert_eq!(parse_dk_cpr("12345"), None);
/// ```
pub fn parse_dk_cpr(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() == 10 {
        Some(digits)
    } else {
        None
    }
}

/// Cascading Mod-11 check used by Estonia and Lithuania.
fn baltic_cascade_check(digits: &str) -> Option<u32> {
    const PASS1: [u32; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1];
    const PASS2: [u32; 10] = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3];
    let body: Vec<u32> = digits
        .chars()
        .take(10)
        .filter_map(|c| c.to_digit(10))
        .collect();
    if body.len() != 10 {
        return None;
    }
    let s1: u32 = body.iter().zip(PASS1.iter()).map(|(d, w)| d * w).sum();
    let r1 = s1 % 11;
    if r1 < 10 {
        return Some(r1);
    }
    let s2: u32 = body.iter().zip(PASS2.iter()).map(|(d, w)| d * w).sum();
    let r2 = s2 % 11;
    if r2 < 10 { Some(r2) } else { Some(0) }
}

/// Parse an Estonia *Isikukood* (Workeral Identification Code).
///
/// 11 digits `GYYMMDDNNNC`. Check digit uses a cascading Mod-11 algorithm.
///
/// ```
/// use worker_matcher::identifiers::parse_ee_ik;
/// assert_eq!(parse_ee_ik("48001150011"), Some("48001150011".to_string()));
/// assert_eq!(parse_ee_ik("48001150012"), None);
/// ```
pub fn parse_ee_ik(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 11 {
        return None;
    }
    let expected = baltic_cascade_check(&digits[..10])?;
    if digits.chars().nth(10)?.to_digit(10)? == expected {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Spain DNI / NIE.
///
/// 8 digits (NIE: prefixed `X`/`Y`/`Z`) + 1 control letter. The letter is
/// `"TRWAGMYFPDXBNJZSQVHLCKE"` indexed by `number mod 23`. NIE prefixes map
/// to leading digits: `X→0`, `Y→1`, `Z→2`.
///
/// ```
/// use worker_matcher::identifiers::parse_es_dni;
/// assert_eq!(parse_es_dni("12345678Z"), Some("12345678Z".to_string()));
/// assert_eq!(parse_es_dni("12345678-Z"), Some("12345678Z".to_string()));
/// assert_eq!(parse_es_dni("12345678A"), None);  // wrong letter
/// ```
pub fn parse_es_dni(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_uppercase();
    if cleaned.is_empty() {
        return None;
    }
    let last = cleaned.chars().last()?;
    if !last.is_ascii_alphabetic() {
        return None;
    }
    let body = &cleaned[..cleaned.len() - 1];
    let n: u64 = match body.chars().next()? {
        'X' => format!("0{}", &body[1..]).parse().ok()?,
        'Y' => format!("1{}", &body[1..]).parse().ok()?,
        'Z' => format!("2{}", &body[1..]).parse().ok()?,
        d if d.is_ascii_digit() => body.parse().ok()?,
        _ => return None,
    };
    const LETTERS: &[u8; 23] = b"TRWAGMYFPDXBNJZSQVHLCKE";
    let expected = LETTERS[(n % 23) as usize] as char;
    if last == expected {
        Some(cleaned)
    } else {
        None
    }
}

/// Parse a Finland HETU (*Henkilötunnus*).
///
/// 11 characters `DDMMYYCZZZK` where `C` is a century sign (`-`/`+`/`A` and
/// later additions) and `K` is a check character from
/// `"0123456789ABCDEFHJKLMNPRSTUVWXY"` indexed by `(DDMMYYZZZ as 9-digit
/// number) mod 31`.
///
/// ```
/// use worker_matcher::identifiers::parse_fi_hetu;
/// assert_eq!(parse_fi_hetu("150180-999B"), Some("150180-999B".to_string()));
/// assert_eq!(parse_fi_hetu("150180-999C"), None);
/// ```
pub fn parse_fi_hetu(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| !c.is_whitespace())
        .collect::<String>()
        .to_uppercase();
    if !cleaned.is_ascii() || cleaned.len() != 11 {
        return None;
    }
    let date: &str = &cleaned[..6];
    let sign = cleaned.chars().nth(6)?;
    let serial: &str = &cleaned[7..10];
    let check = cleaned.chars().nth(10)?;
    if !date.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }
    if !serial.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }
    // Accept the historically-known and FICORA-extended signs.
    if !matches!(
        sign,
        '-' | '+' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'X' | 'Y'
    ) {
        return None;
    }
    let n: u64 = format!("{}{}", date, serial).parse().ok()?;
    const TABLE: &[u8; 31] = b"0123456789ABCDEFHJKLMNPRSTUVWXY";
    let expected = TABLE[(n % 31) as usize] as char;
    if check == expected {
        Some(cleaned)
    } else {
        None
    }
}

/// Parse a Croatia OIB (*Osobni identifikacijski broj*).
///
/// 11 digits. Check digit via ISO 7064 MOD 11,10.
///
/// ```
/// use worker_matcher::identifiers::parse_hr_oib;
/// assert_eq!(parse_hr_oib("12345678903"), Some("12345678903".to_string()));
/// assert_eq!(parse_hr_oib("12345678901"), None);
/// ```
pub fn parse_hr_oib(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 11 {
        return None;
    }
    let mut acc: u32 = 10;
    for c in digits.chars().take(10) {
        let d = c.to_digit(10)?;
        let mut x = (d + acc) % 10;
        if x == 0 {
            x = 10;
        }
        acc = (x * 2) % 11;
    }
    let expected = (11 - acc) % 10;
    if digits.chars().nth(10)?.to_digit(10)? == expected {
        Some(digits)
    } else {
        None
    }
}

/// Parse an Iceland *Kennitala*.
///
/// 10 digits `DDMMYYRRCN`. Check digit uses weights `[3,2,7,6,5,4,3,2]`
/// over the first 8 digits; mod 11 = 10 is invalid.
///
/// ```
/// use worker_matcher::identifiers::parse_is_kt;
/// assert_eq!(parse_is_kt("1501802529"), Some("1501802529".to_string()));
/// assert_eq!(parse_is_kt("1501802539"), None);  // wrong check digit
/// ```
pub fn parse_is_kt(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 10 {
        return None;
    }
    const WEIGHTS: [u32; 8] = [3, 2, 7, 6, 5, 4, 3, 2];
    let mut sum: u32 = 0;
    for (i, c) in digits.chars().take(8).enumerate() {
        sum += c.to_digit(10)? * WEIGHTS[i];
    }
    let r = sum % 11;
    if r == 10 {
        return None;
    }
    let expected = (11 - r) % 11;
    if digits.chars().nth(8)?.to_digit(10)? == expected {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Lithuania *Asmens kodas*.
///
/// 11 digits `GYYMMDDNNNC` with the same cascading Mod-11 check as Estonia.
///
/// ```
/// use worker_matcher::identifiers::parse_lt_ak;
/// assert_eq!(parse_lt_ak("48001150011"), Some("48001150011".to_string()));
/// assert_eq!(parse_lt_ak("48001150012"), None);
/// ```
pub fn parse_lt_ak(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 11 {
        return None;
    }
    let expected = baltic_cascade_check(&digits[..10])?;
    if digits.chars().nth(10)?.to_digit(10)? == expected {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Latvia *Workeras kods*.
///
/// 11 digits `DDMMYYCZZZK`. Check uses weights `[1,6,3,7,9,10,5,8,4,2]`
/// over the first 10 digits; `check = ((1101 − Σ) mod 11) mod 10`.
///
/// ```
/// use worker_matcher::identifiers::parse_lv_pk;
/// assert_eq!(parse_lv_pk("15018010007"), Some("15018010007".to_string()));
/// assert_eq!(parse_lv_pk("15018010008"), None);
/// ```
pub fn parse_lv_pk(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 11 {
        return None;
    }
    const WEIGHTS: [i32; 10] = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    let mut sum: i32 = 0;
    for (i, c) in digits.chars().take(10).enumerate() {
        sum += (c.to_digit(10)? as i32) * WEIGHTS[i];
    }
    let expected = ((1101 - sum).rem_euclid(11)) % 10;
    if digits.chars().nth(10)?.to_digit(10)? as i32 == expected {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Malta National ID.
///
/// 7 digits + 1 letter from `{M, G, A, P, L, H, B, Z}`. Format-only — the
/// suffix letter encodes geographic / registration provenance and is not a
/// check digit.
///
/// ```
/// use worker_matcher::identifiers::parse_mt_id;
/// assert_eq!(parse_mt_id("1234567M"), Some("1234567M".to_string()));
/// assert_eq!(parse_mt_id("1234567X"), None);  // X not in valid letter set
/// ```
pub fn parse_mt_id(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_uppercase();
    if cleaned.len() != 8 {
        return None;
    }
    let last = cleaned.chars().last()?;
    if !matches!(last, 'M' | 'G' | 'A' | 'P' | 'L' | 'H' | 'B' | 'Z') {
        return None;
    }
    if !cleaned[..7].chars().all(|c| c.is_ascii_digit()) {
        return None;
    }
    Some(cleaned)
}

/// Parse a Norway *Fødselsnummer*.
///
/// 11 digits with two Mod-11 check digits. Check 1 weights:
/// `[3,7,6,1,8,9,4,5,2]` over the first 9 digits. Check 2 weights:
/// `[5,4,3,2,7,6,5,4,3,2]` over the first 10. mod 11 = 10 is invalid.
///
/// ```
/// use worker_matcher::identifiers::parse_no_fnr;
/// assert_eq!(parse_no_fnr("15018012399"), Some("15018012399".to_string()));
/// assert_eq!(parse_no_fnr("15018012390"), None);
/// ```
pub fn parse_no_fnr(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 11 {
        return None;
    }
    const W1: [u32; 9] = [3, 7, 6, 1, 8, 9, 4, 5, 2];
    const W2: [u32; 10] = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
    let body: Vec<u32> = digits.chars().filter_map(|c| c.to_digit(10)).collect();
    if body.len() != 11 {
        return None;
    }
    let s1: u32 = body.iter().take(9).zip(W1.iter()).map(|(d, w)| d * w).sum();
    let r1 = s1 % 11;
    if r1 == 10 {
        return None;
    }
    let c1 = (11 - r1) % 11;
    if c1 != body[9] {
        return None;
    }
    let s2: u32 = body
        .iter()
        .take(10)
        .zip(W2.iter())
        .map(|(d, w)| d * w)
        .sum();
    let r2 = s2 % 11;
    if r2 == 10 {
        return None;
    }
    let c2 = (11 - r2) % 11;
    if c2 != body[10] {
        return None;
    }
    Some(digits)
}

/// Parse a Poland PESEL.
///
/// 11 digits `YYMMDDZZZZK` with century-encoded month. Check uses weights
/// `[1,3,7,9,1,3,7,9,1,3]` over the first 10 digits;
/// `check = (10 − (Σ mod 10)) mod 10`.
///
/// ```
/// use worker_matcher::identifiers::parse_pl_pesel;
/// assert_eq!(parse_pl_pesel("80011500014"), Some("80011500014".to_string()));
/// assert_eq!(parse_pl_pesel("80011500015"), None);
/// ```
pub fn parse_pl_pesel(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 11 {
        return None;
    }
    const WEIGHTS: [u32; 10] = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3];
    let mut sum: u32 = 0;
    for (i, c) in digits.chars().take(10).enumerate() {
        sum += c.to_digit(10)? * WEIGHTS[i];
    }
    let expected = (10 - (sum % 10)) % 10;
    if digits.chars().nth(10)?.to_digit(10)? == expected {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Romania CNP (*Cod Numeric Workeral*).
///
/// 13 digits `SYYMMDDJJNNNK`. Check uses weights "279146358279" (`[2,7,9,1,
/// 4,6,3,5,8,2,7,9]`) over the first 12 digits; `r = Σ mod 11`; check is
/// `1` if `r == 10`, else `r`.
///
/// ```
/// use worker_matcher::identifiers::parse_ro_cnp;
/// assert_eq!(parse_ro_cnp("1800115400012"), Some("1800115400012".to_string()));
/// assert_eq!(parse_ro_cnp("1800115400015"), None);
/// ```
pub fn parse_ro_cnp(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 13 {
        return None;
    }
    const WEIGHTS: [u32; 12] = [2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9];
    let mut sum: u32 = 0;
    for (i, c) in digits.chars().take(12).enumerate() {
        sum += c.to_digit(10)? * WEIGHTS[i];
    }
    let r = sum % 11;
    let expected = if r == 10 { 1 } else { r };
    if digits.chars().nth(12)?.to_digit(10)? == expected {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Slovenia EMŠO (*Enotna Matična Številka Občana*).
///
/// 13 digits `DDMMYYYRRGGGK`. Check uses weights `[7,6,5,4,3,2,7,6,5,4,3,2]`
/// over the first 12 digits; `r = Σ mod 11`; check is `0` if `r == 0`,
/// else `11 − r` (rejected if 10).
///
/// ```
/// use worker_matcher::identifiers::parse_si_emso;
/// assert_eq!(parse_si_emso("1501980500015"), Some("1501980500015".to_string()));
/// assert_eq!(parse_si_emso("1501980500014"), None);
/// ```
pub fn parse_si_emso(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 13 {
        return None;
    }
    const WEIGHTS: [u32; 12] = [7, 6, 5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
    let mut sum: u32 = 0;
    for (i, c) in digits.chars().take(12).enumerate() {
        sum += c.to_digit(10)? * WEIGHTS[i];
    }
    let r = sum % 11;
    let expected = if r == 0 { 0 } else { 11 - r };
    if expected == 10 {
        return None;
    }
    if digits.chars().nth(12)?.to_digit(10)? == expected {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Slovakia *Rodné číslo*. Same algorithm as Czech RČ.
///
/// ```
/// use worker_matcher::identifiers::parse_sk_rc;
/// assert_eq!(parse_sk_rc("8051150019"), Some("8051150019".to_string()));
/// assert_eq!(parse_sk_rc("8051150010"), None);
/// ```
pub fn parse_sk_rc(s: &str) -> Option<String> {
    parse_cz_rc(s)
}

/// Parse a United Kingdom National Insurance Number (NINO).
///
/// Format `AA999999A`: 2 prefix letters + 6 digits + 1 suffix letter.
/// Banned first prefix letters: `D F I Q U V`.
/// Banned second prefix letters: `D F I O Q U V`.
/// Banned admin prefixes: `OO CR FY MW NC PP PZ TN`.
/// Suffix MUST be one of `A B C D`. Format-only; no checksum.
///
/// ```
/// use worker_matcher::identifiers::parse_uk_nino;
/// assert_eq!(parse_uk_nino("AB123456A"), Some("AB123456A".to_string()));
/// assert_eq!(parse_uk_nino("ab 12 34 56 a"), Some("AB123456A".to_string()));
/// assert_eq!(parse_uk_nino("DA123456A"), None);  // banned first letter
/// assert_eq!(parse_uk_nino("ABCDEFGHI"), None);
/// ```
pub fn parse_uk_nino(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_uppercase();
    if cleaned.len() != 9 {
        return None;
    }
    let bytes = cleaned.as_bytes();
    let p1 = bytes[0] as char;
    let p2 = bytes[1] as char;
    if !p1.is_ascii_alphabetic() || !p2.is_ascii_alphabetic() {
        return None;
    }
    if matches!(p1, 'D' | 'F' | 'I' | 'Q' | 'U' | 'V') {
        return None;
    }
    if matches!(p2, 'D' | 'F' | 'I' | 'O' | 'Q' | 'U' | 'V') {
        return None;
    }
    let prefix = &cleaned[..2];
    if matches!(
        prefix,
        "OO" | "CR" | "FY" | "MW" | "NC" | "PP" | "PZ" | "TN"
    ) {
        return None;
    }
    if !cleaned[2..8].chars().all(|c| c.is_ascii_digit()) {
        return None;
    }
    let suffix = bytes[8] as char;
    if !matches!(suffix, 'A' | 'B' | 'C' | 'D') {
        return None;
    }
    Some(cleaned)
}

// ----------------------------------------------------------------------------
// Five additional workeral national identifiers (T-28).
// ----------------------------------------------------------------------------

/// Parse a Greece DSS (Dematerialised Securities System) investor share code.
///
/// 10-digit investor identifier issued by the Hellenic Central Securities
/// Depository (ATHEXCSD). Format-only validation: 10 ASCII digits.
///
/// ```
/// use worker_matcher::identifiers::parse_gr_dss;
/// assert_eq!(parse_gr_dss("1234567890"), Some("1234567890".to_string()));
/// assert_eq!(parse_gr_dss("12345"), None);
/// assert_eq!(parse_gr_dss("ABCDEFGHIJ"), None);
/// ```
pub fn parse_gr_dss(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() == 10 {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Liechtenstein National Identity Card Number.
///
/// Combination of 2 letters and 8 digits (the example in the spec
/// `ID022143586` shows a 9-digit run, so the parser accepts 8 *or* 9
/// trailing digits — total length 10 or 11). Note: per Liechtenstein
/// practice the number is **regenerated on each renewal**, so for
/// cross-renewal matching consumers should prefer
/// [`crate::PassportBook`] with `country = "LI"`.
///
/// ```
/// use worker_matcher::identifiers::parse_li_id;
/// assert_eq!(parse_li_id("ID12345678"), Some("ID12345678".to_string()));
/// assert_eq!(parse_li_id("ID022143586"), Some("ID022143586".to_string()));
/// assert_eq!(parse_li_id("12 34 56 78"), None);  // missing letters
/// assert_eq!(parse_li_id(""), None);
/// ```
pub fn parse_li_id(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_uppercase();
    if !(10..=11).contains(&cleaned.len()) {
        return None;
    }
    let chars: Vec<char> = cleaned.chars().collect();
    if !chars[0].is_ascii_alphabetic() || !chars[1].is_ascii_alphabetic() {
        return None;
    }
    if !chars[2..].iter().all(|c| c.is_ascii_digit()) {
        return None;
    }
    Some(cleaned)
}

/// Parse a Netherlands National Identity Card Number.
///
/// 9 characters: positions 1 and 2 are uppercase letters `[A-Z]` except
/// `O`; positions 3–8 are alphanumeric `[A-Z0-9]` except `O`; position 9
/// is a digit `[0-9]`. The character `O` is disallowed (to avoid
/// confusion with `0`), but the digit `0` is allowed.
///
/// ```
/// use worker_matcher::identifiers::parse_nl_id;
/// assert_eq!(parse_nl_id("AB1234567"), Some("AB1234567".to_string()));
/// assert_eq!(parse_nl_id("ab 12 34 567"), Some("AB1234567".to_string()));
/// assert_eq!(parse_nl_id("AO1234567"), None);   // O is banned
/// assert_eq!(parse_nl_id("AB12345AB"), None);   // last must be digit
/// assert_eq!(parse_nl_id("12345AB67"), None);   // leading must be letters
/// ```
pub fn parse_nl_id(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_uppercase();
    if cleaned.len() != 9 {
        return None;
    }
    let chars: Vec<char> = cleaned.chars().collect();
    for c in chars.iter().take(2) {
        if !c.is_ascii_alphabetic() || *c == 'O' {
            return None;
        }
    }
    for c in chars.iter().take(8).skip(2) {
        if !c.is_ascii_alphanumeric() || *c == 'O' {
            return None;
        }
    }
    if !chars[8].is_ascii_digit() {
        return None;
    }
    Some(cleaned)
}

/// Parse a Poland NIP (*Numer Identyfikacji Podatkowej*).
///
/// 10 digits with a weighted Mod-11 check. Weights for the first 9
/// digits: `[6, 5, 7, 2, 3, 4, 5, 6, 7]`. `r = Σ mod 11`; a remainder
/// of 10 indicates an invalid NIP; otherwise the 10th digit MUST equal
/// `r`.
///
/// ```
/// use worker_matcher::identifiers::parse_pl_nip;
/// assert_eq!(parse_pl_nip("1234567802"), Some("1234567802".to_string()));
/// assert_eq!(parse_pl_nip("123-456-78-02"), Some("1234567802".to_string()));
/// assert_eq!(parse_pl_nip("1234567803"), None);    // wrong check
/// assert_eq!(parse_pl_nip("1234567890"), None);    // r = 10 — invalid by spec
/// ```
pub fn parse_pl_nip(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 10 {
        return None;
    }
    const WEIGHTS: [u32; 9] = [6, 5, 7, 2, 3, 4, 5, 6, 7];
    let mut sum: u32 = 0;
    for (i, c) in digits.chars().take(9).enumerate() {
        sum += c.to_digit(10)? * WEIGHTS[i];
    }
    let r = sum % 11;
    if r == 10 {
        return None;
    }
    if digits.chars().nth(9)?.to_digit(10)? == r {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Portugal NIF (*Número de Identificação Fiscal*).
///
/// 9 digits with a weighted Mod-11 check. Weights for the first 8
/// digits: `[9, 8, 7, 6, 5, 4, 3, 2]`. `r = Σ mod 11`; check is `0` if
/// `r < 2`, else `11 − r`. The 9th digit MUST equal the check.
///
/// ```
/// use worker_matcher::identifiers::parse_pt_nif;
/// assert_eq!(parse_pt_nif("123456789"), Some("123456789".to_string()));
/// assert_eq!(parse_pt_nif("123 456 789"), Some("123456789".to_string()));
/// assert_eq!(parse_pt_nif("123456780"), None);
/// ```
pub fn parse_pt_nif(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 9 {
        return None;
    }
    const WEIGHTS: [u32; 8] = [9, 8, 7, 6, 5, 4, 3, 2];
    let mut sum: u32 = 0;
    for (i, c) in digits.chars().take(8).enumerate() {
        sum += c.to_digit(10)? * WEIGHTS[i];
    }
    let r = sum % 11;
    let expected = if r < 2 { 0 } else { 11 - r };
    if digits.chars().nth(8)?.to_digit(10)? == expected {
        Some(digits)
    } else {
        None
    }
}

// ----------------------------------------------------------------------------
// T-17.1 — seven next-batch national identifier schemes.
//
// Per the T-17 spike (§21.4 / §23.2): one parser per jurisdiction the
// crate already supports phones for but not national IDs.
// ----------------------------------------------------------------------------

/// Parse a Brazil CPF (*Cadastro de Pessoas Físicas*).
///
/// The CPF is 11 digits, often formatted `NNN.NNN.NNN-DD`. The last two
/// digits are computed check digits using weighted Mod-11. The parser
/// strips non-digits, requires exactly 11 digits, rejects all-equal
/// sequences (the canonical sentinel / test vectors a real CPF MUST
/// NOT take), and validates both check digits.
///
/// ```
/// use worker_matcher::identifiers::parse_br_cpf;
/// assert_eq!(parse_br_cpf("123.456.789-09"), Some("12345678909".to_string()));
/// assert_eq!(parse_br_cpf("12345678909"),    Some("12345678909".to_string()));
/// assert_eq!(parse_br_cpf("12345678900"),    None);             // wrong D2
/// assert_eq!(parse_br_cpf("11111111111"),    None);             // all-equal sentinel
/// assert_eq!(parse_br_cpf("1234567890"),     None);             // too short
/// ```
pub fn parse_br_cpf(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 11 {
        return None;
    }
    let bytes = digits.as_bytes();
    if bytes.iter().all(|&b| b == bytes[0]) {
        return None;
    }
    let d = |i: usize| (bytes[i] - b'0') as u32;
    let mut sum1: u32 = 0;
    for i in 0..9 {
        sum1 += d(i) * (10 - i as u32);
    }
    let r1 = sum1 % 11;
    let exp1 = if r1 < 2 { 0 } else { 11 - r1 };
    if d(9) != exp1 {
        return None;
    }
    let mut sum2: u32 = 0;
    for i in 0..10 {
        sum2 += d(i) * (11 - i as u32);
    }
    let r2 = sum2 % 11;
    let exp2 = if r2 < 2 { 0 } else { 11 - r2 };
    if d(10) != exp2 {
        return None;
    }
    Some(digits)
}

/// Parse a China Resident Identity Card number (*居民身份证*, 18-digit
/// 1999 reform).
///
/// 18 characters: 17 digits + a check character (digit or `X`). The
/// substring at positions 6..14 encodes the date of birth (`YYYYMMDD`)
/// and MUST be a valid calendar date. The check character is computed
/// from a weighted Mod-11 sum over the 17 leading digits with the
/// lookup `1,0,X,9,8,7,6,5,4,3,2`. Lowercase `x` is accepted and
/// canonicalised to uppercase. The 15-digit pre-1999 form is NOT
/// accepted; consumers MUST migrate to the 18-digit form before
/// matching (the conversion is well-documented but jurisdiction-locked
/// and out of scope for this parser).
///
/// ```
/// use worker_matcher::identifiers::parse_cn_rrn;
/// assert_eq!(
///     parse_cn_rrn("11010519491231002X"),
///     Some("11010519491231002X".to_string()),
/// );
/// assert_eq!(
///     parse_cn_rrn("11010519491231002x"),
///     Some("11010519491231002X".to_string()),
/// );
/// assert_eq!(parse_cn_rrn("11010519491231002Y"), None);          // wrong check
/// assert_eq!(parse_cn_rrn("11010513491231002X"), None);          // invalid month
/// assert_eq!(parse_cn_rrn("110105194912310020"), None);          // wrong check
/// assert_eq!(parse_cn_rrn("11010519491231"),     None);          // too short
/// ```
pub fn parse_cn_rrn(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .map(|c| c.to_ascii_uppercase())
        .collect();
    if cleaned.len() != 18 {
        return None;
    }
    let bytes = cleaned.as_bytes();
    for &b in &bytes[..17] {
        if !b.is_ascii_digit() {
            return None;
        }
    }
    if !bytes[17].is_ascii_digit() && bytes[17] != b'X' {
        return None;
    }
    let yyyy: i32 = cleaned[6..10].parse().ok()?;
    let mm: u32 = cleaned[10..12].parse().ok()?;
    let dd: u32 = cleaned[12..14].parse().ok()?;
    chrono::NaiveDate::from_ymd_opt(yyyy, mm, dd)?;
    const WEIGHTS: [u32; 17] = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    const CHECK: [u8; 11] = [
        b'1', b'0', b'X', b'9', b'8', b'7', b'6', b'5', b'4', b'3', b'2',
    ];
    let mut sum: u32 = 0;
    for i in 0..17 {
        sum += u32::from(bytes[i] - b'0') * WEIGHTS[i];
    }
    if bytes[17] != CHECK[(sum % 11) as usize] {
        return None;
    }
    Some(cleaned)
}

/// Parse an India Aadhaar number (12 digits, Verhoeff check digit).
///
/// The Verhoeff algorithm uses two precomputed lookup tables (the
/// dihedral-group multiplication table `D` and the permutation table
/// `P`) and runs in linear time over the input. The parser strips
/// non-digits, requires exactly 12 digits, rejects all-equal sequences
/// and the UIDAI-test-prefix ranges (numbers beginning with `0` or
/// `1`, which UIDAI guidance reserves and never issues to real
/// citizens), and validates the Verhoeff check digit at the rightmost
/// position.
///
/// ```
/// use worker_matcher::identifiers::parse_in_aadhaar;
/// assert_eq!(parse_in_aadhaar("234123412346"),   Some("234123412346".to_string()));
/// assert_eq!(parse_in_aadhaar("2341 2341 2346"), Some("234123412346".to_string()));
/// assert_eq!(parse_in_aadhaar("234123412347"),   None);  // wrong check
/// assert_eq!(parse_in_aadhaar("234123412"),      None);  // too short
/// assert_eq!(parse_in_aadhaar("222222222222"),   None);  // all-equal sentinel
/// assert_eq!(parse_in_aadhaar("034123412346"),   None);  // reserved prefix
/// ```
pub fn parse_in_aadhaar(s: &str) -> Option<String> {
    const VERHOEFF_D: [[u8; 10]; 10] = [
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
        [1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
        [2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
        [3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
        [4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
        [5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
        [6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
        [7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
        [8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
        [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    ];
    const VERHOEFF_P: [[u8; 10]; 8] = [
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
        [1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
        [5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
        [8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
        [9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
        [4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
        [2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
        [7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
    ];
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 12 {
        return None;
    }
    let bytes = digits.as_bytes();
    if bytes.iter().all(|&b| b == bytes[0]) {
        return None;
    }
    if bytes[0] == b'0' || bytes[0] == b'1' {
        return None;
    }
    let mut c: u8 = 0;
    for i in 0..12 {
        let d = bytes[11 - i] - b'0';
        c = VERHOEFF_D[c as usize][VERHOEFF_P[i % 8][d as usize] as usize];
    }
    if c == 0 { Some(digits) } else { None }
}

/// Parse a Japan My Number (個人番号, 12 digits).
///
/// The check digit is computed by a weighted Mod-11 sum over the
/// first 11 digits using the weights `[6, 5, 4, 3, 2, 7, 6, 5, 4, 3, 2]`
/// (per the Japanese e-Gov Cabinet Order specification). If the
/// remainder is `< 2`, the check digit is `0`; otherwise it is
/// `11 - remainder`.
///
/// ```
/// use worker_matcher::identifiers::parse_jp_my_number;
/// assert_eq!(parse_jp_my_number("123456789018"),   Some("123456789018".to_string()));
/// assert_eq!(parse_jp_my_number("1234 5678 9018"), Some("123456789018".to_string()));
/// assert_eq!(parse_jp_my_number("123456789010"),   None);  // wrong check
/// assert_eq!(parse_jp_my_number("12345678901"),    None);  // too short
/// ```
pub fn parse_jp_my_number(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 12 {
        return None;
    }
    let bytes = digits.as_bytes();
    const WEIGHTS: [u32; 11] = [6, 5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
    let mut sum: u32 = 0;
    for i in 0..11 {
        sum += u32::from(bytes[i] - b'0') * WEIGHTS[i];
    }
    let r = sum % 11;
    let expected = if r < 2 { 0 } else { 11 - r };
    if u32::from(bytes[11] - b'0') != expected {
        return None;
    }
    Some(digits)
}

/// Parse a Mexico CURP (*Clave Única de Registro de Población*).
///
/// 18 characters with rich internal structure: 4 letters (surname /
/// given-name initials), 6 digits (`YYMMDD`), 1 sex char (`H` or `M`),
/// 2 letters (state code), 3 letters (internal consonants), 1
/// alphanumeric (homonym discriminator), 1 check digit. The parser
/// uppercases, validates the structural shape, verifies the embedded
/// date of birth is a valid calendar date (century inferred per the
/// usual Mexican convention: `YY <= 29 → 20YY`, else `19YY`), and
/// validates the Mod-10 weighted check digit using the standard CURP
/// value table (`0..9` literal, `A..N` = 10..23, `Ñ` = 24,
/// `O..Z` = 25..36).
///
/// Ñ in the input is accepted; non-ASCII characters other than Ñ are
/// rejected.
///
/// ```
/// use worker_matcher::identifiers::parse_mx_curp;
/// assert_eq!(
///     parse_mx_curp("HEGG560427MVZRRL04"),
///     Some("HEGG560427MVZRRL04".to_string()),
/// );
/// assert_eq!(
///     parse_mx_curp("hegg560427mvzrrl04"),
///     Some("HEGG560427MVZRRL04".to_string()),
/// );
/// assert_eq!(parse_mx_curp("HEGG560427MVZRRL05"), None);   // wrong check
/// assert_eq!(parse_mx_curp("HEGG561327MVZRRL04"), None);   // invalid month
/// assert_eq!(parse_mx_curp("HEGG560427"),         None);   // too short
/// ```
pub fn parse_mx_curp(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| !c.is_whitespace())
        .map(|c| c.to_uppercase().next().unwrap_or(c))
        .collect();
    if cleaned.chars().count() != 18 {
        return None;
    }
    let chars: Vec<char> = cleaned.chars().collect();
    let is_letter_or_n_tilde = |c: char| c.is_ascii_uppercase() || c == 'Ñ';
    if !chars[..4].iter().copied().all(is_letter_or_n_tilde) {
        return None;
    }
    if !chars[4..10].iter().all(|c| c.is_ascii_digit()) {
        return None;
    }
    if chars[10] != 'H' && chars[10] != 'M' {
        return None;
    }
    if !chars[11..16].iter().copied().all(is_letter_or_n_tilde) {
        return None;
    }
    if !chars[16].is_ascii_alphanumeric() && chars[16] != 'Ñ' {
        return None;
    }
    if !chars[17].is_ascii_digit() {
        return None;
    }
    let yy: i32 = cleaned[4..6].parse().ok()?;
    let mm: u32 = cleaned[6..8].parse().ok()?;
    let dd: u32 = cleaned[8..10].parse().ok()?;
    let yyyy = if yy <= 29 { 2000 + yy } else { 1900 + yy };
    chrono::NaiveDate::from_ymd_opt(yyyy, mm, dd)?;
    let value = |c: char| -> Option<u32> {
        Some(match c {
            '0'..='9' => (c as u32) - ('0' as u32),
            'A'..='N' => 10 + ((c as u32) - ('A' as u32)),
            'Ñ' => 24,
            'O'..='Z' => 25 + ((c as u32) - ('O' as u32)),
            _ => return None,
        })
    };
    let mut sum: u32 = 0;
    for (i, &c) in chars.iter().enumerate().take(17) {
        sum += value(c)? * (18 - i as u32);
    }
    let expected = (10 - (sum % 10)) % 10;
    if u32::from(chars[17] as u8 - b'0') != expected {
        return None;
    }
    Some(cleaned)
}

/// Parse a New Zealand NHI Number (original 7-character format:
/// 3 letters + 4 digits, where the last digit is a Mod-11 check).
///
/// The letter values are: `A..Z` minus `I` and `O` (excluded because
/// they collide visually with `1` and `0`), assigned consecutively:
/// `A=1, B=2, C=3, D=4, E=5, F=6, G=7, H=8, J=9, K=10, L=11, M=12,
/// N=13, P=14, Q=15, R=16, S=17, T=18, U=19, V=20, W=21, X=22, Y=23,
/// Z=24`. The weighted sum applies weights `[7, 6, 5, 4, 3, 2]` to
/// the first six positions (3 letters + 3 digits); the remainder mod
/// 11 yields the expected check digit (`0` if remainder is `0`,
/// otherwise `11 - remainder`; if the result is `10` the NHI is
/// invalid because no single decimal digit can encode `10`).
///
/// The 2019 7-character alphanumeric NHI revision (3 letters + 2
/// digits + 2 letters) uses a different algorithm and is **not**
/// supported by this parser; calls fall through to `None` for the new
/// format. Consumers handling 2019-format NHIs SHOULD validate
/// upstream and pass the value through as a third-party identifier.
///
/// ```
/// use worker_matcher::identifiers::parse_nz_nhi;
/// assert_eq!(parse_nz_nhi("ZAA0083"), Some("ZAA0083".to_string()));
/// assert_eq!(parse_nz_nhi("zaa0083"), Some("ZAA0083".to_string()));
/// assert_eq!(parse_nz_nhi("ZAA0082"), None);          // wrong check
/// assert_eq!(parse_nz_nhi("ZAI0083"), None);          // I excluded
/// assert_eq!(parse_nz_nhi("ZAA008"),  None);          // too short
/// ```
pub fn parse_nz_nhi(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .map(|c| c.to_ascii_uppercase())
        .collect();
    if cleaned.len() != 7 {
        return None;
    }
    let bytes = cleaned.as_bytes();
    for &b in &bytes[..3] {
        if !b.is_ascii_uppercase() || b == b'I' || b == b'O' {
            return None;
        }
    }
    for &b in &bytes[3..] {
        if !b.is_ascii_digit() {
            return None;
        }
    }
    let letter_value = |b: u8| -> u32 {
        let idx = u32::from(b - b'A') + 1;
        if b > b'O' {
            idx - 2
        } else if b > b'I' {
            idx - 1
        } else {
            idx
        }
    };
    const WEIGHTS: [u32; 6] = [7, 6, 5, 4, 3, 2];
    let mut sum: u32 = 0;
    for i in 0..3 {
        sum += letter_value(bytes[i]) * WEIGHTS[i];
    }
    for i in 0..3 {
        sum += u32::from(bytes[3 + i] - b'0') * WEIGHTS[3 + i];
    }
    let r = sum % 11;
    if r == 1 {
        return None;
    }
    let expected = if r == 0 { 0 } else { 11 - r };
    if u32::from(bytes[6] - b'0') != expected {
        return None;
    }
    Some(cleaned)
}

/// Parse a South Africa ID Number (13 digits, Luhn check digit + a
/// date-of-birth substring at positions 0..6).
///
/// The first 6 digits encode `YYMMDD`; the century is conventionally
/// inferred (`YY <= 29 → 20YY`, else `19YY`). The check digit at
/// position 12 is computed by the standard Luhn algorithm over all
/// 13 digits.
///
/// The remaining substrings (sequence at positions 6..10, citizenship
/// at position 10, and the legacy race indicator at position 11) are
/// intentionally NOT validated by this parser — they are demographic
/// information the worker-matcher layer does not use.
///
/// ```
/// use worker_matcher::identifiers::parse_za_id;
/// assert_eq!(parse_za_id("8001015009087"),   Some("8001015009087".to_string()));
/// assert_eq!(parse_za_id("800101 5009 087"), Some("8001015009087".to_string()));
/// assert_eq!(parse_za_id("8001015009088"),   None);  // wrong Luhn
/// assert_eq!(parse_za_id("8013015009087"),   None);  // invalid month
/// assert_eq!(parse_za_id("80010150090"),     None);  // too short
/// ```
pub fn parse_za_id(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() != 13 {
        return None;
    }
    let yy: i32 = digits[0..2].parse().ok()?;
    let mm: u32 = digits[2..4].parse().ok()?;
    let dd: u32 = digits[4..6].parse().ok()?;
    let yyyy = if yy <= 29 { 2000 + yy } else { 1900 + yy };
    chrono::NaiveDate::from_ymd_opt(yyyy, mm, dd)?;
    let bytes = digits.as_bytes();
    let mut sum: u32 = 0;
    // Standard Luhn: process right-to-left, double every second digit
    // starting from the second-to-last (i.e. positions 1, 3, 5, … from
    // the right). For a 13-digit ID this doubles positions 11, 9, 7,
    // 5, 3, 1 (counting from the left, 0-indexed).
    for i in 0..13 {
        let mut d = u32::from(bytes[12 - i] - b'0');
        if i % 2 == 1 {
            d *= 2;
            if d > 9 {
                d -= 9;
            }
        }
        sum += d;
    }
    if !sum.is_multiple_of(10) {
        return None;
    }
    Some(digits)
}

// ----------------------------------------------------------------------------
// Nine per-country passport-number format validators (T-28).
//
// These are pure format validators that consumers may call before
// constructing a `PassportBook`. They do NOT correspond to `Worker`
// fields — passport-book numbers belong to the `PassportBook` model
// because they change with each renewal and a worker may carry
// passports from multiple countries simultaneously.
// ----------------------------------------------------------------------------

/// Parse a Cyprus passport number.
///
/// Pre-2010 passports: letter `E` + 6 digits (e.g. `E123456`).
/// Biometric passports issued from 13 December 2010 onwards: letter `K`
/// + 8 digits (e.g. `K12345678`).
///
/// ```
/// use worker_matcher::identifiers::parse_cy_passport;
/// assert_eq!(parse_cy_passport("E123456"),   Some("E123456".to_string()));
/// assert_eq!(parse_cy_passport("k12345678"), Some("K12345678".to_string()));
/// assert_eq!(parse_cy_passport("A123456"),   None);
/// assert_eq!(parse_cy_passport("E12345"),    None);  // too short
/// ```
pub fn parse_cy_passport(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_uppercase();
    let chars: Vec<char> = cleaned.chars().collect();
    match (chars.first(), chars.len()) {
        (Some('E'), 7) if chars[1..].iter().all(|c| c.is_ascii_digit()) => Some(cleaned),
        (Some('K'), 9) if chars[1..].iter().all(|c| c.is_ascii_digit()) => Some(cleaned),
        _ => None,
    }
}

/// Parse a Czech Republic passport number.
///
/// Usually an 8-digit number; per the TSV it may be longer. The parser
/// accepts 8 to 12 ASCII digits.
///
/// ```
/// use worker_matcher::identifiers::parse_cz_passport;
/// assert_eq!(parse_cz_passport("12345678"), Some("12345678".to_string()));
/// assert_eq!(parse_cz_passport("123-456-78"), Some("12345678".to_string()));
/// assert_eq!(parse_cz_passport("123"), None);  // too short
/// ```
pub fn parse_cz_passport(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if (8..=12).contains(&digits.len()) {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Liechtenstein passport number. 1 letter + 5 digits (e.g. `R00536`).
///
/// ```
/// use worker_matcher::identifiers::parse_li_passport;
/// assert_eq!(parse_li_passport("R00536"), Some("R00536".to_string()));
/// assert_eq!(parse_li_passport("r00536"), Some("R00536".to_string()));
/// assert_eq!(parse_li_passport("123456"), None);
/// ```
pub fn parse_li_passport(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_uppercase();
    if cleaned.len() != 6 {
        return None;
    }
    let chars: Vec<char> = cleaned.chars().collect();
    if !chars[0].is_ascii_alphabetic() {
        return None;
    }
    if !chars[1..].iter().all(|c| c.is_ascii_digit()) {
        return None;
    }
    Some(cleaned)
}

/// Parse a Lithuania passport number. 8 ASCII digits (also used on the
/// national ID card).
///
/// ```
/// use worker_matcher::identifiers::parse_lt_passport;
/// assert_eq!(parse_lt_passport("12345678"), Some("12345678".to_string()));
/// assert_eq!(parse_lt_passport("1234567"), None);
/// ```
pub fn parse_lt_passport(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() == 8 {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Malta passport number. 7 ASCII digits.
///
/// ```
/// use worker_matcher::identifiers::parse_mt_passport;
/// assert_eq!(parse_mt_passport("1234567"), Some("1234567".to_string()));
/// assert_eq!(parse_mt_passport("123"), None);
/// ```
pub fn parse_mt_passport(s: &str) -> Option<String> {
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() == 7 {
        Some(digits)
    } else {
        None
    }
}

/// Parse a Netherlands passport number. Same shape as the NL ID card
/// (see [`parse_nl_id`]).
///
/// ```
/// use worker_matcher::identifiers::parse_nl_passport;
/// assert_eq!(parse_nl_passport("AB1234567"), Some("AB1234567".to_string()));
/// assert_eq!(parse_nl_passport("AO1234567"), None);  // O is banned
/// ```
pub fn parse_nl_passport(s: &str) -> Option<String> {
    parse_nl_id(s)
}

/// Parse a Portugal passport number. 1 letter + 6 digits.
///
/// ```
/// use worker_matcher::identifiers::parse_pt_passport;
/// assert_eq!(parse_pt_passport("A123456"), Some("A123456".to_string()));
/// assert_eq!(parse_pt_passport("AA12345"), None);
/// ```
pub fn parse_pt_passport(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_uppercase();
    if cleaned.len() != 7 {
        return None;
    }
    let chars: Vec<char> = cleaned.chars().collect();
    if !chars[0].is_ascii_alphabetic() {
        return None;
    }
    if !chars[1..].iter().all(|c| c.is_ascii_digit()) {
        return None;
    }
    Some(cleaned)
}

/// Parse a Romania passport number. 2 letters + 6 digits.
///
/// ```
/// use worker_matcher::identifiers::parse_ro_passport;
/// assert_eq!(parse_ro_passport("AB123456"), Some("AB123456".to_string()));
/// assert_eq!(parse_ro_passport("A1234567"), None);
/// ```
pub fn parse_ro_passport(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_uppercase();
    if cleaned.len() != 8 {
        return None;
    }
    let chars: Vec<char> = cleaned.chars().collect();
    if !chars[..2].iter().all(|c| c.is_ascii_alphabetic()) {
        return None;
    }
    if !chars[2..].iter().all(|c| c.is_ascii_digit()) {
        return None;
    }
    Some(cleaned)
}

/// Parse a Slovakia passport number. 2 letters + 7 digits.
///
/// ```
/// use worker_matcher::identifiers::parse_sk_passport;
/// assert_eq!(parse_sk_passport("AB1234567"), Some("AB1234567".to_string()));
/// assert_eq!(parse_sk_passport("AB12345"), None);
/// ```
pub fn parse_sk_passport(s: &str) -> Option<String> {
    let cleaned: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_uppercase();
    if cleaned.len() != 9 {
        return None;
    }
    let chars: Vec<char> = cleaned.chars().collect();
    if !chars[..2].iter().all(|c| c.is_ascii_alphabetic()) {
        return None;
    }
    if !chars[2..].iter().all(|c| c.is_ascii_digit()) {
        return None;
    }
    Some(cleaned)
}

#[cfg(test)]
mod tests {
    use super::*;

    // ---------- parse_uk_nhs_number ----------

    #[test]
    fn uk_nhs_number_compact_form_parses() {
        assert_eq!(parse_uk_nhs_number("9434765919"), Some("9434765919".into()));
    }

    #[test]
    fn uk_nhs_number_spaced_form_parses_to_same_canonical() {
        assert_eq!(
            parse_uk_nhs_number("943 476 5919"),
            parse_uk_nhs_number("9434765919"),
        );
    }

    #[test]
    fn uk_nhs_number_rejects_letters_and_short_input() {
        assert_eq!(parse_uk_nhs_number("ABCDEFGHIJ"), None);
        assert_eq!(parse_uk_nhs_number("123"), None);
        assert_eq!(parse_uk_nhs_number(""), None);
    }

    // ---------- parse_fr_nir ----------

    #[test]
    fn fr_nir_round_trip_for_a_constructed_valid_value() {
        // Body 1801275123456 → key = 97 - (N mod 97) = 42. Verified by parse.
        let valid = "180127512345642";
        assert_eq!(parse_fr_nir(valid), Some(valid.into()));
    }

    #[test]
    fn fr_nir_whitespace_is_tolerated() {
        assert_eq!(
            parse_fr_nir("1 80 12 75 123 456 42"),
            Some("180127512345642".into()),
        );
    }

    #[test]
    fn fr_nir_rejects_wrong_check_key() {
        assert_eq!(parse_fr_nir("180127512345699"), None);
    }

    #[test]
    fn fr_nir_rejects_wrong_length() {
        assert_eq!(parse_fr_nir("12345"), None);
        assert_eq!(parse_fr_nir("1234567890123456"), None); // 16 chars
        assert_eq!(parse_fr_nir(""), None);
    }

    #[test]
    fn fr_nir_rejects_letters_in_digit_positions() {
        assert_eq!(parse_fr_nir("A80127512345642"), None);
    }

    #[test]
    fn fr_nir_handles_corsica_2a() {
        let body = "184032A001234";
        let numeric: u64 = "1840319001234".parse().unwrap();
        let key = 97 - (numeric % 97);
        let nir = format!("{body}{key:02}");
        assert_eq!(parse_fr_nir(&nir), Some(nir.clone()));
    }

    #[test]
    fn fr_nir_handles_corsica_2b() {
        let body = "184032B001234";
        let numeric: u64 = "1840318001234".parse().unwrap();
        let key = 97 - (numeric % 97);
        let nir = format!("{body}{key:02}");
        assert_eq!(parse_fr_nir(&nir), Some(nir.clone()));
    }

    #[test]
    fn fr_nir_canonical_form_is_uppercased() {
        let body = "184032a001234";
        let numeric: u64 = "1840319001234".parse().unwrap();
        let key = 97 - (numeric % 97);
        let nir = format!("{body}{key:02}");
        let canonical = nir.to_uppercase();
        assert_eq!(parse_fr_nir(&nir), Some(canonical));
    }

    // ---------- parse_es_tsi ----------

    #[test]
    fn es_tsi_canonical_cip_sns_parses() {
        assert_eq!(
            parse_es_tsi("ABCD123456XY1234"),
            Some("ABCD123456XY1234".into()),
        );
    }

    #[test]
    fn es_tsi_whitespace_and_hyphens_stripped() {
        assert_eq!(
            parse_es_tsi("abcd 123 456-xy1234"),
            Some("ABCD123456XY1234".into()),
        );
    }

    #[test]
    fn es_tsi_rejects_too_short_or_too_long() {
        assert_eq!(parse_es_tsi("ABC123"), None);
        assert_eq!(parse_es_tsi("ABCDEF123456XY12345678"), None);
    }

    #[test]
    fn es_tsi_rejects_non_alphanumerics() {
        assert_eq!(parse_es_tsi("ABC@123!XYZ"), None);
    }

    #[test]
    fn es_tsi_rejects_non_ascii() {
        assert_eq!(parse_es_tsi("ABCDÑ12345XYZ"), None);
    }

    // ---------- parse_ie_ihi ----------

    #[test]
    fn ie_ihi_seven_digits_parses() {
        assert_eq!(parse_ie_ihi("1234567"), Some("1234567".into()));
    }

    #[test]
    fn ie_ihi_punctuation_and_spaces_stripped() {
        assert_eq!(parse_ie_ihi("123 4567"), Some("1234567".into()));
        assert_eq!(parse_ie_ihi("123-45-67"), Some("1234567".into()));
    }

    #[test]
    fn ie_ihi_rejects_wrong_digit_count() {
        assert_eq!(parse_ie_ihi("123456"), None);
        assert_eq!(parse_ie_ihi("12345678"), None);
        assert_eq!(parse_ie_ihi(""), None);
    }

    #[test]
    fn ie_ihi_rejects_when_no_digits_present() {
        assert_eq!(parse_ie_ihi("ABCDEFG"), None);
    }

    // ---------- parse_uk_hc_number ----------

    #[test]
    fn uk_hc_number_matches_nhs_number_semantics() {
        assert_eq!(
            parse_uk_hc_number("9434765919"),
            parse_uk_nhs_number("9434765919"),
        );
        assert_eq!(
            parse_uk_hc_number("943 476 5919"),
            parse_uk_nhs_number("943 476 5919"),
        );
    }

    #[test]
    fn uk_hc_number_rejects_letters() {
        assert_eq!(parse_uk_hc_number("ABCDEFGHIJ"), None);
    }

    // ---------- parse_us_ssn ----------

    #[test]
    fn us_ssn_canonical_compact_form_parses() {
        assert_eq!(parse_us_ssn("123456789"), Some("123456789".into()));
    }

    #[test]
    fn us_ssn_hyphenated_form_parses_to_same_canonical() {
        assert_eq!(parse_us_ssn("123-45-6789"), parse_us_ssn("123456789"),);
    }

    #[test]
    fn us_ssn_whitespace_variants_canonicalise_identically() {
        assert_eq!(parse_us_ssn("123 45 6789"), Some("123456789".into()),);
        assert_eq!(parse_us_ssn(" 123  45 6789 "), Some("123456789".into()),);
    }

    #[test]
    fn us_ssn_rejects_invalid_area_numbers() {
        assert_eq!(parse_us_ssn("000-12-3456"), None);
        assert_eq!(parse_us_ssn("666-12-3456"), None);
        assert_eq!(parse_us_ssn("900-12-3456"), None);
        assert_eq!(parse_us_ssn("987-65-4321"), None); // 987 is in 900..=999
        assert_eq!(parse_us_ssn("999-99-9999"), None);
    }

    #[test]
    fn us_ssn_accepts_boundary_areas() {
        // 001 and 899 are the lowest and highest valid area numbers.
        assert_eq!(parse_us_ssn("001-23-4567"), Some("001234567".into()));
        assert_eq!(parse_us_ssn("899-23-4567"), Some("899234567".into()));
        // 665 just below the 666 carve-out; 667 just above.
        assert_eq!(parse_us_ssn("665-23-4567"), Some("665234567".into()));
        assert_eq!(parse_us_ssn("667-23-4567"), Some("667234567".into()));
    }

    #[test]
    fn us_ssn_rejects_zero_group() {
        assert_eq!(parse_us_ssn("123-00-4567"), None);
    }

    #[test]
    fn us_ssn_rejects_zero_serial() {
        assert_eq!(parse_us_ssn("123-45-0000"), None);
    }

    #[test]
    fn us_ssn_rejects_wrong_length() {
        assert_eq!(parse_us_ssn("12345"), None);
        assert_eq!(parse_us_ssn("1234567890"), None);
        assert_eq!(parse_us_ssn(""), None);
    }

    #[test]
    fn us_ssn_rejects_letters() {
        assert_eq!(parse_us_ssn("ABC-DE-FGHI"), None);
        assert_eq!(parse_us_ssn("ABCDEFGHI"), None);
    }

    #[test]
    fn us_ssn_strips_arbitrary_punctuation() {
        assert_eq!(parse_us_ssn("(123).45.6789"), Some("123456789".into()),);
    }

    // ---------- parse_de_kvnr ----------

    #[test]
    fn de_kvnr_canonical_form_parses() {
        assert_eq!(parse_de_kvnr("A123456780"), Some("A123456780".into()));
    }

    #[test]
    fn de_kvnr_accepts_lowercase_letter_canonicalises_to_upper() {
        assert_eq!(parse_de_kvnr("a123456780"), Some("A123456780".into()));
    }

    #[test]
    fn de_kvnr_accepts_internal_whitespace() {
        assert_eq!(parse_de_kvnr("A 123 456 780"), Some("A123456780".into()));
    }

    #[test]
    fn de_kvnr_second_valid_vector() {
        assert_eq!(parse_de_kvnr("B987654320"), Some("B987654320".into()));
    }

    #[test]
    fn de_kvnr_rejects_wrong_check_digit() {
        assert_eq!(parse_de_kvnr("A123456789"), None);
    }

    #[test]
    fn de_kvnr_rejects_missing_letter() {
        assert_eq!(parse_de_kvnr("1234567890"), None);
    }

    #[test]
    fn de_kvnr_rejects_wrong_length() {
        assert_eq!(parse_de_kvnr("A12345"), None);
        assert_eq!(parse_de_kvnr("A1234567890"), None);
        assert_eq!(parse_de_kvnr(""), None);
    }

    #[test]
    fn de_kvnr_rejects_non_digit_in_body() {
        assert_eq!(parse_de_kvnr("A12345A780"), None);
    }

    // ---------- parse_it_cf ----------

    #[test]
    fn it_cf_canonical_form_parses() {
        assert_eq!(
            parse_it_cf("RSSMRA85T10A562S"),
            Some("RSSMRA85T10A562S".into()),
        );
    }

    #[test]
    fn it_cf_accepts_lowercase_and_whitespace() {
        assert_eq!(
            parse_it_cf("rss mra 85t 10a 562s"),
            Some("RSSMRA85T10A562S".into()),
        );
    }

    #[test]
    fn it_cf_second_valid_vector() {
        assert_eq!(
            parse_it_cf("MNRMRC75H17H501I"),
            Some("MNRMRC75H17H501I".into()),
        );
    }

    #[test]
    fn it_cf_rejects_wrong_check_character() {
        assert_eq!(parse_it_cf("RSSMRA85T10A562X"), None);
    }

    #[test]
    fn it_cf_rejects_wrong_length() {
        assert_eq!(parse_it_cf("RSSMRA85T10A562"), None);
        assert_eq!(parse_it_cf("RSSMRA85T10A562SS"), None);
        assert_eq!(parse_it_cf(""), None);
    }

    #[test]
    fn it_cf_rejects_non_alphanumeric() {
        assert_eq!(parse_it_cf("RSSMRA85T10A562!"), None);
        assert_eq!(parse_it_cf("RSSMRA-85T-10A562S"), None);
    }

    // ---------- parse_nl_bsn ----------

    #[test]
    fn nl_bsn_canonical_form_parses() {
        assert_eq!(parse_nl_bsn("111222333"), Some("111222333".into()));
    }

    #[test]
    fn nl_bsn_second_valid_vector() {
        assert_eq!(parse_nl_bsn("123456782"), Some("123456782".into()));
    }

    #[test]
    fn nl_bsn_strips_separators() {
        assert_eq!(parse_nl_bsn("111 222 333"), Some("111222333".into()));
        assert_eq!(parse_nl_bsn("111-222-333"), Some("111222333".into()));
    }

    #[test]
    fn nl_bsn_rejects_wrong_eleven_test() {
        assert_eq!(parse_nl_bsn("111222334"), None);
    }

    #[test]
    fn nl_bsn_rejects_all_zeros() {
        assert_eq!(parse_nl_bsn("000000000"), None);
    }

    #[test]
    fn nl_bsn_rejects_wrong_length() {
        assert_eq!(parse_nl_bsn("12345"), None);
        assert_eq!(parse_nl_bsn("1234567890"), None);
        assert_eq!(parse_nl_bsn(""), None);
    }

    #[test]
    fn nl_bsn_rejects_letters() {
        assert_eq!(parse_nl_bsn("ABCDEFGHI"), None);
    }

    // ---------- parse_se_workernummer ----------

    #[test]
    fn se_pnr_ten_digit_form_parses() {
        assert_eq!(
            parse_se_workernummer("4603243850"),
            Some("4603243850".into()),
        );
    }

    #[test]
    fn se_pnr_with_separator_canonicalises_to_ten_digit() {
        assert_eq!(
            parse_se_workernummer("460324-3850"),
            Some("4603243850".into()),
        );
        assert_eq!(
            parse_se_workernummer("460324+3850"),
            Some("4603243850".into()),
        );
    }

    #[test]
    fn se_pnr_twelve_digit_form_preserves_century() {
        assert_eq!(
            parse_se_workernummer("19460324-3850"),
            Some("194603243850".into()),
        );
        assert_eq!(
            parse_se_workernummer("194603243850"),
            Some("194603243850".into()),
        );
    }

    #[test]
    fn se_pnr_second_valid_vector() {
        assert_eq!(
            parse_se_workernummer("8112310092"),
            Some("8112310092".into()),
        );
    }

    #[test]
    fn se_pnr_rejects_wrong_luhn() {
        assert_eq!(parse_se_workernummer("4603243851"), None);
    }

    #[test]
    fn se_pnr_rejects_wrong_length() {
        assert_eq!(parse_se_workernummer("12345"), None);
        assert_eq!(parse_se_workernummer("12345678901"), None);
        assert_eq!(parse_se_workernummer(""), None);
    }

    #[test]
    fn se_pnr_rejects_letters() {
        assert_eq!(parse_se_workernummer("ABCDEFGHIJ"), None);
    }

    // ---------- parse_au_ihi ----------

    #[test]
    fn au_ihi_canonical_form_parses() {
        assert_eq!(
            parse_au_ihi("8003601234567894"),
            Some("8003601234567894".into()),
        );
    }

    #[test]
    fn au_ihi_strips_whitespace() {
        assert_eq!(
            parse_au_ihi("8003 6012 3456 7894"),
            Some("8003601234567894".into()),
        );
    }

    #[test]
    fn au_ihi_second_valid_vector() {
        assert_eq!(
            parse_au_ihi("8003619876543213"),
            Some("8003619876543213".into()),
        );
    }

    #[test]
    fn au_ihi_rejects_wrong_luhn() {
        assert_eq!(parse_au_ihi("8003601234567890"), None);
    }

    #[test]
    fn au_ihi_rejects_wrong_length() {
        assert_eq!(parse_au_ihi("12345"), None);
        assert_eq!(parse_au_ihi("80036012345678941"), None);
        assert_eq!(parse_au_ihi(""), None);
    }

    #[test]
    fn au_ihi_rejects_letters() {
        assert_eq!(parse_au_ihi("ABCDEFGHIJKLMNOP"), None);
    }

    // ---------- parse_uk_chi_number ----------

    #[test]
    fn uk_chi_canonical_form_parses() {
        assert_eq!(parse_uk_chi_number("0101701233"), Some("0101701233".into()),);
    }

    #[test]
    fn uk_chi_strips_whitespace() {
        assert_eq!(
            parse_uk_chi_number("010 170 1233"),
            Some("0101701233".into()),
        );
    }

    #[test]
    fn uk_chi_second_valid_vector() {
        assert_eq!(parse_uk_chi_number("0101701241"), Some("0101701241".into()),);
    }

    #[test]
    fn uk_chi_rejects_wrong_check_digit() {
        assert_eq!(parse_uk_chi_number("0101701234"), None);
    }

    #[test]
    fn uk_chi_rejects_wrong_length() {
        assert_eq!(parse_uk_chi_number("12345"), None);
        assert_eq!(parse_uk_chi_number("01017012339"), None);
        assert_eq!(parse_uk_chi_number(""), None);
    }

    #[test]
    fn uk_chi_rejects_letters() {
        assert_eq!(parse_uk_chi_number("ABCDEFGHIJ"), None);
    }

    // ----------------------------------------------------------------------
    // Eighteen additional national workeral identifiers (T-27).
    // ----------------------------------------------------------------------

    // ---------- parse_be_nn ----------

    #[test]
    fn be_nn_canonical_form_parses() {
        assert_eq!(parse_be_nn("80010100107"), Some("80010100107".into()));
    }
    #[test]
    fn be_nn_strips_punctuation() {
        assert_eq!(parse_be_nn("80.01.01-001.07"), Some("80010100107".into()),);
    }
    #[test]
    fn be_nn_rejects_wrong_check() {
        assert_eq!(parse_be_nn("80010100100"), None);
    }
    #[test]
    fn be_nn_rejects_wrong_length() {
        assert_eq!(parse_be_nn("12345"), None);
        assert_eq!(parse_be_nn(""), None);
    }

    // ---------- parse_bg_egn ----------

    #[test]
    fn bg_egn_canonical_form_parses() {
        assert_eq!(parse_bg_egn("8001010013"), Some("8001010013".into()));
    }
    #[test]
    fn bg_egn_rejects_wrong_check() {
        assert_eq!(parse_bg_egn("8001010014"), None);
    }
    #[test]
    fn bg_egn_rejects_wrong_length() {
        assert_eq!(parse_bg_egn("80010100"), None);
        assert_eq!(parse_bg_egn(""), None);
    }

    // ---------- parse_cz_rc ----------

    #[test]
    fn cz_rc_ten_digit_divisible_by_eleven() {
        assert_eq!(parse_cz_rc("8001150014"), Some("8001150014".into()));
    }
    #[test]
    fn cz_rc_nine_digit_pre_1954_accepted_as_is() {
        assert_eq!(parse_cz_rc("800115001"), Some("800115001".into()));
    }
    #[test]
    fn cz_rc_rejects_wrong_check() {
        assert_eq!(parse_cz_rc("8001150015"), None);
    }
    #[test]
    fn cz_rc_rejects_bad_length() {
        assert_eq!(parse_cz_rc("12345"), None);
        assert_eq!(parse_cz_rc("12345678901"), None);
    }

    // ---------- parse_dk_cpr ----------

    #[test]
    fn dk_cpr_canonical_parses() {
        assert_eq!(parse_dk_cpr("1501801234"), Some("1501801234".into()));
    }
    #[test]
    fn dk_cpr_strips_separator() {
        assert_eq!(parse_dk_cpr("150180-1234"), Some("1501801234".into()));
    }
    #[test]
    fn dk_cpr_rejects_bad_length() {
        assert_eq!(parse_dk_cpr("12345"), None);
        assert_eq!(parse_dk_cpr(""), None);
    }

    // ---------- parse_ee_ik ----------

    #[test]
    fn ee_ik_canonical_form_parses() {
        assert_eq!(parse_ee_ik("48001150011"), Some("48001150011".into()));
    }
    #[test]
    fn ee_ik_rejects_wrong_check() {
        assert_eq!(parse_ee_ik("48001150012"), None);
    }
    #[test]
    fn ee_ik_rejects_bad_length() {
        assert_eq!(parse_ee_ik("4800115001"), None);
    }

    // ---------- parse_es_dni ----------

    #[test]
    fn es_dni_canonical_form_parses() {
        assert_eq!(parse_es_dni("12345678Z"), Some("12345678Z".into()));
    }
    #[test]
    fn es_dni_rejects_wrong_letter() {
        assert_eq!(parse_es_dni("12345678A"), None);
    }
    #[test]
    fn es_dni_lowercase_letter_canonicalises_upper() {
        assert_eq!(parse_es_dni("12345678z"), Some("12345678Z".into()));
    }
    #[test]
    fn es_dni_handles_nie_prefix_x() {
        // NIE X1234567L → number is "01234567" mod 23 = (01234567 % 23).
        // 1234567 mod 23: 23 × 53676 = 1234548. 1234567 - 1234548 = 19.
        // LETTERS[19] = 'L'. So "X1234567L" is valid.
        assert_eq!(parse_es_dni("X1234567L"), Some("X1234567L".into()));
    }

    // ---------- parse_fi_hetu ----------

    #[test]
    fn fi_hetu_canonical_form_parses() {
        assert_eq!(parse_fi_hetu("150180-999B"), Some("150180-999B".into()));
    }
    #[test]
    fn fi_hetu_rejects_wrong_check() {
        assert_eq!(parse_fi_hetu("150180-999C"), None);
    }
    #[test]
    fn fi_hetu_rejects_bad_length() {
        assert_eq!(parse_fi_hetu("12345"), None);
    }

    // ---------- parse_hr_oib ----------

    #[test]
    fn hr_oib_canonical_form_parses() {
        assert_eq!(parse_hr_oib("12345678903"), Some("12345678903".into()));
    }
    #[test]
    fn hr_oib_rejects_wrong_check() {
        assert_eq!(parse_hr_oib("12345678901"), None);
    }
    #[test]
    fn hr_oib_rejects_bad_length() {
        assert_eq!(parse_hr_oib("123456789"), None);
    }

    // ---------- parse_is_kt ----------

    #[test]
    fn is_kt_canonical_form_parses() {
        assert_eq!(parse_is_kt("1501802529"), Some("1501802529".into()));
    }
    #[test]
    fn is_kt_rejects_wrong_check() {
        assert_eq!(parse_is_kt("1501802539"), None);
    }
    #[test]
    fn is_kt_rejects_bad_length() {
        assert_eq!(parse_is_kt("12345"), None);
    }

    // ---------- parse_lt_ak ----------

    #[test]
    fn lt_ak_canonical_form_parses() {
        assert_eq!(parse_lt_ak("48001150011"), Some("48001150011".into()));
    }
    #[test]
    fn lt_ak_rejects_wrong_check() {
        assert_eq!(parse_lt_ak("48001150012"), None);
    }

    // ---------- parse_lv_pk ----------

    #[test]
    fn lv_pk_canonical_form_parses() {
        assert_eq!(parse_lv_pk("15018010007"), Some("15018010007".into()));
    }
    #[test]
    fn lv_pk_rejects_wrong_check() {
        assert_eq!(parse_lv_pk("15018010008"), None);
    }
    #[test]
    fn lv_pk_rejects_bad_length() {
        assert_eq!(parse_lv_pk("1501801000"), None);
    }

    // ---------- parse_mt_id ----------

    #[test]
    fn mt_id_canonical_form_parses() {
        assert_eq!(parse_mt_id("1234567M"), Some("1234567M".into()));
    }
    #[test]
    fn mt_id_accepts_all_valid_letters() {
        for letter in ['M', 'G', 'A', 'P', 'L', 'H', 'B', 'Z'] {
            let s = format!("1234567{letter}");
            assert!(parse_mt_id(&s).is_some(), "letter {letter} should be valid");
        }
    }
    #[test]
    fn mt_id_rejects_invalid_letter() {
        assert_eq!(parse_mt_id("1234567X"), None);
        assert_eq!(parse_mt_id("1234567K"), None);
    }
    #[test]
    fn mt_id_rejects_bad_length() {
        assert_eq!(parse_mt_id("12345M"), None);
    }

    // ---------- parse_no_fnr ----------

    #[test]
    fn no_fnr_canonical_form_parses() {
        assert_eq!(parse_no_fnr("15018012399"), Some("15018012399".into()));
    }
    #[test]
    fn no_fnr_rejects_wrong_check() {
        assert_eq!(parse_no_fnr("15018012390"), None);
        assert_eq!(parse_no_fnr("15018012398"), None);
    }
    #[test]
    fn no_fnr_rejects_bad_length() {
        assert_eq!(parse_no_fnr("12345"), None);
    }

    // ---------- parse_pl_pesel ----------

    #[test]
    fn pl_pesel_canonical_form_parses() {
        assert_eq!(parse_pl_pesel("80011500014"), Some("80011500014".into()));
    }
    #[test]
    fn pl_pesel_rejects_wrong_check() {
        assert_eq!(parse_pl_pesel("80011500015"), None);
    }
    #[test]
    fn pl_pesel_rejects_bad_length() {
        assert_eq!(parse_pl_pesel("1234"), None);
    }

    // ---------- parse_ro_cnp ----------

    #[test]
    fn ro_cnp_canonical_form_parses() {
        assert_eq!(parse_ro_cnp("1800115400012"), Some("1800115400012".into()));
    }
    #[test]
    fn ro_cnp_rejects_wrong_check() {
        assert_eq!(parse_ro_cnp("1800115400015"), None);
    }
    #[test]
    fn ro_cnp_rejects_bad_length() {
        assert_eq!(parse_ro_cnp("180011540001"), None);
    }

    // ---------- parse_si_emso ----------

    #[test]
    fn si_emso_canonical_form_parses() {
        assert_eq!(parse_si_emso("1501980500015"), Some("1501980500015".into()));
    }
    #[test]
    fn si_emso_rejects_wrong_check() {
        assert_eq!(parse_si_emso("1501980500014"), None);
    }

    // ---------- parse_sk_rc ----------

    #[test]
    fn sk_rc_canonical_form_parses() {
        assert_eq!(parse_sk_rc("8051150019"), Some("8051150019".into()));
    }
    #[test]
    fn sk_rc_rejects_wrong_check() {
        assert_eq!(parse_sk_rc("8051150010"), None);
    }

    // ---------- parse_uk_nino ----------

    #[test]
    fn uk_nino_canonical_form_parses() {
        assert_eq!(parse_uk_nino("AB123456A"), Some("AB123456A".into()));
    }
    #[test]
    fn uk_nino_accepts_lowercase_and_whitespace() {
        assert_eq!(parse_uk_nino("ab 12 34 56 a"), Some("AB123456A".into()),);
    }
    #[test]
    fn uk_nino_rejects_banned_first_letter() {
        for ch in ['D', 'F', 'I', 'Q', 'U', 'V'] {
            let s = format!("{ch}A123456A");
            assert!(parse_uk_nino(&s).is_none(), "letter {ch} should be banned");
        }
    }
    #[test]
    fn uk_nino_rejects_banned_admin_prefix() {
        for prefix in ["OO", "CR", "FY", "MW", "NC", "PP", "PZ", "TN"] {
            let s = format!("{prefix}123456A");
            assert!(
                parse_uk_nino(&s).is_none(),
                "prefix {prefix} should be banned"
            );
        }
    }
    #[test]
    fn uk_nino_rejects_bad_suffix() {
        for ch in ['E', 'F', 'X', 'Z'] {
            let s = format!("AB123456{ch}");
            assert!(parse_uk_nino(&s).is_none(), "suffix {ch} should be invalid");
        }
    }
    #[test]
    fn uk_nino_rejects_bad_length() {
        assert_eq!(parse_uk_nino("AB12345A"), None);
    }

    // ----------------------------------------------------------------------
    // T-28: Five additional workeral identifiers.
    // ----------------------------------------------------------------------

    // ---------- parse_gr_dss ----------

    #[test]
    fn gr_dss_canonical_form_parses() {
        assert_eq!(parse_gr_dss("1234567890"), Some("1234567890".into()));
    }
    #[test]
    fn gr_dss_strips_punctuation() {
        assert_eq!(parse_gr_dss("12 34-56 78 90"), Some("1234567890".into()));
    }
    #[test]
    fn gr_dss_rejects_bad_length() {
        assert_eq!(parse_gr_dss("12345"), None);
        assert_eq!(parse_gr_dss("12345678901"), None);
        assert_eq!(parse_gr_dss(""), None);
    }
    #[test]
    fn gr_dss_rejects_letters() {
        assert_eq!(parse_gr_dss("ABCDEFGHIJ"), None);
    }

    // ---------- parse_li_id ----------

    #[test]
    fn li_id_eight_digit_form_parses() {
        assert_eq!(parse_li_id("ID12345678"), Some("ID12345678".into()));
    }
    #[test]
    fn li_id_nine_digit_example_from_spec_parses() {
        assert_eq!(parse_li_id("ID022143586"), Some("ID022143586".into()));
    }
    #[test]
    fn li_id_lowercase_letters_uppercased() {
        assert_eq!(parse_li_id("id12345678"), Some("ID12345678".into()));
    }
    #[test]
    fn li_id_rejects_missing_letters() {
        assert_eq!(parse_li_id("1234567890"), None);
        assert_eq!(parse_li_id("I12345678"), None); // only one leading letter
    }
    #[test]
    fn li_id_rejects_bad_length() {
        assert_eq!(parse_li_id(""), None);
        assert_eq!(parse_li_id("ID1234"), None);
        assert_eq!(parse_li_id("ID123456789012"), None);
    }

    // ---------- parse_nl_id ----------

    #[test]
    fn nl_id_canonical_form_parses() {
        assert_eq!(parse_nl_id("AB1234567"), Some("AB1234567".into()));
    }
    #[test]
    fn nl_id_lowercase_and_whitespace_canonicalise() {
        assert_eq!(parse_nl_id("ab 12 34 567"), Some("AB1234567".into()));
    }
    #[test]
    fn nl_id_rejects_letter_o_in_disallowed_positions() {
        assert_eq!(parse_nl_id("AO1234567"), None);
        assert_eq!(parse_nl_id("OB1234567"), None);
        assert_eq!(parse_nl_id("ABO234567"), None);
    }
    #[test]
    fn nl_id_allows_digit_zero() {
        assert_eq!(parse_nl_id("AB0234567"), Some("AB0234567".into()));
    }
    #[test]
    fn nl_id_rejects_bad_shape() {
        assert_eq!(parse_nl_id("12345AB67"), None);
        assert_eq!(parse_nl_id("AB12345AB"), None);
        assert_eq!(parse_nl_id(""), None);
    }

    // ---------- parse_pl_nip ----------

    #[test]
    fn pl_nip_canonical_form_parses() {
        assert_eq!(parse_pl_nip("1234567802"), Some("1234567802".into()));
    }
    #[test]
    fn pl_nip_strips_separators() {
        assert_eq!(parse_pl_nip("123-456-78-02"), Some("1234567802".into()));
    }
    #[test]
    fn pl_nip_rejects_wrong_check() {
        assert_eq!(parse_pl_nip("1234567803"), None);
    }
    #[test]
    fn pl_nip_rejects_check_value_ten_per_spec() {
        // For "123456789" body the weighted sum mod 11 is 10, which the
        // Polish NIP spec defines as invalid.
        assert_eq!(parse_pl_nip("1234567890"), None);
    }
    #[test]
    fn pl_nip_rejects_bad_length() {
        assert_eq!(parse_pl_nip("12345"), None);
    }

    // ---------- parse_pt_nif ----------

    #[test]
    fn pt_nif_canonical_form_parses() {
        assert_eq!(parse_pt_nif("123456789"), Some("123456789".into()));
    }
    #[test]
    fn pt_nif_rejects_wrong_check() {
        assert_eq!(parse_pt_nif("123456780"), None);
    }
    #[test]
    fn pt_nif_rejects_bad_length() {
        assert_eq!(parse_pt_nif("12345"), None);
        assert_eq!(parse_pt_nif("1234567890"), None);
    }

    // ----------------------------------------------------------------------
    // T-17.1: Seven next-batch national identifier schemes.
    // ----------------------------------------------------------------------

    // ---------- parse_br_cpf ----------
    #[test]
    fn br_cpf_canonical_form_parses() {
        assert_eq!(parse_br_cpf("12345678909"), Some("12345678909".into()));
    }
    #[test]
    fn br_cpf_formatted_input_strips_punctuation() {
        assert_eq!(parse_br_cpf("123.456.789-09"), Some("12345678909".into()));
    }
    #[test]
    fn br_cpf_rejects_wrong_check() {
        assert_eq!(parse_br_cpf("12345678900"), None);
    }
    #[test]
    fn br_cpf_rejects_all_equal_sequences() {
        for d in '0'..='9' {
            let s: String = std::iter::repeat_n(d, 11).collect();
            assert_eq!(parse_br_cpf(&s), None, "{s}");
        }
    }
    #[test]
    fn br_cpf_rejects_bad_length() {
        assert_eq!(parse_br_cpf("1234567890"), None);
        assert_eq!(parse_br_cpf("123456789090"), None);
    }
    #[test]
    fn br_cpf_rejects_non_digit_only_input() {
        assert_eq!(parse_br_cpf("abcdefghijk"), None);
    }

    // ---------- parse_cn_rrn ----------
    #[test]
    fn cn_rrn_canonical_form_parses() {
        assert_eq!(
            parse_cn_rrn("11010519491231002X"),
            Some("11010519491231002X".into()),
        );
    }
    #[test]
    fn cn_rrn_uppercases_lowercase_x() {
        assert_eq!(
            parse_cn_rrn("11010519491231002x"),
            Some("11010519491231002X".into()),
        );
    }
    #[test]
    fn cn_rrn_rejects_wrong_check_char() {
        assert_eq!(parse_cn_rrn("11010519491231002Y"), None);
        assert_eq!(parse_cn_rrn("110105194912310020"), None);
    }
    #[test]
    fn cn_rrn_rejects_invalid_date_substring() {
        assert_eq!(parse_cn_rrn("11010513491231002X"), None);
        assert_eq!(parse_cn_rrn("110105194913320002X"), None);
    }
    #[test]
    fn cn_rrn_rejects_bad_length() {
        assert_eq!(parse_cn_rrn("11010519491231"), None);
        assert_eq!(parse_cn_rrn("11010519491231002XY"), None);
    }
    #[test]
    fn cn_rrn_rejects_non_alnum_letters() {
        // A non-X non-digit at the check position is rejected.
        assert_eq!(parse_cn_rrn("11010519491231002A"), None);
    }

    // ---------- parse_in_aadhaar ----------
    #[test]
    fn in_aadhaar_canonical_form_parses() {
        assert_eq!(
            parse_in_aadhaar("234123412346"),
            Some("234123412346".into())
        );
    }
    #[test]
    fn in_aadhaar_strips_whitespace() {
        assert_eq!(
            parse_in_aadhaar("2341 2341 2346"),
            Some("234123412346".into()),
        );
    }
    #[test]
    fn in_aadhaar_rejects_wrong_verhoeff_check() {
        assert_eq!(parse_in_aadhaar("234123412347"), None);
        assert_eq!(parse_in_aadhaar("234123412345"), None);
    }
    #[test]
    fn in_aadhaar_rejects_all_equal_sequences() {
        for d in '2'..='9' {
            let s: String = std::iter::repeat_n(d, 12).collect();
            assert_eq!(parse_in_aadhaar(&s), None, "{s}");
        }
    }
    #[test]
    fn in_aadhaar_rejects_reserved_prefixes() {
        // UIDAI never issues numbers starting with 0 or 1.
        assert_eq!(parse_in_aadhaar("034123412346"), None);
        assert_eq!(parse_in_aadhaar("134123412346"), None);
    }
    #[test]
    fn in_aadhaar_rejects_bad_length() {
        assert_eq!(parse_in_aadhaar("234123412"), None);
        assert_eq!(parse_in_aadhaar("2341234123466"), None);
    }

    // ---------- parse_jp_my_number ----------
    #[test]
    fn jp_my_number_canonical_form_parses() {
        assert_eq!(
            parse_jp_my_number("123456789018"),
            Some("123456789018".into()),
        );
    }
    #[test]
    fn jp_my_number_strips_whitespace() {
        assert_eq!(
            parse_jp_my_number("1234 5678 9018"),
            Some("123456789018".into()),
        );
    }
    #[test]
    fn jp_my_number_rejects_wrong_check() {
        assert_eq!(parse_jp_my_number("123456789010"), None);
        assert_eq!(parse_jp_my_number("123456789019"), None);
    }
    #[test]
    fn jp_my_number_rejects_bad_length() {
        assert_eq!(parse_jp_my_number("12345678901"), None);
        assert_eq!(parse_jp_my_number("1234567890123"), None);
    }
    #[test]
    fn jp_my_number_rejects_non_digit_only_input() {
        assert_eq!(parse_jp_my_number("abcdefghijkl"), None);
    }

    // ---------- parse_mx_curp ----------
    #[test]
    fn mx_curp_canonical_form_parses() {
        assert_eq!(
            parse_mx_curp("HEGG560427MVZRRL04"),
            Some("HEGG560427MVZRRL04".into()),
        );
    }
    #[test]
    fn mx_curp_uppercases_input() {
        assert_eq!(
            parse_mx_curp("hegg560427mvzrrl04"),
            Some("HEGG560427MVZRRL04".into()),
        );
    }
    #[test]
    fn mx_curp_rejects_wrong_check() {
        assert_eq!(parse_mx_curp("HEGG560427MVZRRL05"), None);
    }
    #[test]
    fn mx_curp_rejects_invalid_date_substring() {
        assert_eq!(parse_mx_curp("HEGG561327MVZRRL04"), None);
        assert_eq!(parse_mx_curp("HEGG569927MVZRRL04"), None);
    }
    #[test]
    fn mx_curp_rejects_bad_sex_char() {
        assert_eq!(parse_mx_curp("HEGG560427XVZRRL04"), None);
    }
    #[test]
    fn mx_curp_rejects_bad_length() {
        assert_eq!(parse_mx_curp("HEGG560427"), None);
        assert_eq!(parse_mx_curp("HEGG560427MVZRRL04EXTRA"), None);
    }

    // ---------- parse_nz_nhi ----------
    #[test]
    fn nz_nhi_canonical_form_parses() {
        assert_eq!(parse_nz_nhi("ZAA0083"), Some("ZAA0083".into()));
    }
    #[test]
    fn nz_nhi_uppercases_input() {
        assert_eq!(parse_nz_nhi("zaa0083"), Some("ZAA0083".into()));
    }
    #[test]
    fn nz_nhi_rejects_wrong_check() {
        assert_eq!(parse_nz_nhi("ZAA0082"), None);
    }
    #[test]
    fn nz_nhi_rejects_excluded_letters_i_and_o() {
        assert_eq!(parse_nz_nhi("ZAI0083"), None);
        assert_eq!(parse_nz_nhi("ZAO0083"), None);
        assert_eq!(parse_nz_nhi("IZA0083"), None);
    }
    #[test]
    fn nz_nhi_rejects_bad_length() {
        assert_eq!(parse_nz_nhi("ZAA008"), None);
        assert_eq!(parse_nz_nhi("ZAA00830"), None);
    }
    #[test]
    fn nz_nhi_rejects_non_letter_prefix() {
        assert_eq!(parse_nz_nhi("Z1A0083"), None);
    }

    // ---------- parse_za_id ----------
    #[test]
    fn za_id_canonical_form_parses() {
        assert_eq!(parse_za_id("8001015009087"), Some("8001015009087".into()));
    }
    #[test]
    fn za_id_strips_whitespace() {
        assert_eq!(parse_za_id("800101 5009 087"), Some("8001015009087".into()),);
    }
    #[test]
    fn za_id_rejects_wrong_luhn() {
        assert_eq!(parse_za_id("8001015009088"), None);
        assert_eq!(parse_za_id("8001015009086"), None);
    }
    #[test]
    fn za_id_rejects_invalid_date_substring() {
        assert_eq!(parse_za_id("8013015009087"), None);
        assert_eq!(parse_za_id("8002305009087"), None);
    }
    #[test]
    fn za_id_rejects_bad_length() {
        assert_eq!(parse_za_id("80010150090"), None);
        assert_eq!(parse_za_id("80010150090870"), None);
    }

    // ----------------------------------------------------------------------
    // T-28: Nine per-country passport-number format validators.
    // ----------------------------------------------------------------------

    #[test]
    fn cy_passport_pre_2010_form_parses() {
        assert_eq!(parse_cy_passport("E123456"), Some("E123456".into()));
    }
    #[test]
    fn cy_passport_biometric_form_parses() {
        assert_eq!(parse_cy_passport("K12345678"), Some("K12345678".into()));
    }
    #[test]
    fn cy_passport_rejects_wrong_prefix() {
        assert_eq!(parse_cy_passport("A123456"), None);
        assert_eq!(parse_cy_passport("Z12345678"), None);
    }
    #[test]
    fn cy_passport_rejects_bad_length() {
        assert_eq!(parse_cy_passport("E12345"), None);
        assert_eq!(parse_cy_passport("K1234567"), None);
    }

    #[test]
    fn cz_passport_eight_digit_form_parses() {
        assert_eq!(parse_cz_passport("12345678"), Some("12345678".into()));
    }
    #[test]
    fn cz_passport_accepts_longer_forms() {
        assert_eq!(
            parse_cz_passport("123456789012"),
            Some("123456789012".into())
        );
    }
    #[test]
    fn cz_passport_rejects_short_forms() {
        assert_eq!(parse_cz_passport("1234567"), None);
        assert_eq!(parse_cz_passport(""), None);
    }

    #[test]
    fn li_passport_canonical_form_parses() {
        assert_eq!(parse_li_passport("R00536"), Some("R00536".into()));
    }
    #[test]
    fn li_passport_lowercases_to_upper() {
        assert_eq!(parse_li_passport("r00536"), Some("R00536".into()));
    }
    #[test]
    fn li_passport_rejects_bad_format() {
        assert_eq!(parse_li_passport("RR0536"), None);
        assert_eq!(parse_li_passport("123456"), None);
    }

    #[test]
    fn lt_passport_eight_digit_parses() {
        assert_eq!(parse_lt_passport("12345678"), Some("12345678".into()));
    }
    #[test]
    fn lt_passport_rejects_wrong_length() {
        assert_eq!(parse_lt_passport("1234567"), None);
        assert_eq!(parse_lt_passport("123456789"), None);
    }

    #[test]
    fn mt_passport_seven_digit_parses() {
        assert_eq!(parse_mt_passport("1234567"), Some("1234567".into()));
    }
    #[test]
    fn mt_passport_rejects_letters() {
        assert_eq!(parse_mt_passport("123456M"), None);
    }

    #[test]
    fn nl_passport_uses_nl_id_format() {
        assert_eq!(parse_nl_passport("AB1234567"), Some("AB1234567".into()));
        assert_eq!(parse_nl_passport("AO1234567"), None);
    }

    #[test]
    fn pt_passport_canonical_form_parses() {
        assert_eq!(parse_pt_passport("A123456"), Some("A123456".into()));
    }
    #[test]
    fn pt_passport_rejects_bad_shape() {
        assert_eq!(parse_pt_passport("AA12345"), None);
        assert_eq!(parse_pt_passport("1234567"), None);
    }

    #[test]
    fn ro_passport_canonical_form_parses() {
        assert_eq!(parse_ro_passport("AB123456"), Some("AB123456".into()));
    }
    #[test]
    fn ro_passport_rejects_bad_shape() {
        assert_eq!(parse_ro_passport("A1234567"), None);
        assert_eq!(parse_ro_passport("ABC12345"), None);
    }

    #[test]
    fn sk_passport_canonical_form_parses() {
        assert_eq!(parse_sk_passport("AB1234567"), Some("AB1234567".into()));
    }
    #[test]
    fn sk_passport_rejects_bad_shape() {
        assert_eq!(parse_sk_passport("ABC123456"), None);
        assert_eq!(parse_sk_passport("AB12345"), None);
    }
}