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
//! MRZ emission for all five ICAO 9303 formats — the deterministic inverse of
//! [`crate::parse_td3`], [`crate::parse_td2`], [`crate::parse_td1`],
//! [`crate::parse_mrv_a`], and [`crate::parse_mrv_b`].
//!
//! Given structured field values, [`format_td3`], [`format_td2`],
//! [`format_td1`], [`format_mrv_a`], and [`format_mrv_b`] produce the
//! ICAO-specified lines with every check digit computed via the same
//! [`crate::check_digit`] math the parsers verify against. Feeding the output
//! back through the matching `parse_*` function yields `valid() == true`
//! when the document code is accepted by that parser.
//!
//! Field widths and offsets mirror the parsers exactly:
//! - **TD3** (two 44-char lines): document code (2) + issuing country (3) +
//! name field (39) on line 1; document number (9) + check (1) +
//! nationality (3) + date of birth (6) + check (1) + sex (1) + date of
//! expiry (6) + check (1) + personal number (14) + check (1) + composite
//! check (1) on line 2.
//! - **TD2** (two 36-char lines): document code (2) + issuing country (3) +
//! name field (31) on line 1; document number (9) + check (1) +
//! nationality (3) + date of birth (6) + check (1) + sex (1) + date of
//! expiry (6) + check (1) + optional data (7) + composite check (1) on
//! line 2. TD2 has no separate check digit over the optional-data field.
//! - **TD1** (three 30-char lines): document code (2) + issuing country (3) +
//! document number (9) + check (1) + optional data 1 (15) on line 1; date
//! of birth (6) + check (1) + sex (1) + date of expiry (6) + check (1) +
//! nationality (3) + optional data 2 (11) + composite check (1) on line 2;
//! name field (30) on line 3. TD1 has no separate check digit over either
//! optional-data field.
//! - **MRV-A** (two 44-char lines): document code (2, `"V"`) + issuing country
//! (3) + name field (39) on line 1; document number (9) + check (1) +
//! nationality (3) + date of birth (6) + check (1) + sex (1) + date of
//! expiry (6) + check (1) + optional data (16) on line 2. No personal-number
//! check digit and no composite check digit — MRVs don't have one.
//! - **MRV-B** (two 36-char lines): document code (2, `"V"`) + issuing country
//! (3) + name field (31) on line 1; document number (9) + check (1) +
//! nationality (3) + date of birth (6) + check (1) + sex (1) + date of
//! expiry (6) + check (1) + optional data (8) on line 2. Same as MRV-A: no
//! personal-number check digit, no composite check digit.
//!
//! Dates and sex use the parser's typed MRZ vocabulary. Other fields use
//! MRZ-native text; names are transliterated and fixed-width fields are
//! cleaned and padded deterministically.
//! Non-name text fields map characters outside `[A-Z0-9]` to filler `<`
//! and are padded or truncated to their fixed widths. Typed dates and sex
//! write their six and one zone characters directly.
//!
//! The `surname`/`given_names` pair feeding the name field is the one
//! exception to the blanket "non-alphanumeric maps to filler" rule above:
//! ICAO 9303 Part 3 §4.6 defines punctuation-specific handling for names
//! (`knowledge/docs9303/Doc_9303_Part3_Specs_Common_to_all_MRTDs.md:509-535`)
//! that an apostrophe is dropped with no filler, not mapped to one — see
//! [`clean_name_half`] for the full rule set. Every other field
//! (`document_number`, `personal_number`/`optional_data`) keeps the blanket
//! [`clean`] behavior, per that same Part 3 passage read together with
//! `Doc_9303_Part4_Specs_for_MRPs_and_TD3_MRTDs.md:434`, which specifies
//! those fields as flat alphanumeric-or-filler, with no name-style
//! punctuation carve-out.
use cratecheck_digit;
use crate::;
use ;
/// Raw TD3 sub-fields, in MRZ-native form.
///
/// Dates and sex use the parser's typed MRZ vocabulary; states are
/// 3-letter ICAO codes. Non-name text fields are cleaned and padded or
/// truncated to their exact widths, so emitting
/// never panics and never produces a line of the wrong length. The two name
/// fields are the exception: they are encoded per Doc 9303 Part 3 §4.6 and
/// transliterated per §6 A/§6 B, exactly as [`encode_name_component`] does,
/// and a name too long for the field is truncated the way ICAO prescribes.
///
/// A `document_number` longer than the 9-character field overflows into the
/// personal-number field when the remainder fits (up to 12 more characters);
/// see [`MrzData::full_document_number`](crate::MrzData::full_document_number).
/// Otherwise it is truncated to 9. `personal_number` holds up to 14 characters.
///
/// ```
/// use mrz::Td3Fields;
///
/// // Struct-update syntax is the intended way to build one: unset fields
/// // emit as fillers, and the document code defaults to "P".
/// let fields = Td3Fields {
/// issuing_country: "UTO".into(),
/// surname: "Eriksson".into(),
/// given_names: "Anna Maria".into(),
/// ..Default::default()
/// };
/// assert_eq!(fields.document_code, "P");
///
/// let zone = mrz::format_td3(&fields);
/// assert!(zone.starts_with("P<UTOERIKSSON<<ANNA<MARIA<<"));
/// ```
/// **Deliberately not `#[non_exhaustive]`.** This struct mirrors a field
/// layout ICAO 9303 fixes, so it does not grow the way [`crate::Checks`] or
/// [`crate::MrzData`] do, and callers build it with a struct expression as
/// the examples above show. **Put any future tunable in a separate
/// `#[non_exhaustive]` companion rather than adding a field here** -- the
/// same separation [`crate::ParseOptions`] already has from the data it
/// parses. Adding a field here is a breaking change and should stay one.
/// The printed sex cell. `M`, `F` and `<` come from [`Sex::zone_char`]. A
/// [`Sex::NonConformant`] cell is written as given when it is a character of
/// the MRZ alphabet (`A`-`Z`, `0`-`9`, `<`), so a parsed zone re-emits as it
/// was read. Any other character is written as `<`, as 0.7 wrote every
/// non-`M`/`F` value. The variant is public and can hold any `char`, and one
/// non-ASCII character would otherwise make the line the wrong length in bytes.
/// The six printed characters of a typed date, with the century dropped.
/// Map any character outside `[A-Z0-9]` (after uppercasing) to the filler `<`.
/// Clean and pad/truncate `s` to exactly `width` characters using `<` filler.
/// ICAO 9303 §4.6 encoding of one name component (a surname half or a given-
/// names half), as it would be printed into an MRZ name field: uppercased,
/// transliterated, apostrophes dropped with no filler, hyphens/commas/spaces
/// folded to a single `<`.
///
/// Transliteration covers Doc 9303 Part 3 §6 A (Latin national characters,
/// `Expanded` style) *and* §6 B (Cyrillic) — but §6 B is applied with its base
/// (≈ Russian) column, because this function has no language context. A name in
/// a language whose §6 B column differs (Serbian, Ukrainian, Bulgarian,
/// Macedonian, Belarusian) should be run through
/// [`transliterate_cyrillic`](crate::transliterate_cyrillic) with the right
/// [`CyrillicLanguage`](crate::CyrillicLanguage) first.
///
/// Public so a consumer can ask "could this visual-zone reading have produced
/// that MRZ name field?" using the same encoder that writes them, rather than
/// re-deriving the rules. `mrz::transliterate` alone is not a substitute — its
/// own doc comment notes it performs no filler handling and no truncation, so
/// a name containing an apostrophe or a separator round-trips through it
/// unchanged and then compares unequal to the MRZ form.
///
/// ```
/// use mrz::encode_name_component;
///
/// assert_eq!(encode_name_component("O'Connor"), "OCONNOR"); // apostrophe: dropped, no filler
/// assert_eq!(encode_name_component("Anna Maria"), "ANNA<MARIA"); // space: folded to one `<`
/// assert_eq!(encode_name_component("Müller"), "MUELLER"); // §6 A transliteration
/// assert_eq!(encode_name_component("Иванов"), "IVANOV"); // §6 B (base column)
/// ```
/// Clean one half (primary or secondary identifier) of a name field per
/// ICAO 9303 Part 3 §4.6
/// (`knowledge/docs9303/Doc_9303_Part3_Specs_Common_to_all_MRTDs.md:509-535`).
/// Unlike `clean`, this does **not** map every non-alphanumeric character
/// to the filler `<` — names get their own punctuation rules:
///
/// - Each character is first uppercased via `char::to_uppercase()` (not
/// `to_ascii_uppercase`), then, for **each** resulting char `u` (usually
/// one, but `ß` uppercases to `ẞ`, then Table A maps it to `SS`):
/// 1. if `u` is a Table A national character (Part 3 §6,
/// `:703-807`), it is replaced by its
/// [`Expanded`](crate::TransliterationStyle::Expanded)
/// transliteration — e.g. `Ü`→`UE`, `Ñ`→`N` — with **no** separator
/// pushed around it. This is the transliteration Part 3 `:493` mandates
/// ("The issuing State or organization shall transliterate national
/// characters using only the allowed OCR-B characters") and fixes what
/// was previously silent data loss: `MÜLLER` used to emit as `MLLER`,
/// now `MUELLER`.
/// 2. else if `u` is one of the 48 Cyrillic code points of Part 3 §6 B
/// (`:809-863`), it is replaced by its base-column (≈ Russian)
/// transliteration — e.g. `Ж`→`ZH` — for the same reason. This path has
/// no language context, so a name whose language's column differs must
/// be pre-transliterated with `transliterate_cyrillic`.
/// 3. else if `u` is `A`-`Z` or `0`-`9`, it is kept as-is.
/// 4. else the existing punctuation rules below apply to `u`.
/// - Hyphen (`:517-521`), comma (`:523-532`), and whitespace each become a
/// single separator filler `<`.
/// - **Apostrophe is dropped entirely, with no filler in its place**
/// (`:511-515`) — e.g. `O'CONNOR` becomes `OCONNOR`, not `O<CONNOR`. This
/// follows Part 3 §4.6.
/// - Every other punctuation character is likewise dropped entirely, with
/// no filler (`:534-535`).
/// - `0`-`9` is kept as-is. Part 3 §4.6 (`:507`) states plainly that
/// "numeric characters shall not be used in the name fields of the MRZ",
/// but — unlike the apostrophe/hyphen/comma cases above — it defines no
/// mapping for one that shows up anyway. Silently rewriting a digit to a
/// filler would erase information with no textual basis in the spec, so
/// this function instead preserves digits verbatim, matching the
/// "alphanumeric survives untouched" behavior `clean` already applies
/// to the document-number and optional-data fields. A digit reaching this
/// function means the caller handed it a name that isn't spec-clean;
/// callers should validate it before emission.
///
/// After the per-character pass, consecutive separators collapse to one and
/// leading/trailing separators are trimmed. That collapse is what makes
/// `"ANNA, MARIA"` (a comma *immediately followed by* a space) yield
/// `ANNA<MARIA` rather than `ANNA<<MARIA`, matching
/// `Doc_9303_Part3_Specs_Common_to_all_MRTDs.md:531-532`'s worked example.
/// Truncate the `primary`/`secondary` name-field components (already run
/// through [`clean_name_half`]) to fit exactly `width` characters, when
/// their combined length exceeds it.
///
/// ICAO 9303 Part 4 §4.2.3 (`Doc_9303_Part4_Specs_for_MRPs_and_TD3_MRTDs.md:482-606`)
/// illustrates several truncation strategies; Parts 5 and 6 §4.2.3
/// explicitly allow issuer discretion —
/// truncating trailing components to initials, truncating components to a
/// shorter fixed length, or dropping whole components from the end. Part 4
/// does not prefer any one of these, so *which* characters get removed is a
/// genuine choice.
///
/// **What is not a choice** is the `shall` in the Data Element Directory row
/// that §4.2.3 itself cross-references
/// (`Doc_9303_Part4_Specs_for_MRPs_and_TD3_MRTDs.md:426`, repeated verbatim
/// in Part 5:346, Part 6:299, Part 7:282 and Part 7:640):
///
/// > "Characters **shall** be removed from one or more components of the
/// > primary identifier until three character positions are freed, and two
/// > filler characters (`<<`) and the first character of the first component
/// > of the secondary identifier can be inserted. The last character shall be
/// > an alphabetic character (A through Z). ... **Further** truncation of the
/// > primary identifier **may** be carried out to allow characters of the
/// > secondary identifier to be included ... When the name consists of only a
/// > primary identifier which exceeds [the width], characters shall be removed
/// > from one or more components of the name until the last character in the
/// > name field is an alphabetic character."
///
/// So the normative rules this function guarantees are:
/// 1. a name that fits within `width` is never truncated — callers only reach
/// this function when it does not fit (`:465`);
/// 2. the **last character of the name field** is alphabetic `A`-`Z`, the
/// reader's only signal that truncation occurred (`:467`);
/// 3. when a secondary identifier exists, `<<` **and at least the first
/// character of its first component always survive** — the primary is
/// shortened to make room, never the other way around (`:407`);
/// 4. when there is no secondary identifier, the primary is shortened until
/// the field ends on a letter (`:407`).
///
/// Rule 3 is the one this function got wrong until the fix recorded in
/// `changelog.d/mrz-name-truncation-icao.fixed.md`: it filled left-to-right
/// and stopped at the first overflow, so a primary identifier that reached
/// `width` on its own consumed the whole field and the `<<` separator was
/// never emitted. `parse_*` then split on the single `<`
/// fallback and silently reported the *first* primary component as `surname`
/// with the remaining primary components as `given_names` — corruption, not
/// merely loss.
///
/// The strategy chosen for rule 3 reproduces ICAO's own §4.2.3.3(b) worked
/// example byte-for-byte (see [`shrink_primary`]). The narrower formats
/// (TD1 width 30, TD2/MRV-B width 31) run the same width-parameterised
/// algorithm and satisfy rules 1-4, but do **not** reproduce ICAO's
/// narrow-field illustrations byte-for-byte — those illustrations contradict
/// each other at the same width 31 (Part 6 §4.2.3.2(b) shows `<<D<P`, Part 7
/// §7.2.3.2(b) shows `<<DINGO`), so they are drawings of the discretion in
/// rule 3's "further truncation may", not an algorithm to match.
///
/// `Doc_9303_Part4_Specs_for_MRPs_and_TD3_MRTDs.md:604-606` documents that
/// this ambiguity runs both ways:
/// a name that happens to fill the field exactly, with no truncation at
/// all, is indistinguishable from a truncated one by the same rule — "this
/// name has not been truncated but it must be assumed that it has been
/// truncated". That is spec-documented ambiguity to design around, not a
/// bug to fix later; ICAO's own `PAPANDROPOULOUS` example
/// (`Doc_9303_Part4_Specs_for_MRPs_and_TD3_MRTDs.md:603`) is exactly this
/// case, and is pinned as a non-truncating golden vector in
/// `tests/icao_vectors.rs` for that reason.
/// Length of `parts` joined by a single `<`, without building the string.
/// (separator-before, component) pairs in emission order: the first component
/// has an empty separator, same-half components are joined by `<`, and the
/// primary/secondary boundary by `<<`.
/// [`component_pairs`] for a secondary identifier emitted on its own — the
/// first component carries the `<<` boundary separator.
/// Emit `pairs` left-to-right into at most `width` characters, cutting the
/// last component short rather than emitting a separator nothing follows, so
/// the result always ends on a letter (rule 2).
/// Shorten the primary identifier's `parts` to at most `budget` characters,
/// `<`-separated, always ending on a letter.
///
/// Reproduces ICAO 9303 Part 4 §4.2.3.3(b)
/// (`Doc_9303_Part4_Specs_for_MRPs_and_TD3_MRTDs.md:582-588`) byte-for-byte,
/// the width-39 example that truncates the primary unevenly and pins
/// per-component lengths. Those lengths are what fix the strategy below; they are not
/// derivable from the prose:
///
/// ```text
/// BENNELONG WOOLOOMOOLOO WARRANDYTE WARNAMBOOL -> lengths [9, 12, 10, 10]
/// budget 32 (= 39 - len("<<DINGO"))
/// keep the first component whole head = 9
/// water-fill the rest over 29 - 9 = 20:
/// cap 6 -> 6+6+6 = 18 <= 20 cap 7 -> 21 > 20 so cap = 6
/// spread the remaining 2 one at a time, left to right -> [7, 7, 6]
/// BENNELONG<WOOLOOM<WARRAND<WARNAM = 32
/// ```
///
/// The one-at-a-time spread is load-bearing: handing the whole remainder to
/// the first component that can take it yields `[8, 6, 6]`
/// (`BENNELONG<WOOLOOMOO<WARRAN<WARNAM`), which is a *conformant* reading but
/// not ICAO's, and the golden vector would fail.
/// Build the name field: `SURNAME<<GIVEN<NAMES`, padded/truncated to
/// `width`. Each half is cleaned independently by [`clean_name_half`] per
/// ICAO 9303 Part 3 §4.6 before joining — see that function's doc comment
/// for the punctuation rules — and, when the combined result overflows
/// `width`, [`truncate_name_components`] applies this crate's truncation
/// strategy (documented there).
/// The 9-character document-number field, its check-digit character, and any
/// prefix the overflow encoding forces onto the optional-data field.
/// Encode a document number, using the ICAO 9303 Part 5 note j / Part 6 note j
/// long-document-number overflow form when it exceeds 9 characters and the
/// remainder fits `optional_width`. Part 4 defines no such rule for TD3 —
/// applying it there is a deliberate extension by analogy; see
/// `parser::read_overflow`'s doc comment for the full citation.
///
/// The overflow form prints all nine principal characters, sets the
/// check-digit position to a filler (instead of a check digit, per Part 5
/// note j / §4.2.4) to signal a truncated number, and writes
/// `remainder + check digit over the whole number + a terminating filler` at
/// the start of the optional field. The check digit is computed over the nine
/// principal characters concatenated with the remainder — i.e. `cleaned`
/// itself. If the remainder cannot fit, the number is truncated to 9 as
/// before — this function never produces an over-long field.
/// Emit a TD3 (passport) MRZ: two 44-character lines joined by `\n`.
///
/// All four field check digits and the composite check digit are computed
/// from `fields`. With a document code accepted by the matching parser,
/// the result round-trips through
/// [`crate::parse_td3`] with `valid() == true` (see `tests/roundtrip.rs`).
///
/// ```
/// use mrz::{format_td3, parse_td3, Td3Fields};
///
/// let fields = Td3Fields {
/// document_code: "P".into(),
/// issuing_country: "UTO".into(),
/// document_number: "L898902C3".into(),
/// surname: "ERIKSSON".into(),
/// given_names: "ANNA MARIA".into(),
/// nationality: "UTO".into(),
/// date_of_birth: mrz::MrzDate::Calendar(mrz::Date::new(1974, 8, 12)),
/// sex: mrz::Sex::Female,
/// date_of_expiry: mrz::MrzDate::Calendar(mrz::Date::new(2012, 4, 15)),
/// personal_number: Some("ZE184226B".into()),
/// };
/// let mrz = format_td3(&fields);
/// let (l1, l2) = mrz.split_once('\n').unwrap();
/// assert!(parse_td3(l1, l2).unwrap().valid());
/// ```
///
/// # National characters
///
/// Characters that do not fit the MRZ's `[A-Z0-9<]` alphabet are
/// transliterated automatically, so a national name survives instead of being
/// silently deleted. Every emitter does this, through
/// [`encode_name_component`]:
///
/// - **Latin** — Doc 9303 Part 3 §6 A: `Ü`→`UE`, `É`→`E`, `ß`→`SS` via uppercase `ẞ` (Table A). Five
/// characters have more than one recommended form; the emitters use the
/// `Expanded` one. See [`crate::transliterate`] for the others.
/// - **Cyrillic** — Part 3 §6 B: `ИВАНОВ`→`IVANOV`. Twelve rows and five
/// word-initial rules depend on the name's *language*, which an emitter
/// cannot know, so it applies §6 B's base (≈ Russian) column. For a
/// Belarusian, Bulgarian, Macedonian, Serbian or Ukrainian name, run
/// [`crate::transliterate_cyrillic`] with the right
/// [`CyrillicLanguage`](crate::CyrillicLanguage) first.
///
/// Arabic (§6 C) is not implemented.
///
/// ```
/// use mrz::{format_td3, transliterate_cyrillic, CyrillicLanguage, Td3Fields};
///
/// let lines = format_td3(&Td3Fields {
/// issuing_country: "DEU".into(),
/// surname: "MÜLLER".into(),
/// given_names: "TÉRÈSA".into(),
/// ..Default::default()
/// });
/// let line1 = lines.split_once('\n').unwrap().0;
/// assert!(line1.contains("MUELLER"));
/// assert!(line1.contains("TERESA"));
///
/// // Cyrillic, base column: `Ж` becomes `ZH`.
/// let base = format_td3(&Td3Fields { surname: "Живков".into(), ..Default::default() });
/// assert!(base.starts_with("P<<<<ZHIVKOV<<"));
///
/// // The same name as a Serbian issuer writes it: `Ж` becomes `Z`.
/// let serbian = format_td3(&Td3Fields {
/// surname: transliterate_cyrillic("Живков", CyrillicLanguage::Serbian),
/// ..Default::default()
/// });
/// assert!(serbian.starts_with("P<<<<ZIVKOV<<"));
/// ```
/// Raw TD2 sub-fields, in MRZ-native form — the same input conventions as
/// [`Td3Fields`].
///
/// A `document_number` longer than the 9-character field overflows into
/// `optional_data` when the remainder fits (up to 5 more characters, per
/// Doc 9303 Part 6 note j); otherwise it is truncated to 9.
///
/// ```
/// use mrz::{format_td2, Td2Fields};
///
/// let fields = Td2Fields {
/// issuing_country: "UTO".into(),
/// document_number: "D23145890".into(),
/// surname: "ERIKSSON".into(),
/// given_names: "ANNA MARIA".into(),
/// nationality: "UTO".into(),
/// date_of_birth: mrz::MrzDate::Calendar(mrz::Date::new(1974, 8, 12)),
/// sex: mrz::Sex::Female,
/// date_of_expiry: mrz::MrzDate::Calendar(mrz::Date::new(2012, 4, 15)),
/// ..Default::default()
/// };
/// assert_eq!(fields.document_code, "I"); // the default document code
///
/// // ICAO 9303 Part 6's published specimen, byte for byte.
/// assert_eq!(
/// format_td2(&fields),
/// "I<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<\nD231458907UTO7408122F1204159<<<<<<<6",
/// );
/// ```
/// **Deliberately not `#[non_exhaustive]`.** This struct mirrors a field
/// layout ICAO 9303 fixes, so it does not grow the way [`crate::Checks`] or
/// [`crate::MrzData`] do, and callers build it with a struct expression as
/// the examples above show. **Put any future tunable in a separate
/// `#[non_exhaustive]` companion rather than adding a field here** -- the
/// same separation [`crate::ParseOptions`] already has from the data it
/// parses. Adding a field here is a breaking change and should stay one.
/// Emit a TD2 (identity-card) MRZ: two 36-character lines joined by `\n`.
///
/// The document number, date-of-birth, date-of-expiry, and composite check
/// digits are computed from `fields`. With a document code accepted by the matching parser,
/// the result round-trips through
/// [`crate::parse_td2`] with `valid() == true` (see `tests/roundtrip.rs`).
/// TD2 has no separate check digit over the optional-data field.
///
/// ```
/// use mrz::{format_td2, parse_td2, Td2Fields};
///
/// let fields = Td2Fields {
/// document_number: "D23145890".into(),
/// surname: "ERIKSSON".into(),
/// given_names: "ANNA MARIA".into(),
/// nationality: "UTO".into(),
/// date_of_birth: mrz::MrzDate::Calendar(mrz::Date::new(1974, 8, 12)),
/// sex: mrz::Sex::Female,
/// date_of_expiry: mrz::MrzDate::Calendar(mrz::Date::new(2012, 4, 15)),
/// ..Td2Fields::default()
/// };
/// let mrz = format_td2(&fields);
/// let (l1, l2) = mrz.split_once('\n').unwrap();
/// assert!(parse_td2(l1, l2).unwrap().valid());
/// ```
/// Raw TD1 sub-fields, in MRZ-native form — the same input conventions as
/// [`Td3Fields`].
///
/// A `document_number` longer than the 9-character field overflows into
/// `optional_data_1` when the remainder fits (up to 13 more characters, per
/// Doc 9303 Part 5 note j / §4.2.4); otherwise it is truncated to 9.
///
/// ```
/// use mrz::{format_td1, Td1Fields};
///
/// let fields = Td1Fields {
/// issuing_country: "UTO".into(),
/// document_number: "D23145890".into(),
/// surname: "ERIKSSON".into(),
/// given_names: "ANNA MARIA".into(),
/// nationality: "UTO".into(),
/// date_of_birth: mrz::MrzDate::Calendar(mrz::Date::new(1974, 8, 12)),
/// sex: mrz::Sex::Female,
/// date_of_expiry: mrz::MrzDate::Calendar(mrz::Date::new(2012, 4, 15)),
/// ..Default::default()
/// };
/// assert_eq!(fields.document_code, "I"); // the default document code
///
/// // Three lines; the name moves to line 3 on an ID card.
/// assert_eq!(
/// format_td1(&fields),
/// "I<UTOD231458907<<<<<<<<<<<<<<<\n\
/// 7408122F1204159UTO<<<<<<<<<<<6\n\
/// ERIKSSON<<ANNA<MARIA<<<<<<<<<<",
/// );
/// ```
/// **Deliberately not `#[non_exhaustive]`.** This struct mirrors a field
/// layout ICAO 9303 fixes, so it does not grow the way [`crate::Checks`] or
/// [`crate::MrzData`] do, and callers build it with a struct expression as
/// the examples above show. **Put any future tunable in a separate
/// `#[non_exhaustive]` companion rather than adding a field here** -- the
/// same separation [`crate::ParseOptions`] already has from the data it
/// parses. Adding a field here is a breaking change and should stay one.
/// Emit a TD1 (ID-card) MRZ: three 30-character lines joined by `\n`.
///
/// The document number, date-of-birth, date-of-expiry, and composite check
/// digits are computed from `fields`. With a document code accepted by the matching parser,
/// the result round-trips through
/// [`crate::parse_td1`] with `valid() == true` (see `tests/roundtrip.rs`).
/// TD1 has no separate check digit over either optional-data field.
///
/// ```
/// use mrz::{format_td1, parse_td1, Td1Fields};
///
/// let fields = Td1Fields {
/// document_number: "D23145890".into(),
/// surname: "ERIKSSON".into(),
/// given_names: "ANNA MARIA".into(),
/// nationality: "UTO".into(),
/// date_of_birth: mrz::MrzDate::Calendar(mrz::Date::new(1974, 8, 12)),
/// sex: mrz::Sex::Female,
/// date_of_expiry: mrz::MrzDate::Calendar(mrz::Date::new(2012, 4, 15)),
/// ..Td1Fields::default()
/// };
/// let mrz = format_td1(&fields);
/// let mut lines = mrz.lines();
/// let l1 = lines.next().unwrap();
/// let l2 = lines.next().unwrap();
/// let l3 = lines.next().unwrap();
/// assert!(parse_td1(l1, l2, l3).unwrap().valid());
/// ```
/// Raw MRV-A sub-fields, in MRZ-native form — the same input conventions as
/// [`Td3Fields`].
///
/// Visas define no overflow encoding (Doc 9303 Part 7), so a `document_number`
/// longer than 9 characters is truncated to 9. MRV-A has no personal-number or
/// composite check digit — `optional_data` is free-form data up to 16
/// characters.
///
/// ```
/// use mrz::{format_mrv_a, MrvAFields};
///
/// let fields = MrvAFields {
/// issuing_country: "UTO".into(),
/// document_number: "L898902C".into(),
/// surname: "ERIKSSON".into(),
/// given_names: "ANNA MARIA".into(),
/// nationality: "UTO".into(),
/// date_of_birth: mrz::MrzDate::Calendar(mrz::Date::new(1969, 8, 6)),
/// sex: mrz::Sex::Female,
/// date_of_expiry: mrz::MrzDate::Calendar(mrz::Date::new(2094, 6, 23)), // ICAO meant 1994; century does not change `940623`.
/// optional_data: Some("ZE184226B".into()),
/// ..Default::default()
/// };
/// assert_eq!(fields.document_code, "V"); // the default document code
///
/// // ICAO 9303 Part 7's published MRV-A specimen, byte for byte.
/// assert_eq!(
/// format_mrv_a(&fields),
/// "V<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<\n\
/// L898902C<3UTO6908061F9406236ZE184226B<<<<<<<",
/// );
/// ```
/// **Deliberately not `#[non_exhaustive]`.** This struct mirrors a field
/// layout ICAO 9303 fixes, so it does not grow the way [`crate::Checks`] or
/// [`crate::MrzData`] do, and callers build it with a struct expression as
/// the examples above show. **Put any future tunable in a separate
/// `#[non_exhaustive]` companion rather than adding a field here** -- the
/// same separation [`crate::ParseOptions`] already has from the data it
/// parses. Adding a field here is a breaking change and should stay one.
/// Emit an MRV-A (machine readable visa) MRZ: two 44-character lines joined
/// by `\n`.
///
/// The document number, date-of-birth, and date-of-expiry check digits are
/// computed from `fields`. With a document code accepted by the matching parser,
/// the result round-trips through
/// [`crate::parse_mrv_a`] with `valid() == true` (see `tests/roundtrip.rs`).
/// MRV-A has no personal-number check digit and no composite check digit.
///
/// ```
/// use mrz::{format_mrv_a, parse_mrv_a, MrvAFields};
///
/// let fields = MrvAFields {
/// document_number: "L898902C".into(),
/// surname: "ERIKSSON".into(),
/// given_names: "ANNA MARIA".into(),
/// nationality: "UTO".into(),
/// date_of_birth: mrz::MrzDate::Calendar(mrz::Date::new(1969, 8, 6)),
/// sex: mrz::Sex::Female,
/// date_of_expiry: mrz::MrzDate::Calendar(mrz::Date::new(2094, 6, 23)), // ICAO meant 1994; century does not change `940623`.
/// ..MrvAFields::default()
/// };
/// let mrz = format_mrv_a(&fields);
/// let (l1, l2) = mrz.split_once('\n').unwrap();
/// assert!(parse_mrv_a(l1, l2).unwrap().valid());
/// ```
/// Raw MRV-B sub-fields, in MRZ-native form — the same input conventions as
/// [`Td3Fields`].
///
/// Visas define no overflow encoding (Doc 9303 Part 7), so a `document_number`
/// longer than 9 characters is truncated to 9. MRV-B has no personal-number or
/// composite check digit — `optional_data` is free-form data up to 8
/// characters.
///
/// ```
/// use mrz::{format_mrv_b, MrvBFields};
///
/// let fields = MrvBFields {
/// issuing_country: "UTO".into(),
/// document_number: "L898902C".into(),
/// surname: "ERIKSSON".into(),
/// given_names: "ANNA MARIA".into(),
/// nationality: "UTO".into(),
/// date_of_birth: mrz::MrzDate::Calendar(mrz::Date::new(1969, 8, 6)),
/// sex: mrz::Sex::Female,
/// date_of_expiry: mrz::MrzDate::Calendar(mrz::Date::new(2094, 6, 23)), // ICAO meant 1994; century does not change `940623`.
/// optional_data: Some("ZE184226".into()),
/// ..Default::default()
/// };
/// assert_eq!(fields.document_code, "V"); // the default document code
///
/// // ICAO 9303 Part 7's published MRV-B specimen, byte for byte.
/// assert_eq!(
/// format_mrv_b(&fields),
/// "V<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<\n\
/// L898902C<3UTO6908061F9406236ZE184226",
/// );
/// ```
/// **Deliberately not `#[non_exhaustive]`.** This struct mirrors a field
/// layout ICAO 9303 fixes, so it does not grow the way [`crate::Checks`] or
/// [`crate::MrzData`] do, and callers build it with a struct expression as
/// the examples above show. **Put any future tunable in a separate
/// `#[non_exhaustive]` companion rather than adding a field here** -- the
/// same separation [`crate::ParseOptions`] already has from the data it
/// parses. Adding a field here is a breaking change and should stay one.
/// Emit an MRV-B (machine readable visa) MRZ: two 36-character lines joined
/// by `\n`.
///
/// The document number, date-of-birth, and date-of-expiry check digits are
/// computed from `fields`. With a document code accepted by the matching parser,
/// the result round-trips through
/// [`crate::parse_mrv_b`] with `valid() == true` (see `tests/roundtrip.rs`).
/// MRV-B has no personal-number check digit and no composite check digit.
///
/// ```
/// use mrz::{format_mrv_b, parse_mrv_b, MrvBFields};
///
/// let fields = MrvBFields {
/// document_number: "L898902C".into(),
/// surname: "ERIKSSON".into(),
/// given_names: "ANNA MARIA".into(),
/// nationality: "UTO".into(),
/// date_of_birth: mrz::MrzDate::Calendar(mrz::Date::new(1969, 8, 6)),
/// sex: mrz::Sex::Female,
/// date_of_expiry: mrz::MrzDate::Calendar(mrz::Date::new(2094, 6, 23)), // ICAO meant 1994; century does not change `940623`.
/// ..MrvBFields::default()
/// };
/// let mrz = format_mrv_b(&fields);
/// let (l1, l2) = mrz.split_once('\n').unwrap();
/// assert!(parse_mrv_b(l1, l2).unwrap().valid());
/// ```