mf2-build 2.0.0

The MessageFormat 2 build pipeline: loaders, mf2.toml, the manifest, lints, per-locale catalogs and the generated Rust module.
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
//! The generated Rust module (`plans/05-tooling.md` §4,
//! `plans/19-native-and-terminal.md` §10).
//!
//! `build.rs` writes it to `OUT_DIR`; the i18n crate includes it at its root
//! with `mf2::include_generated!()`. Its text is the same in every build of
//! one corpus and emit mode: each compile-time choice goes through one of
//! `mf2`'s cfg-forwarding macros (`__mf2::__if_ssr! { … }`, A2), which keep
//! or drop what they are given by `mf2`'s own features, whichever crate
//! turned them on. It carries
//!
//! * `MANIFEST_HASH` and `SOURCE_LOCALE` — what the client (or the
//!   executable) and every catalog agree on (F6);
//! * `LOCALES` — the tags and their base direction;
//! * `LANGUAGE_MATCHING` — CLDR's language-matching data cut to those
//!   locales' languages (`mf2_locale_data::matching`): what the one matcher
//!   reads, and all of CLDR's table a browser's client carries (19 §9);
//! * `CATALOGS` — the catalogs themselves, **never in a browser's client**
//!   (`__if_host_std!`): keeping the names and hashes out of the client is
//!   what makes its wasm byte-identical across translation edits (P0.9). A
//!   module that embeds catalogs embeds each once, in one table that
//!   `CATALOGS` and `CORPUS` share (a server beside `native`);
//! * `registry()` — the closed world (B13): the handlers this corpus uses
//!   and no others, with `with_numbers` / `with_dates` only where a
//!   placeholder can actually receive one (#90);
//! * `host` — the host the corpus needs, so that a feature that is on but
//!   unused links none of its glue (B1′);
//! * `CORPUS` — for `mf2::native`: under [`Emit::Native`] /
//!   [`Emit::NativeFiles`] (each catalog's file name and, under `Native`,
//!   its bytes; nothing behind a mode), and under [`Emit::Both`] with
//!   `native`;
//! * `Locale` — one variant per locale, with `ALL`, `SOURCE`, `tag()`,
//!   `dir()`, `best_match()`, `FromStr` through the one matcher and
//!   `Display`; `format()` with `native`; `name()` when every locale has a
//!   `language.<tag>` message; a clap value parser with `clap`;
//! * `setup()`, `install()`, `install_from_directory()`, `set_locale()`,
//!   `preload_locale()`, `current_locale()` and `with_locale()`, each where
//!   the build has what it needs (19 §10's table), one item per
//!   combination of modes;
//! * `markup::*` with `ratatui`, and a `prelude`;
//! * `pub use ::mf2 as __mf2;` and the exported `tr!` wrapper, which bakes
//!   the manifest's absolute path and hash into every expansion — or, with
//!   [`crate::Build::manifest_inline`], the manifest's bytes, so that an
//!   expansion survives a target directory that moved (D8).
//!
//! A native module ([`Emit::Native`], [`Emit::NativeFiles`]) never mentions
//! the browser's build in its documentation. The file is regenerated
//! whenever the corpus or the feature set changes, and written only when
//! its bytes differ.

use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::path::Path;

use mf2_catalog::Dir;

use crate::build::{Emit, LocaleInfo};
use crate::error::{Error, Result};
use crate::features::Features;

/// What the module is generated from.
#[derive(Debug)]
pub struct Module<'a> {
    /// The crate path re-exported as `__mf2` (`::mf2`).
    pub facade: &'a str,
    /// The absolute path of `manifest.mf2m`, baked into `tr!`.
    pub manifest_path: &'a Path,
    /// `manifest_hash`.
    pub manifest_hash: u64,
    /// The source locale.
    pub source_locale: &'a str,
    /// Every locale, in tag order.
    pub locales: &'a [LocaleInfo],
    /// CLDR's language-matching data cut to the locales' languages, as a
    /// Rust expression of `mf2::LanguageMatching`
    /// (`mf2_locale_data::matching::Encoded::rust`).
    pub language_matching: &'a str,
    /// The function identifiers the corpus uses, ascending.
    pub functions: &'a [String],
    /// `mf2.toml`'s `[functions]`: identifier → Rust path.
    pub custom: &'a BTreeMap<String, String>,
    /// The client feature set.
    pub features: &'a Features,
    /// The corpus has a placeholder with no function, which can receive a
    /// number or a date at run time.
    pub unannotated: bool,
    /// How many messages, for the header comment.
    pub messages: usize,
    /// What this build writes: with [`Emit::Module`] the catalogs are
    /// another crate's, and nothing here names one.
    pub emit: Emit,
    /// The manifest's bytes, when the wrapper bakes them in instead of the
    /// path ([`crate::Build::manifest_inline`]): every expansion is then
    /// independent of where the target directory lives.
    pub manifest_bytes: Option<&'a [u8]>,
    /// The markup names the corpus uses, ascending: `markup::*`.
    pub markup: &'a [String],
    /// For each of `locales`, whether it has a `language.<tag>` message with
    /// no argument: the switcher's names, and `Locale::name()` when every
    /// locale has one.
    pub names: &'a [bool],
}

/// Refuses what would make the generated module ambiguous: two tags that
/// give `Locale` one variant, and two markup names with one hash (which a
/// style could not tell apart, as `tr!` refuses within one message).
pub(crate) fn check(m: &Module<'_>) -> Result<()> {
    let mut variants: BTreeMap<String, &str> = BTreeMap::new();
    for locale in m.locales {
        let name = variant(&locale.tag);
        if let Some(first) = variants.insert(name.clone(), &locale.tag) {
            return Err(Error::LocaleVariant {
                first: first.to_owned(),
                second: locale.tag.clone(),
                variant: name,
            });
        }
    }
    let mut keys: BTreeMap<u64, &str> = BTreeMap::new();
    for name in m.markup {
        if let Some(first) = keys.insert(mf2_catalog::markup_key(name), name) {
            return Err(Error::MarkupHash {
                first: first.to_owned(),
                second: name.clone(),
            });
        }
    }
    Ok(())
}

/// A tag's variant of `Locale`: its subtags in upper camel case (`pt-BR` is
/// `PtBr`, `es-419` is `Es419`).
pub(crate) fn variant(tag: &str) -> String {
    let mut out = String::with_capacity(tag.len());
    for part in tag.split(['-', '_']) {
        let mut chars = part.chars();
        if let Some(first) = chars.next() {
            out.extend(first.to_uppercase());
            out.extend(chars.flat_map(char::to_lowercase));
        }
    }
    out
}

/// A markup name's constant in `markup`: its ASCII letters and digits in
/// upper case, and `_` for every other character (`key-name` and
/// `ns:key.name` give `KEY_NAME` and `NS_KEY_NAME`).
pub(crate) fn constant(name: &str) -> String {
    name.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() {
                c.to_ascii_uppercase()
            } else {
                '_'
            }
        })
        .collect()
}

/// The markup names that get a constant, with it. Every markup name MF2
/// allows is valid in a corpus, so none is refused: a name whose constant
/// has no letter or digit (`+:_¡`), or shares its constant with another
/// name (`a-b`, `a.b`), gets none.
fn constants(names: &[String]) -> Vec<(&str, String)> {
    let all: Vec<(&str, String)> = names.iter().map(|n| (n.as_str(), constant(n))).collect();
    let mut count: BTreeMap<&str, usize> = BTreeMap::new();
    for (_, c) in &all {
        *count.entry(c.as_str()).or_default() += 1;
    }
    let keep: Vec<bool> = all
        .iter()
        .map(|(_, c)| {
            count.get(c.as_str()) == Some(&1)
                && c.bytes().any(|b| b.is_ascii_alphanumeric())
                && !c.starts_with(|d: char| d.is_ascii_digit())
        })
        .collect();
    all.into_iter()
        .zip(keep)
        .filter_map(|(pair, keep)| keep.then_some(pair))
        .collect()
}

/// The module's source.
pub fn write(module: &Module<'_>) -> String {
    let mut s = String::with_capacity(8192);
    header(&mut s, module);
    identity(&mut s, module);
    locales(&mut s, module);
    if embeds(module.emit) {
        embedded(&mut s, module);
    }
    registry(&mut s, module);
    if is_native(module.emit) {
        native_host(&mut s);
    } else {
        host(&mut s, module);
    }
    if has_corpus(module.emit) {
        corpus(&mut s, module);
    }
    locale(&mut s, module);
    functions(&mut s, module);
    markup(&mut s, module);
    tr(&mut s, module);
    if module.names.contains(&true) {
        names(&mut s, module);
    }
    prelude(&mut s, module);
    s
}

/// Whether `emit` builds for a native application.
pub(crate) const fn is_native(emit: Emit) -> bool {
    matches!(emit, Emit::Native | Emit::NativeFiles)
}

/// Whether the module embeds the catalogs' bytes.
const fn embeds(emit: Emit) -> bool {
    matches!(emit, Emit::Both | Emit::Native)
}

/// Whether the module has a `CORPUS` (under [`Emit::Both`], with `native`).
const fn has_corpus(emit: Emit) -> bool {
    matches!(emit, Emit::Both | Emit::Native | Emit::NativeFiles)
}

/// The catalog table on its own, for a crate only the server binary depends
/// on ([`Emit::Catalogs`]).
///
/// The whole file is server-side by construction, so nothing in it is behind
/// a mode; the crate that includes it is the gate.
pub fn write_catalogs(module: &Module<'_>) -> String {
    let mut s = String::with_capacity(512);
    header(&mut s, module);
    let _ = write!(
        s,
        "
/// The facade the embedded catalogs are read through.
#[doc(hidden)]
pub use {facade} as __mf2;
",
        facade = module.facade
    );
    s.push('\n');
    s.push_str(&catalogs(module, None));
    s
}

fn header(s: &mut String, m: &Module<'_>) {
    let _ = writeln!(
        s,
        "// @generated by mf2-build from locales/ — do not edit.\n\
         // {} messages, {} locale{}, {} function{}; features: {}.",
        m.messages,
        m.locales.len(),
        plural(m.locales.len()),
        m.functions.len(),
        plural(m.functions.len()),
        if m.features.names().next().is_none() {
            "(none)".to_owned()
        } else {
            m.features.names().collect::<Vec<_>>().join(", ")
        }
    );
}

fn plural(n: usize) -> &'static str {
    if n == 1 { "" } else { "s" }
}

/// `body`, each line indented, inside `__mf2::{name}! { … }`: kept or
/// dropped by `mf2`'s features.
fn gate(s: &mut String, name: &str, body: &str) {
    let _ = writeln!(s, "__mf2::{name}! {{");
    indent(s, body, "    ");
    s.push_str("}\n");
}

fn indent(s: &mut String, body: &str, by: &str) {
    for line in body.lines() {
        if !line.is_empty() {
            s.push_str(by);
            s.push_str(line);
        }
        s.push('\n');
    }
}

/// `0x43e0_dc12_eeb0_5ef1`: grouped, so that the generated file reads the way
/// a hand-written one would and clippy's pedantic lints stay quiet in the
/// crate that includes it.
fn hex_u64(value: u64) -> String {
    let hex = format!("{value:016x}");
    let mut out = String::from("0x");
    for (i, chunk) in hex.as_bytes().chunks(4).enumerate() {
        if i > 0 {
            out.push('_');
        }
        out.push_str(core::str::from_utf8(chunk).unwrap_or("0000"));
    }
    out
}

fn identity(s: &mut String, m: &Module<'_>) {
    let agree = if is_native(m.emit) {
        "the ids, slots, markup\n/// names and functions the executable and every catalog agree on. A catalog\n/// whose header carries another one is refused."
    } else {
        "the ids, slots, markup\n/// names and functions the wasm and every catalog agree on. A catalog whose\n/// header carries another one is rejected and refetched (F6)."
    };
    let _ = write!(
        s,
        "
/// The facade every `tr!` expansion goes through.
#[doc(hidden)]
pub use {facade} as __mf2;

/// `manifest_hash` (`plans/02-catalog-format.md` §3): {agree}
pub const MANIFEST_HASH: u64 = {hash};

/// The locale the manifest was built from.
pub const SOURCE_LOCALE: &str = {source:?};
",
        facade = m.facade,
        hash = hex_u64(m.manifest_hash),
        source = m.source_locale
    );
}

fn locales(s: &mut String, m: &Module<'_>) {
    let native = is_native(m.emit);
    let _ = write!(
        s,
        "
/// Every locale this corpus was built for, with its base direction, in tag
/// order. {what}
pub static LOCALES: &[(&str, __mf2::Dir)] = &[
",
        what = if native {
            "`Locale::ALL` is in the same order."
        } else {
            "No catalog name and no hash: the client is told which\n/// URL to fetch."
        }
    );
    for locale in m.locales {
        let _ = writeln!(
            s,
            "    ({:?}, __mf2::Dir::{}),",
            locale.tag,
            match locale.dir {
                Dir::Rtl => "Rtl",
                _ => "Ltr",
            }
        );
    }
    let who = if native {
        "It is what `CORPUS` matches\n/// the system's preferred languages with, and `Locale` a tag."
    } else {
        "It is all a browser's client carries of that\n/// table: a client-only application's boot matches the reader's languages\n/// with it (`install()` passes it), and a hydrated page never matches."
    };
    let _ = write!(
        s,
        "];

/// Whether `tag` is one of them.
pub fn has_locale(tag: &str) -> bool {{
    LOCALES.iter().any(|(t, _)| *t == tag)
}}

/// CLDR's language-matching data, cut to these locales' languages: the rules
/// that can serve one of them, and the likely subtags of the languages whose
/// readers those rules accept. For every reader it gives the answers CLDR's
/// whole table gives. {who}
pub static LANGUAGE_MATCHING: __mf2::LanguageMatching = {matching};
",
        matching = m.language_matching
    );
}

/// The catalogs' bytes, embedded once, and the tables that share them:
/// `CATALOGS` for a server (never a browser's client) and, with `native`,
/// `CORPUS` (written by [`corpus`]).
fn embedded(s: &mut String, m: &Module<'_>) {
    let native = is_native(m.emit);
    let mut table = String::new();
    let _ = writeln!(
        table,
        "/// The catalogs' bytes, embedded once: what {} share.\nstatic MF2_CATALOG_BYTES: [&[u8]; {}] = [",
        if native {
            "`CORPUS` and, beside `ssr`,\n/// `CATALOGS`"
        } else {
            "`CATALOGS` and, with `native`,\n/// `CORPUS`"
        },
        m.locales.len()
    );
    for locale in m.locales {
        let _ = writeln!(
            table,
            "    include_bytes!(concat!(env!(\"OUT_DIR\"), \"/\", {:?})),",
            locale.file_name
        );
    }
    table.push_str("];\n");
    let catalogs = catalogs(m, Some("MF2_CATALOG_BYTES"));
    s.push('\n');
    if native {
        s.push_str(&table);
        s.push('\n');
        gate(s, "__if_ssr", &catalogs);
    } else {
        table.push('\n');
        table.push_str(&catalogs);
        gate(s, "__if_host_std", &table);
    }
}

/// `CATALOGS`, `catalog()` and `catalog_name()`: each catalog's bytes from
/// `table`, or included here ([`Emit::Catalogs`]).
fn catalogs(m: &Module<'_>, table: Option<&str>) -> String {
    let mut s = String::new();
    let _ = writeln!(
        s,
        "/// {}\npub static CATALOGS: &[(&str, &str, &[u8])] = &[",
        if is_native(m.emit) {
            "The catalogs, for the Leptos layer's server beside `native`: `(tag,\n/// file name, bytes)`, the bytes `CORPUS` holds."
        } else {
            "The catalogs, embedded for the **server** only: `(tag, file name,\n/// bytes)`. The client fetches its one locale instead, so its wasm holds no\n/// message text, no id and no catalog name (B6)."
        }
    );
    for (i, locale) in m.locales.iter().enumerate() {
        let bytes = match table {
            Some(table) => format!("{table}[{i}]"),
            None => format!(
                "include_bytes!(concat!(env!(\"OUT_DIR\"), \"/\", {:?}))",
                locale.file_name
            ),
        };
        let _ = writeln!(
            s,
            "    ({tag:?}, {file:?}, {bytes}),",
            tag = locale.tag,
            file = locale.file_name
        );
    }
    s.push_str(
        "];

/// The embedded catalog of `tag`, for a server that serves it from memory.
pub fn catalog(tag: &str) -> Option<&'static [u8]> {
    CATALOGS.iter().find(|(t, _, _)| *t == tag).map(|(_, _, b)| *b)
}

/// The file name `tag`'s catalog is published under, content-hashed and
/// served immutable.
pub fn catalog_name(tag: &str) -> Option<&'static str> {
    CATALOGS.iter().find(|(t, _, _)| *t == tag).map(|(_, n, _)| *n)
}
",
    );
    s
}

/// The Rust path of the handler for a built-in function under `features`.
fn builtin_path(name: &str, features: &Features) -> Option<&'static str> {
    Some(match (name, features.fn_number()) {
        ("string", _) => "__mf2::functions::STRING",
        ("number", false) => "__mf2::functions::NUMBER",
        ("integer", false) => "__mf2::functions::INTEGER",
        ("offset", false) => "__mf2::functions::OFFSET",
        ("number", true) => "__mf2::fn_number::NUMBER",
        ("integer", true) => "__mf2::fn_number::INTEGER",
        ("offset", true) => "__mf2::fn_number::OFFSET",
        ("percent", _) => "__mf2::fn_number::PERCENT",
        ("currency", _) => "__mf2::fn_number::CURRENCY",
        ("unit", _) => "__mf2::fn_number::UNIT",
        ("datetime", _) => "__mf2::fn_datetime::DATETIME",
        ("date", _) => "__mf2::fn_datetime::DATE",
        ("time", _) => "__mf2::fn_datetime::TIME",
        _ => return None,
    })
}

fn registry(s: &mut String, m: &Module<'_>) {
    let mut entries: Vec<(String, String)> = Vec::new();
    for name in m.functions {
        let path = match builtin_path(name, m.features) {
            Some(path) => path.to_owned(),
            // A custom function. `unknown-function` is an error by default,
            // so reaching here means the corpus turned that lint down: the
            // call stays in the catalog and the runtime reports Unknown
            // Function against it, which is the fallback the spec defines.
            None => match m.custom.get(name) {
                Some(path) => path.clone(),
                None => continue,
            },
        };
        entries.push((name.clone(), path));
    }
    let _ = write!(
        s,
        "
/// The handlers this corpus uses — and no others, so an unused function is
/// not linked (B13).
static FUNCTIONS: [(&str, &dyn __mf2::Function); {n}] = [
",
        n = entries.len()
    );
    for (name, path) in &entries {
        let _ = writeln!(s, "    ({name:?}, &{path}),");
    }
    let _ = write!(
        s,
        "];\n\nstatic REGISTRY: __mf2::Registry = __mf2::Registry::new(&FUNCTIONS)"
    );
    // The unannotated hooks: only with their feature, and only if some
    // placeholder can reach them (#90).
    if m.unannotated && m.features.fn_number() {
        let _ = write!(s, "\n    .with_numbers(&__mf2::fn_number::NUMBERS)");
    }
    if m.unannotated && m.features.fn_datetime() {
        let _ = write!(s, "\n    .with_dates(&__mf2::fn_datetime::DATES)");
    }
    let _ = write!(
        s,
        ";

/// The registry every formatter in this application uses.
pub fn registry() -> &'static __mf2::Registry {{
    &REGISTRY
}}
"
    );
}

fn host(s: &mut String, m: &Module<'_>) {
    // Which host depends on how `mf2` was built, which is not the build's
    // to know: `__use_host!` is defined once per combination of `mf2`'s
    // features, and names one static, so the others are never linked
    // (B1′). A corpus with no date names no date host at all.
    let dates = if m.features.fn_datetime() {
        "dates"
    } else {
        ""
    };
    let _ = write!(
        s,
        "
/// The host this build formats through: the server's, or the browser's — and
/// of the browser's, the one this corpus actually needs, so that the glue of
/// a feature that is on but unused is never linked (B1′).
pub mod host {{
    super::__mf2::__use_host!({dates});
}}
"
    );
}

fn native_host(s: &mut String) {
    let _ = write!(
        s,
        "
/// The host this build formats through: the native one.
pub mod host {{
    pub use super::__mf2::host_std::HOST;
}}
"
    );
}

fn corpus(s: &mut String, m: &Module<'_>) {
    let mut c = String::new();
    let _ = writeln!(
        c,
        "/// Everything `mf2::native` needs, as one value: the source locale, the
/// manifest hash, the locales, the registry, each catalog's file name{embedded},
/// and the part of CLDR's language-matching data they need.
pub static CORPUS: __mf2::Corpus = __mf2::Corpus::new(
    SOURCE_LOCALE,
    MANIFEST_HASH,
    LOCALES,
    &REGISTRY,
    &[",
        embedded = if embeds(m.emit) { " and bytes" } else { "" }
    );
    for (i, locale) in m.locales.iter().enumerate() {
        let bytes = if embeds(m.emit) {
            format!("Some(MF2_CATALOG_BYTES[{i}])")
        } else {
            "None".to_owned()
        };
        let _ = writeln!(
            c,
            "        __mf2::CatalogFile::new({tag:?}, {file:?}, {bytes}),",
            tag = locale.tag,
            file = locale.file_name
        );
    }
    c.push_str("    ],\n)\n.with_language_matching(&LANGUAGE_MATCHING);\n");
    s.push('\n');
    if is_native(m.emit) {
        s.push_str(&c);
    } else {
        // What `Locale::format` reads: with `native`, and on a server.
        gate(s, "__if_format", &c);
    }
}

fn locale(s: &mut String, m: &Module<'_>) {
    let variants: Vec<(String, &LocaleInfo)> =
        m.locales.iter().map(|l| (variant(&l.tag), l)).collect();
    let source = variant(m.source_locale);
    let hydrated = if is_native(m.emit) {
        ""
    } else {
        " In a hydrated page, which never matches (the\n///   server chose), `from_str` takes an exact tag."
    };
    let _ = write!(
        s,
        "
/// The languages this application is translated into: one variant per
/// locale, in tag order (`pt-BR` is `PtBr`).
///
/// * `ALL` lists them, and `SOURCE` is the one the messages are written in;
/// * `tag()` and `dir()` give the tag and its base direction, and `Display`
///   writes the tag;
/// * `from_str` (`\"fr_CA.UTF-8\".parse()`) and `best_match` choose the
///   language that best serves a reader, by CLDR's language-matching data;
///   `from_str`'s error lists the languages there are.{hydrated}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum Locale {{
"
    );
    for (name, l) in &variants {
        let _ = writeln!(s, "    /// `{}`\n    {name},", l.tag);
    }
    let _ = write!(
        s,
        "}}

impl Locale {{
    /// Every language, in tag order, as `LOCALES` lists them.
    pub const ALL: [Locale; {n}] = [{all}];

    /// The language the messages are written in.
    pub const SOURCE: Locale = Locale::{source};

    /// The BCP 47 tag.
    #[must_use]
    pub const fn tag(self) -> &'static str {{
        match self {{
",
        n = variants.len(),
        all = variants
            .iter()
            .map(|(name, _)| format!("Locale::{name}"))
            .collect::<Vec<_>>()
            .join(", "),
    );
    for (name, l) in &variants {
        let _ = writeln!(s, "            Locale::{name} => {:?},", l.tag);
    }
    let rtl: Vec<&str> = variants
        .iter()
        .filter(|(_, l)| l.dir == Dir::Rtl)
        .map(|(name, _)| name.as_str())
        .collect();
    let dir = if rtl.is_empty() {
        "        let _ = self;\n        __mf2::Dir::Ltr\n".to_owned()
    } else if rtl.len() == variants.len() {
        "        let _ = self;\n        __mf2::Dir::Rtl\n".to_owned()
    } else {
        format!(
            "        match self {{\n            {} => __mf2::Dir::Rtl,\n            _ => __mf2::Dir::Ltr,\n        }}\n",
            rtl.iter()
                .map(|name| format!("Locale::{name}"))
                .collect::<Vec<_>>()
                .join(" | ")
        )
    };
    let _ = write!(
        s,
        "        }}
    }}

    /// The base direction of the language's text.
    #[must_use]
    pub const fn dir(self) -> __mf2::Dir {{
{dir}    }}

    /// The language that best serves a reader of `desired`, their languages
    /// in order of preference, by CLDR's language-matching data; `None` when
    /// none is close enough (the source language is then the usual answer).
    pub fn best_match<'a>(desired: impl IntoIterator<Item = &'a str>) -> Option<Locale> {{
        LANGUAGE_MATCHING
            .best_match(desired, LOCALES)
            .and_then(|index| Locale::ALL.get(index).copied())
    }}
}}

/// The language that best serves `tag` (`fr_CA.UTF-8` is French); an
/// error, which lists the languages there are, when none is close enough.
impl ::core::str::FromStr for Locale {{
    type Err = __mf2::UnknownLocale;

    fn from_str(tag: &str) -> Result<Locale, __mf2::UnknownLocale> {{
        __mf2::__best_locale!(LANGUAGE_MATCHING, LOCALES, tag)
            .and_then(|index| Locale::ALL.get(index).copied())
            .ok_or(__mf2::UnknownLocale::new(LOCALES))
    }}
}}

/// The tag.
impl ::core::fmt::Display for Locale {{
    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {{
        f.write_str(self.tag())
    }}
}}

__mf2::__if_clap! {{
    /// `--lang fr_CA.UTF-8` parses through `from_str`, and `--help` lists the
    /// tags.
    impl __mf2::__generated::clap::ValueParserFactory for Locale {{
        type Parser = __mf2::__generated::clap::LocaleParser<Locale>;

        fn value_parser() -> Self::Parser {{
            __mf2::__generated::clap::LocaleParser::new(
                LOCALES,
                <Locale as ::core::str::FromStr>::from_str,
            )
        }}
    }}
}}
"
    );
    if has_corpus(m.emit) {
        s.push('\n');
        gate(
            s,
            if is_native(m.emit) {
                "__if_native"
            } else {
                "__if_format"
            },
            "impl Locale {
    /// `message`, formatted in this language. It needs no `install()`, and
    /// chooses no app-wide language.
    #[must_use]
    pub fn format(self, message: &impl __mf2::Message) -> __mf2::__generated::String {
        __mf2::__generated::format_in(&CORPUS, self.tag(), message)
    }
}
",
        );
    }
    s.push('\n');
    gate(
        s,
        "__if_axum",
        "/// An Axum extractor: the language the `Negotiator` layer chose for this
/// request, else the one `mf2::axum::Negotiator::default()`'s sources
/// negotiate. It never rejects a request.
impl<S: ::core::marker::Send + ::core::marker::Sync> __mf2::__generated::axum::FromRequestParts<S>
    for Locale
{
    type Rejection = ::core::convert::Infallible;

    fn from_request_parts(
        parts: &mut __mf2::__generated::axum::Parts,
        _state: &S,
    ) -> impl ::core::future::Future<Output = ::core::result::Result<Self, Self::Rejection>>
           + ::core::marker::Send {
        let locale = __mf2::__generated::axum::locale_index(parts, LOCALES, SOURCE_LOCALE)
            .and_then(|index| Locale::ALL.get(index).copied())
            .unwrap_or(Locale::SOURCE);
        ::core::future::ready(::core::result::Result::Ok(locale))
    }
}
",
    );
}

/// The locale functions, each once, whatever the combination of modes:
/// each body names every mode's work through the cfg-forwarding macros.
fn functions(s: &mut String, m: &Module<'_>) {
    let native = is_native(m.emit);
    // What the Leptos layer is given: a client-only application's boot
    // matches the reader's languages, so its setup alone carries the
    // matching data (a hydrated page never matches, and links none).
    s.push('\n');
    gate(
        s,
        "__if_leptos",
        &"/// What the Leptos layer is given, from what this build generated: the
/// registry, the host, the manifest hash and the locales, and a client-only
/// application's language-matching data. `install()` installs it; an
/// application that adds to it (`setup().with_time_zone(…)`) installs it
/// itself, once on each side.
#[must_use]
#[allow(clippy::let_and_return)]
pub fn setup() -> __mf2::leptos::Setup {
    let setup = __mf2::leptos::Setup::new(
        registry(),
        &host::HOST,
        MANIFEST_HASH,
        SOURCE_LOCALE,
        LOCALES,
    );
    __mf2::__if_csr! {
        let setup = setup.with_language_matching(&LANGUAGE_MATCHING);
    }
    __NAMES__setup
}
"
        .replace(
            "__NAMES__",
            if m.names.contains(&true) {
                "let setup = setup.with_names(__locale_name);\n    "
            } else {
                ""
            },
        ),
    );

    // `install()`.
    let mut body = String::new();
    if embeds(m.emit) {
        body.push_str(
            "    __mf2::__if_native! {\n        __mf2::native::install(&CORPUS);\n    }\n",
        );
        body.push_str(
            "    __mf2::__if_ssr! {\n        __mf2::__generated::install_server(setup(), CATALOGS);\n    }\n",
        );
        body.push_str(
            "    __mf2::__if_axum! {\n        __mf2::__generated::install_axum(&CORPUS);\n    }\n",
        );
    } else {
        body.push_str("    __mf2::__if_ssr! {\n        __mf2::leptos::install(setup());\n    }\n");
    }
    body.push_str("    __mf2::__if_client! {\n        __mf2::leptos::install(setup());\n    }\n");
    let doc = match m.emit {
        Emit::Native => {
            "/// Installs the catalogs the executable embeds as the process's, and makes
/// the language that best serves the system's preferred languages the
/// app-wide one, else the source language. Call it once, at start-up. It
/// returns nothing: embedded catalogs from the same build cannot fail to
/// load. Beside a Leptos mode it also gives the Leptos layer this build's
/// setup, and on the server the same catalogs."
        }
        Emit::NativeFiles => {
            "/// Gives the Leptos layer this build's setup (its catalogs are files:
/// `install_from_directory` installs them)."
        }
        Emit::Module => {
            "/// Gives the Leptos layer what this build generated, `setup()`. Call it
/// once on each side, before rendering or hydrating. This module names no
/// catalog: the server installs them from the crate that embeds them."
        }
        _ => {
            "/// Gives the Leptos layer what this build generated, `setup()`, and, on
/// the server, the embedded catalogs, each checked against the manifest
/// hash. Call it once on each side, before rendering or hydrating. With
/// `native`, it also installs the catalogs as the process's (`mf2::native`);
/// with `axum`, as the ones `mf2::axum` negotiates among and serves.
///
/// # Panics
///
/// On the server, if an embedded catalog does not load: a corrupt
/// executable."
        }
    };
    let install = format!("{doc}\npub fn install() {{\n{body}}}\n");
    s.push('\n');
    gate(
        s,
        if embeds(m.emit) {
            "__if_install"
        } else {
            "__if_leptos"
        },
        &install,
    );

    if has_corpus(m.emit) {
        s.push('\n');
        gate(
            s,
            "__if_native",
            "/// Installs the catalog files this build wrote from `directory` as the
/// process's, and chooses the app-wide language as `install()` does. Only
/// the source language's file is required; each must hash to its name.
pub fn install_from_directory(
    directory: impl AsRef<__mf2::__generated::Path>,
) -> Result<(), __mf2::native::Error> {
    __mf2::native::install_from_directory(&CORPUS, directory)
}
",
        );
    }

    // The locale functions exist with a Leptos mode, and with `native`
    // wherever there is a `CORPUS` to install.
    let mode = if has_corpus(m.emit) {
        "__if_mode"
    } else {
        "__if_leptos"
    };
    let set = if native {
        "/// Makes `locale` the app-wide language: every thread's next format uses
/// it, but a thread inside `with_locale`. Beside a Leptos mode, a client
/// switches its page (spawned), and the server does nothing.
///
/// # Panics
///
/// Before `install()`."
    } else {
        "/// Switches to `locale`: in the browser, fetch, check and swap the catalog,
/// update every live text and remember the choice (spawned; a failure
/// leaves the page as it is). On the server it does nothing: the request's
/// language is the negotiation's. With `native`, it sets the app-wide
/// language too."
    };
    let current = if native {
        "/// The language this thread formats in: its own (`with_locale`), else the
/// app-wide one. Beside a Leptos mode, the request's or the page's first.
///
/// # Panics
///
/// In a build whose only mode is `native`, before `install()` outside
/// `with_locale`."
    } else {
        "/// The language of the request being rendered, or of the page — tracked:
/// a view that reads it follows a switch. With `native`, then the native
/// store's; the source language before any is chosen."
    };
    let _ = write!(
        s,
        "
__mf2::{mode}! {{
{set_doc}
    pub fn set_locale(locale: Locale) {{
        __mf2::__generated::set_locale(locale.tag());
    }}

{current_doc}
    #[must_use]
    pub fn current_locale() -> Locale {{
        __mf2::__generated::current_locale(LOCALES)
            .and_then(|index| Locale::ALL.get(index).copied())
            .unwrap_or(Locale::SOURCE)
    }}
}}
",
        set_doc = indented(set),
        current_doc = indented(current),
    );

    s.push('\n');
    gate(
        s,
        "__if_leptos",
        "/// Fetches and checks `locale`'s catalog without switching to it — what a
/// language menu calls on hover (spawned). On the server it does nothing.
pub fn preload_locale(locale: Locale) {
    __mf2::__generated::preload_locale(locale.tag());
}

/// `<LocaleOption tag=Locale::Fr>`: the helper's tag type.
impl From<Locale> for __mf2::leptos::LocaleTag {
    fn from(locale: Locale) -> __mf2::leptos::LocaleTag {
        __mf2::leptos::LocaleTag(locale.tag())
    }
}
",
    );

    if has_corpus(m.emit) {
        s.push('\n');
        gate(
            s,
            "__if_native",
            "/// Runs `body` with this thread formatting in `locale`, and returns what it
/// returns; the thread's language is restored when `body` returns or
/// unwinds, and other threads are not affected. It needs no `install()`.
pub fn with_locale<R>(locale: Locale, body: impl FnOnce() -> R) -> R {
    __mf2::__generated::with_locale_in(&CORPUS, locale.tag(), body)
}
",
        );
    }
}

/// `text`, each line indented by four spaces.
fn indented(text: &str) -> String {
    let mut out = String::new();
    indent(&mut out, text, "    ");
    out.pop();
    out
}

/// `markup::*`, with `ratatui`: a constant per markup name, holding the
/// name and its hash.
fn markup(s: &mut String, m: &Module<'_>) {
    let mut body = String::from(
        "/// The markup names the messages use, one constant each, for a theme's
/// styles: `markup::KEY` is `{#key}…{/key}`. A name whose ASCII letters and
/// digits give no constant, or the same one as another name's, has none.
pub mod markup {
",
    );
    for (name, constant) in constants(m.markup) {
        let _ = writeln!(
            body,
            "    /// `{{#{name}}}`\n    pub const {constant}: super::__mf2::ratatui::Markup =\n        super::__mf2::ratatui::Markup::new({key}, {name:?});",
            key = hex_u64(mf2_catalog::markup_key(name)),
        );
    }
    body.push_str("}\n");
    s.push('\n');
    gate(s, "__if_ratatui", &body);
}

fn tr(s: &mut String, m: &Module<'_>) {
    // Either the manifest's absolute path, or — opt-in — its bytes, which
    // survive a target directory that moves (`Build::manifest_inline`).
    let (source, how) = match m.manifest_bytes {
        None => (
            format!("{:?}", m.manifest_path.display().to_string()),
            "The manifest's absolute path and its hash are baked in, so any crate that\n\
             /// depends on this one can call it",
        ),
        Some(bytes) => (
            format!("bytes {}", byte_string(bytes)),
            "The manifest's bytes and its hash are baked in (inline mode), so an\n\
             /// expansion needs no file and survives a target directory that moved",
        ),
    };
    let _ = write!(
        s,
        "
/// `tr!(\"id\", name = value, …)` — the id and the argument set checked
/// against the manifest at compile time.
///
/// {how}, and a manifest whose hash differs is
/// reported as stale rather than used (D8).
#[doc(hidden)]
#[macro_export]
macro_rules! __mf2_tr {{
    ($($t:tt)*) => {{
        $crate::__mf2::__tr_impl!({source} 0x{hash:016x}u64 ; $crate ; $($t)*)
    }};
}}
// Exported under a hidden name and named `tr` by a `use`, so that any module
// of this crate reaches it by path (`use crate::tr;`, the prelude), which a
// `macro_export` macro from an expansion cannot be (rust-lang/rust#52234).
#[allow(unused_imports)]
pub use __mf2_tr as tr;

/// `msg_id!(\"id\")` — the id checked against the manifest, and nothing
/// else: the `MsgId` a caller needs to format a message whose arguments are
/// not known until run time (`mf2::TrDyn`).
#[doc(hidden)]
#[macro_export]
macro_rules! __mf2_msg_id {{
    ($($t:tt)*) => {{
        $crate::__mf2::__msg_id_impl!({source} 0x{hash:016x}u64 ; $crate ; $($t)*)
    }};
}}
#[allow(unused_imports)]
pub use __mf2_msg_id as msg_id;
",
        hash = m.manifest_hash
    );
}

/// The id of the message that names the language `tag`: what the switcher
/// and `Locale::name()` show.
pub(crate) fn name_id(tag: &str) -> String {
    format!("language.{tag}")
}

/// For each of `tags`, whether `manifest` has its [`name_id`] with no
/// argument: [`Module::names`].
pub(crate) fn named(tags: &[&str], manifest: &mf2_catalog::Manifest) -> Vec<bool> {
    tags.iter()
        .map(|tag| {
            let id = name_id(tag);
            manifest
                .ids
                .iter()
                .position(|i| *i == id)
                .is_some_and(|at| manifest.slots.get(at).is_some_and(Vec::is_empty))
        })
        .collect()
}

/// The ids the generated module names itself, and no `tr!` of the
/// application's does: each named language's, for the switcher and
/// `Locale::name()`.
pub(crate) fn uses(tags: &[&str], manifest: &mf2_catalog::Manifest) -> Vec<String> {
    tags.iter()
        .zip(named(tags, manifest))
        .filter(|(_, named)| *named)
        .map(|(tag, _)| name_id(tag))
        .collect()
}

/// `Locale::name()`: after `tr!`, which it expands, as a macro is in scope
/// only after its definition.
fn names(s: &mut String, m: &Module<'_>) {
    if m.names.len() == m.locales.len() && m.names.iter().all(|named| *named) {
        let _ = write!(
            s,
            "
impl Locale {{
    /// The language's name, for a language menu: its `language.<tag>`
    /// message, which each translation writes.
    #[must_use]
    pub fn name(self) -> __mf2::Tr {{
        match self {{
"
        );
        for l in m.locales {
            let id = name_id(&l.tag);
            let _ = writeln!(s, "            Locale::{} => tr!({id:?}),", variant(&l.tag));
        }
        s.push_str("        }\n    }\n}\n");
    }
    // What `setup()` gives the switcher: each named language's message, by
    // its index in `LOCALES`.
    s.push_str(
        "
__mf2::__if_leptos! {
    /// The name of the language at `index` in `LOCALES`: `setup()`'s names.
    fn __locale_name(index: usize) -> Option<__mf2::Tr> {
        match index {
",
    );
    for (index, (l, named)) in m.locales.iter().zip(m.names).enumerate() {
        if *named {
            let id = name_id(&l.tag);
            let _ = writeln!(s, "            {index} => Some(tr!({id:?})),");
        }
    }
    s.push_str("            _ => None,\n        }\n    }\n}\n");
}

/// The prelude: `tr` and `msg_id`, `Locale`, the functions that choose and
/// read the language where this build has them, and the description types
/// for signatures. `install`, `setup` and `markup` stay out: each is named
/// once.
fn prelude(s: &mut String, m: &Module<'_>) {
    let mode = if has_corpus(m.emit) {
        "__if_mode"
    } else {
        "__if_leptos"
    };
    let _ = write!(
        s,
        "
/// What an application names everywhere, for `use …::prelude::*;` (in this
/// crate, `use crate::prelude::*;`): `tr!` and `msg_id!`, `Locale`, the
/// functions that choose and read the language where this build has them,
/// and the description types for signatures. `install`, `setup` and
/// `markup` stay out: each is named once.
pub mod prelude {{
    pub use super::{{Locale, msg_id, tr}};
    pub use super::__mf2::{{Tr, TrArgs, TrDyn, TrRich}};
    super::__mf2::{mode}! {{
        pub use super::{{current_locale, set_locale}};
    }}
    super::__mf2::__if_leptos! {{
        pub use super::preload_locale;
    }}
"
    );
    if has_corpus(m.emit) {
        s.push_str(
            "    super::__mf2::__if_native! {\n        pub use super::with_locale;\n    }\n",
        );
    }
    s.push_str("}\n");
}

/// `b"…"`: the bytes as a Rust byte-string literal, escaped so that the
/// generated module stays valid UTF-8 and diff-able.
fn byte_string(bytes: &[u8]) -> String {
    let mut out = String::with_capacity(bytes.len() * 2 + 3);
    out.push_str("b\"");
    for &b in bytes {
        match b {
            b'\\' => out.push_str("\\\\"),
            b'"' => out.push_str("\\\""),
            0x20..=0x7e => out.push(b as char),
            _ => {
                let _ = write!(out, "\\x{b:02x}");
            }
        }
    }
    out.push('"');
    out
}

#[cfg(test)]
mod tests {
    use super::{Module, check, constant, variant, write};
    use crate::build::{Emit, LocaleInfo};
    use crate::error::Error;
    use crate::features::Features;
    use mf2_catalog::Dir;
    use std::collections::BTreeMap;
    use std::path::Path;

    fn locales() -> Vec<LocaleInfo> {
        vec![
            LocaleInfo {
                tag: "en".to_owned(),
                dir: Dir::Ltr,
                hash: "0123456789abcdef".to_owned(),
                file_name: "en.0123456789abcdef.mf2b".to_owned(),
            },
            LocaleInfo {
                tag: "ar".to_owned(),
                dir: Dir::Rtl,
                hash: "fedcba9876543210".to_owned(),
                file_name: "ar.fedcba9876543210.mf2b".to_owned(),
            },
        ]
    }

    fn module<'a>(
        functions: &'a [String],
        features: &'a Features,
        custom: &'a BTreeMap<String, String>,
        locales: &'a [LocaleInfo],
        unannotated: bool,
    ) -> Module<'a> {
        Module {
            facade: "::mf2",
            manifest_path: Path::new("/out/manifest.mf2m"),
            manifest_hash: 0x43e0_dc12_eeb0_5ef1,
            source_locale: "en",
            locales,
            functions,
            custom,
            features,
            unannotated,
            messages: 3,
            emit: Emit::Both,
            manifest_bytes: None,
            language_matching: "__mf2::LanguageMatching::EMPTY",
            markup: &[],
            names: &[],
        }
    }

    /// The text of the first `__mf2::{gate}! { … }` block at column 0.
    fn block<'c>(code: &'c str, gate: &str) -> &'c str {
        let open = format!("\n__mf2::{gate}! {{\n");
        let Some(start) = code.find(&open) else {
            return "";
        };
        let rest = &code[start + 1..];
        &rest[..rest.find("\n}\n").map_or(rest.len(), |end| end + 3)]
    }

    #[test]
    fn the_module_carries_the_corpus_cut_of_the_matching_data() {
        let locales = locales();
        let features = Features::default();
        let custom = BTreeMap::new();
        let cut = mf2_locale_data::matching::Matching::shipped()
            .expect("the shipped table")
            .cut(&["en", "ar"])
            .encode()
            .expect("encodes")
            .rust("__mf2::LanguageMatching");
        let mut m = module(&[], &features, &custom, &locales, false);
        m.language_matching = &cut;
        let code = write(&m);
        assert!(
            code.contains("pub static LANGUAGE_MATCHING: __mf2::LanguageMatching = __mf2::LanguageMatching::new(\n"),
            "{code}"
        );
        // Numbers only: no catalog name or hash, no text of the corpus.
        let start = code.find("pub static LANGUAGE_MATCHING").unwrap_or(0);
        let table = &code[start..];
        let table = &table[..table.find(");\n").unwrap_or(table.len())];
        assert!(!table.contains(".mf2b") && !table.contains('"'), "{table}");
        // English's and Arabic's paradigms and match variables, and the
        // languages whose readers accept either.
        assert!(table.lines().count() > 100, "{table}");
    }

    #[test]
    fn the_client_sees_no_catalog_name_or_hash() {
        let locales = locales();
        let features = Features::default();
        let custom = BTreeMap::new();
        let code = write(&module(&[], &features, &custom, &locales, false));
        // Everything that names a catalog is dropped from a browser's
        // client by `mf2`'s own features — the bytes, `CATALOGS` and, with
        // `native` or on a server, `CORPUS`.
        let server = block(&code, "__if_host_std");
        let format = block(&code, "__if_format");
        assert_eq!(server.matches(".mf2b").count(), 4, "{code}");
        assert_eq!(format.matches(".mf2b").count(), 2, "{code}");
        assert_eq!(code.matches(".mf2b").count(), 6, "{code}");
        assert!(server.contains("pub static CATALOGS:"), "{code}");
        assert!(format.contains("pub static CORPUS:"), "{code}");
        assert!(
            code.contains("pub const MANIFEST_HASH: u64 = 0x43e0_dc12_eeb0_5ef1;"),
            "{code}"
        );
        assert!(code.contains("pub static LOCALES: &[(&str, __mf2::Dir)]"));
        assert!(code.contains("(\"ar\", __mf2::Dir::Rtl),"));
    }

    #[test]
    fn a_native_module_embeds_each_catalog_once() {
        let locales = locales();
        let features = Features::default();
        let custom = BTreeMap::new();
        let mut m = module(&[], &features, &custom, &locales, false);

        m.emit = Emit::Native;
        let code = write(&m);
        assert!(!code.contains("host_web"), "{code}");
        assert!(
            code.contains("pub use super::__mf2::host_std::HOST;"),
            "{code}"
        );
        // One table of bytes: `CORPUS` always, and `CATALOGS` beside `ssr`.
        assert_eq!(code.matches("include_bytes!").count(), 2, "{code}");
        assert!(
            code.contains("\npub static CORPUS: __mf2::Corpus"),
            "{code}"
        );
        assert!(
            block(&code, "__if_ssr")
                .contains("    (\"ar\", \"ar.fedcba9876543210.mf2b\", MF2_CATALOG_BYTES[1]),"),
            "{code}"
        );
        // The corpus carries the cut its locales are matched with.
        assert!(
            code.contains("\n.with_language_matching(&LANGUAGE_MATCHING);"),
            "{code}"
        );
        assert!(
            code.contains(
                "__mf2::CatalogFile::new(\"ar\", \"ar.fedcba9876543210.mf2b\", Some(MF2_CATALOG_BYTES[1])),"
            ),
            "{code}"
        );

        m.emit = Emit::NativeFiles;
        let code = write(&m);
        assert!(!code.contains("include_bytes!"), "{code}");
        assert!(!code.contains("CATALOGS"), "{code}");
        assert!(
            code.contains("__mf2::CatalogFile::new(\"en\", \"en.0123456789abcdef.mf2b\", None),"),
            "{code}"
        );
        assert!(code.contains("pub fn install_from_directory("), "{code}");
    }

    #[test]
    fn a_native_module_never_says_wasm() {
        let locales = locales();
        let features = Features::parse("fn-number,fn-datetime");
        let custom = BTreeMap::new();
        let markup = ["key".to_owned()];
        let named = vec![true; locales.len()];
        for emit in [Emit::Native, Emit::NativeFiles, Emit::Both] {
            let mut m = module(&[], &features, &custom, &locales, true);
            m.emit = emit;
            m.markup = &markup;
            m.names = &named;
            let code = write(&m).to_ascii_lowercase();
            assert_eq!(
                code.contains("wasm"),
                emit == Emit::Both,
                "{emit:?}:\n{code}"
            );
        }
    }

    #[test]
    fn every_choice_is_mf2s_features_and_one_item_each() {
        let locales = locales();
        let features = Features::parse("fn-number,fn-datetime,datetime-intl");
        let custom = BTreeMap::new();
        for emit in [Emit::Both, Emit::Module, Emit::Native, Emit::NativeFiles] {
            let mut m = module(&[], &features, &custom, &locales, false);
            m.emit = emit;
            let code = write(&m);
            // No `cfg` of the crate that includes it: that crate's
            // features say nothing about how `mf2` was built.
            assert!(!code.contains("#[cfg("), "{emit:?}:\n{code}");
            for item in [
                "pub fn setup()",
                "pub fn install()",
                "pub fn set_locale(",
                "pub fn current_locale(",
                "pub fn preload_locale(",
                "pub enum Locale",
                "FromRequestParts<S>",
            ] {
                assert_eq!(code.matches(item).count(), 1, "{emit:?} {item}:\n{code}");
            }
            let native = emit != Emit::Module;
            for item in [
                "pub fn with_locale<",
                "pub fn format(",
                "pub fn install_from_directory(",
            ] {
                assert_eq!(
                    code.matches(item).count(),
                    usize::from(native),
                    "{emit:?} {item}"
                );
            }
        }
    }

    #[test]
    fn a_web_module_has_a_corpus_only_where_it_formats() {
        let locales = locales();
        let features = Features::default();
        let custom = BTreeMap::new();
        let mut m = module(&[], &features, &custom, &locales, false);
        let code = write(&m);
        // With `native`, and on a server (`ssr`, `axum`): `Locale::format`.
        assert!(
            block(&code, "__if_format").contains("    pub static CORPUS:"),
            "{code}"
        );
        // Under `axum`, `Locale` is an extractor, and `install()` gives
        // `mf2::axum` the corpus.
        assert!(
            block(&code, "__if_axum").contains("FromRequestParts<S>"),
            "{code}"
        );
        assert!(code.contains("install_axum(&CORPUS)"), "{code}");
        assert_eq!(code.matches("pub static CORPUS:").count(), 1, "{code}");

        m.emit = Emit::Module;
        let code = write(&m);
        assert!(!code.contains("CORPUS"), "{code}");
        assert!(!code.contains("CatalogFile"), "{code}");
        assert!(!code.contains(".mf2b"), "{code}");
    }

    #[test]
    fn the_registry_is_closed_over_what_the_corpus_uses() {
        let locales = locales();
        let custom = BTreeMap::from([("app:emoji".to_owned(), "my_app::EMOJI".to_owned())]);
        let functions = ["integer".to_owned(), "app:emoji".to_owned()];

        let core = Features::default();
        let code = write(&module(&functions, &core, &custom, &locales, false));
        assert!(
            code.contains("(\"integer\", &__mf2::functions::INTEGER),"),
            "{code}"
        );
        assert!(code.contains("(\"app:emoji\", &my_app::EMOJI),"), "{code}");
        assert!(
            !code.contains("STRING"),
            "an unused handler is linked:\n{code}"
        );
        assert!(!code.contains("with_numbers"), "{code}");

        // With `fn-number`, the same function comes from the localized crate.
        let localized = Features::parse("fn-number");
        let code = write(&module(&functions, &localized, &custom, &locales, false));
        assert!(
            code.contains("(\"integer\", &__mf2::fn_number::INTEGER),"),
            "{code}"
        );
        // Still no unannotated hook: nothing in this corpus can receive one.
        assert!(!code.contains("with_numbers"), "{code}");
    }

    #[test]
    fn the_unannotated_hooks_need_a_placeholder_that_can_reach_them() {
        let locales = locales();
        let custom = BTreeMap::new();
        let both = Features::parse("fn-number,fn-datetime");
        let code = write(&module(&[], &both, &custom, &locales, true));
        assert!(
            code.contains(".with_numbers(&__mf2::fn_number::NUMBERS)"),
            "{code}"
        );
        assert!(
            code.contains(".with_dates(&__mf2::fn_datetime::DATES)"),
            "{code}"
        );
    }

    #[test]
    fn a_corpus_without_dates_never_names_a_date_host() {
        let locales = locales();
        let custom = BTreeMap::new();
        let none = Features::default();
        let code = write(&module(&[], &none, &custom, &locales, false));
        assert!(code.contains("super::__mf2::__use_host!();"), "{code}");

        let dates = Features::parse("fn-datetime,datetime-intl");
        let code = write(&module(&[], &dates, &custom, &locales, false));
        assert!(code.contains("super::__mf2::__use_host!(dates);"), "{code}");
    }

    #[test]
    fn inline_mode_bakes_the_manifest_itself() {
        let locales = locales();
        let custom = BTreeMap::new();
        let features = Features::default();
        let mut m = module(&[], &features, &custom, &locales, false);
        // A manifest's first bytes: the magic, the version, the hash — the
        // escaping has to survive both text and non-text bytes.
        let bytes = b"MF2M\x00\x01\xf1^\xb0\xee\x12\xdc\xe0C\"\\";
        m.manifest_bytes = Some(bytes);
        let code = write(&m);
        assert!(
            code.contains(
                "$crate::__mf2::__tr_impl!(bytes b\"MF2M\\x00\\x01\\xf1^\\xb0\\xee\\x12\\xdc\\xe0C\\\"\\\\\" 0x43e0dc12eeb05ef1u64"
            ),
            "{code}"
        );
        assert!(!code.contains("/out/manifest.mf2m"), "{code}");
    }

    #[test]
    fn the_tr_wrapper_bakes_in_the_path_and_the_hash() {
        let locales = locales();
        let custom = BTreeMap::new();
        let features = Features::default();
        let code = write(&module(&[], &features, &custom, &locales, false));
        assert!(
            code.contains("$crate::__mf2::__tr_impl!(\"/out/manifest.mf2m\" 0x43e0dc12eeb05ef1u64 ; $crate ; $($t)*)"),
            "{code}"
        );
        assert!(
            code.contains("$crate::__mf2::__msg_id_impl!(\"/out/manifest.mf2m\" 0x43e0dc12eeb05ef1u64 ; $crate ; $($t)*)"),
            "{code}"
        );
    }

    #[test]
    fn locale_has_a_variant_per_tag() {
        let locales = locales();
        let features = Features::default();
        let custom = BTreeMap::new();
        let code = write(&module(&[], &features, &custom, &locales, false));
        for expected in [
            "pub enum Locale {\n    /// `en`\n    En,\n    /// `ar`\n    Ar,\n}",
            "pub const ALL: [Locale; 2] = [Locale::En, Locale::Ar];",
            "pub const SOURCE: Locale = Locale::En;",
            "Locale::Ar => \"ar\",",
            "Locale::Ar => __mf2::Dir::Rtl,\n            _ => __mf2::Dir::Ltr,",
            "impl ::core::str::FromStr for Locale {\n    type Err = __mf2::UnknownLocale;",
            "__mf2::__best_locale!(LANGUAGE_MATCHING, LOCALES, tag)",
            "impl ::core::fmt::Display for Locale {",
        ] {
            assert!(code.contains(expected), "{expected}:\n{code}");
        }
        assert!(
            block(&code, "__if_clap")
                .contains("impl __mf2::__generated::clap::ValueParserFactory for Locale {"),
            "{code}"
        );
        // A client-only application's setup alone carries the matching data.
        assert!(
            block(&code, "__if_leptos").contains(
                "        __mf2::__if_csr! {\n            let setup = setup.with_language_matching(&LANGUAGE_MATCHING);"
            ),
            "{code}"
        );
    }

    #[test]
    fn variants_and_constants_are_named_by_rule() {
        assert_eq!(variant("pt-BR"), "PtBr");
        assert_eq!(variant("es-419"), "Es419");
        assert_eq!(variant("zh-Hant-TW"), "ZhHantTw");
        assert_eq!(variant("und"), "Und");
        assert_eq!(constant("key"), "KEY");
        assert_eq!(constant("ns:warn-now.x"), "NS_WARN_NOW_X");
        assert_eq!(constant("caf\u{e9}"), "CAF_");
    }

    #[test]
    fn a_shared_variant_is_refused_and_a_shared_constant_left_out() {
        let features = Features::default();
        let custom = BTreeMap::new();
        let mut two = locales();
        two[0].tag = "pt-BR".to_owned();
        two[1].tag = "pt_br".to_owned();
        let m = module(&[], &features, &custom, &two, false);
        assert!(
            matches!(check(&m), Err(Error::LocaleVariant { variant, .. }) if variant == "PtBr")
        );

        // Every markup name MF2 allows is valid: one that gives no
        // constant, or the same one as another, is left out, not refused.
        let locales = locales();
        let mut m = module(&[], &features, &custom, &locales, false);
        let names = [
            "+:_\u{a1}".to_owned(),
            "a-b".to_owned(),
            "a.b".to_owned(),
            "ok".to_owned(),
        ];
        m.markup = &names;
        assert!(check(&m).is_ok());
        let code = write(&m);
        let ratatui = block(&code, "__if_ratatui");
        assert_eq!(ratatui.matches("pub const ").count(), 1, "{ratatui}");
        assert!(ratatui.contains("pub const OK:"), "{ratatui}");
    }

    #[test]
    fn markup_names_are_constants_with_their_hash() {
        let locales = locales();
        let features = Features::default();
        let custom = BTreeMap::new();
        let markup = ["key".to_owned(), "ns:warn".to_owned()];
        let mut m = module(&[], &features, &custom, &locales, false);
        m.markup = &markup;
        let code = write(&m);
        let ratatui = block(&code, "__if_ratatui");
        let key = super::hex_u64(mf2_catalog::markup_key("key"));
        assert!(
            ratatui.contains(&format!(
                "    pub const KEY: super::__mf2::ratatui::Markup =\n            super::__mf2::ratatui::Markup::new({key}, \"key\");"
            )),
            "{code}"
        );
        assert!(ratatui.contains("pub const NS_WARN:"), "{code}");
    }

    #[test]
    fn the_prelude_holds_what_the_build_has() {
        let locales = locales();
        let features = Features::default();
        let custom = BTreeMap::new();
        let mut m = module(&[], &features, &custom, &locales, false);
        let code = write(&m);
        let prelude = &code[code.find("pub mod prelude {").unwrap_or(0)..];
        for expected in [
            "pub use super::{Locale, msg_id, tr};",
            "pub use super::__mf2::{Tr, TrArgs, TrDyn, TrRich};",
            "super::__mf2::__if_mode! {\n        pub use super::{current_locale, set_locale};",
            "super::__mf2::__if_leptos! {\n        pub use super::preload_locale;",
            "super::__mf2::__if_native! {\n        pub use super::with_locale;",
        ] {
            assert!(prelude.contains(expected), "{expected}:\n{prelude}");
        }
        assert!(!prelude.contains("install") && !prelude.contains("markup"));

        m.emit = Emit::Module;
        let code = write(&m);
        let prelude = &code[code.find("pub mod prelude {").unwrap_or(0)..];
        assert!(
            prelude.contains(
                "super::__mf2::__if_leptos! {\n        pub use super::{current_locale, set_locale};"
            ),
            "{prelude}"
        );
        assert!(!prelude.contains("with_locale"), "{prelude}");
    }

    #[test]
    fn names_come_after_the_macro_they_expand() {
        let locales = locales();
        let features = Features::default();
        let custom = BTreeMap::new();
        let named = vec![true; locales.len()];
        let some = vec![false; locales.len()];
        let mut m = module(&[], &features, &custom, &locales, false);
        assert!(!write(&m).contains("pub fn name("));
        m.names = &some;
        assert!(!write(&m).contains("with_names"));
        m.names = &named;
        let code = write(&m);
        let name = code.find("pub fn name(self) -> __mf2::Tr").unwrap_or(0);
        assert!(
            name > code.find("macro_rules! __mf2_tr {").unwrap_or(usize::MAX),
            "{code}"
        );
        // A3's shape (3c): exported under hidden names, named by a `use`,
        // and no textual `tr` beside it, which would make the name ambiguous.
        for expected in [
            "#[doc(hidden)]\n#[macro_export]\nmacro_rules! __mf2_tr {",
            "pub use __mf2_tr as tr;",
            "#[doc(hidden)]\n#[macro_export]\nmacro_rules! __mf2_msg_id {",
            "pub use __mf2_msg_id as msg_id;",
        ] {
            assert!(code.contains(expected), "{expected}:\n{code}");
        }
        assert!(
            !code.contains("macro_rules! tr ") && !code.contains("macro_rules! msg_id "),
            "{code}"
        );
        assert!(
            code.contains("Locale::Ar => tr!(\"language.ar\"),"),
            "{code}"
        );
        // The switcher's names, by index, after the macro too.
        let at = code.find("fn __locale_name(").unwrap_or(0);
        assert!(at > code.find("macro_rules! __mf2_tr {").unwrap_or(usize::MAX));
        assert!(
            code.contains("let setup = setup.with_names(__locale_name);"),
            "{code}"
        );
    }
}