mf2-locale-data 2.0.0

CLDR locale data for Rust MF2 catalogs (build side): all-locale plural, direction, number, currency and unit tables and the LOCALE entries built from them.
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
//! CLDR's language-matching data, build side (plans/19-native-and-terminal.md
//! §9; the master plan's D21): likely subtags (UTS #35 Part 1 §4.3), and the
//! rules, match variables and paradigm locales of language matching (§4.4),
//! shipped as `data/matching.txt` (`cargo xtask locale-data`, from the
//! vendored `likelySubtags.json`, `languageMatching.json` and
//! `territoryContainment.json`).
//!
//! * [`Matching::shipped`] reads the table;
//! * [`Matching::cut`] keeps what a corpus's languages need, which gives them
//!   the answers the whole table gives;
//! * [`Matching::encode`] packs a table as `mf2`'s matcher reads it, and
//!   [`Encoded::rust`] writes that as Rust: the generated module's
//!   `LANGUAGE_MATCHING` (`mf2-build`), and `mf2`'s own whole table.
//!
//! The packing is `mf2`'s (`crates/mf2/src/matching.rs`): a language, script
//! or two-letter region as its letters, 5 bits each (`a` is 1); a
//! three-digit region as 1024 plus its number; a region-level pattern's
//! match variable as `0x8000` and its index, `0x4000` more for `$!`.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write as _;
use std::sync::OnceLock;

use crate::error::Error;

/// The shipped table (`cargo xtask locale-data`).
const TABLE: &str = include_str!("../data/matching.txt");

/// A region pattern's match variable, as `mf2` reads it.
const VARIABLE: u16 = 0x8000;
/// A negated one (`$!name`).
const NEGATED: u16 = 0x4000;

/// CLDR's language-matching data as the data states it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Matching {
    /// The CLDR release it comes from.
    pub cldr: String,
    /// The paradigm locales, as the data lists them.
    pub paradigms: Vec<String>,
    /// The match variables, in the data's order.
    pub variables: Vec<Variable>,
    /// The rules, in the data's order: each level's specific rules, then
    /// its default (`*`, `*-*`, `*-*-*`).
    pub rules: Vec<Rule>,
    /// Likely subtags: a tag with a language (never `und`, which the
    /// matcher never fills) → language-script-region.
    pub likely: BTreeMap<String, String>,
}

/// A match variable.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Variable {
    /// Its name, with the `$`.
    pub name: String,
    /// Its value as the data writes it (`019`, `HK+MO`).
    pub value: String,
    /// The regions inside it, ascending: the value's, each macroregion
    /// replaced by its contents, and every macroregion all of whose contents
    /// are among them (the reading that reproduces the specification's
    /// `es-419` examples: 18, C3's text half).
    pub regions: Vec<String>,
}

/// One `languageMatch` rule.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Rule {
    /// The reader's side: one, two or three fields, `*` for any.
    pub desired: String,
    /// The application's side.
    pub supported: String,
    /// The distance it adds.
    pub distance: u8,
    /// Whether it matches only from the reader's side to the application's.
    pub oneway: bool,
}

impl Rule {
    /// 1, 2 or 3: language, script or region.
    fn level(&self) -> usize {
        self.desired.split('-').count()
    }

    fn is_default(&self) -> bool {
        self.desired.split('-').all(|f| f == "*") && self.supported.split('-').all(|f| f == "*")
    }
}

impl Matching {
    /// The shipped table.
    pub fn shipped() -> Result<&'static Matching, Error> {
        static PARSED: OnceLock<Result<Matching, String>> = OnceLock::new();
        match PARSED.get_or_init(|| Matching::parse(TABLE).map_err(|e| e.to_string())) {
            Ok(m) => Ok(m),
            Err(message) => Err(Error::Table {
                line: 0,
                message: message.clone(),
            }),
        }
    }

    /// Reads `data/matching.txt`'s format.
    pub fn parse(text: &str) -> Result<Matching, Error> {
        let mut m = Matching {
            cldr: String::new(),
            paradigms: Vec::new(),
            variables: Vec::new(),
            rules: Vec::new(),
            likely: BTreeMap::new(),
        };
        for (i, line) in text.lines().enumerate() {
            let bad = |message: &str| Error::Table {
                line: i + 1,
                message: message.to_owned(),
            };
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            let words: Vec<&str> = line.split(' ').collect();
            match words.as_slice() {
                ["cldr", version] => (*version).clone_into(&mut m.cldr),
                ["paradigms", rest @ ..] => {
                    m.paradigms = rest.iter().map(|s| (*s).to_owned()).collect();
                }
                ["variable", name, value, ":", regions @ ..] => m.variables.push(Variable {
                    name: (*name).to_owned(),
                    value: (*value).to_owned(),
                    regions: regions.iter().map(|s| (*s).to_owned()).collect(),
                }),
                ["rule", desired, supported, distance, rest @ ..] => {
                    let distance = distance.parse().map_err(|_| bad("a distance"))?;
                    let oneway = match rest {
                        [] => false,
                        ["oneway"] => true,
                        _ => return Err(bad("`oneway` or nothing after the distance")),
                    };
                    m.rules.push(Rule {
                        desired: (*desired).to_owned(),
                        supported: (*supported).to_owned(),
                        distance,
                        oneway,
                    });
                }
                ["likely", from, to] => {
                    m.likely.insert((*from).to_owned(), (*to).to_owned());
                }
                _ => return Err(bad("unknown line")),
            }
        }
        Ok(m)
    }

    /// The table's text: what [`Matching::parse`] reads and `cargo xtask
    /// locale-data` writes.
    #[must_use]
    pub fn text(&self) -> String {
        let mut out = String::new();
        out.push_str(
            "# mf2-locale-data: CLDR's language-matching data: likely subtags (UTS #35 Part 1 §4.3),\n\
             # and the rules, match variables and paradigm locales of language matching (§4.4).\n",
        );
        out.push_str(
            "# Generated by `cargo xtask locale-data` from third_party/cldr-json. Do not edit.\n",
        );
        out.push_str(
            "# `variable NAME VALUE : REGIONS`: the regions inside the variable, macroregions\n\
             # replaced by their contents, and each macroregion all of whose contents are inside.\n\
             # `rule DESIRED SUPPORTED DISTANCE [oneway]`, in the data's order. `likely FROM TO`,\n\
             # without `und`, which the matcher never fills.\n",
        );
        let _ = writeln!(out, "cldr {}", self.cldr);
        let _ = writeln!(out, "paradigms {}", self.paradigms.join(" "));
        for v in &self.variables {
            let _ = writeln!(
                out,
                "variable {} {} : {}",
                v.name,
                v.value,
                v.regions.join(" ")
            );
        }
        for r in &self.rules {
            let _ = writeln!(
                out,
                "rule {} {} {}{}",
                r.desired,
                r.supported,
                r.distance,
                if r.oneway { " oneway" } else { "" }
            );
        }
        for (from, to) in &self.likely {
            let _ = writeln!(out, "likely {from} {to}");
        }
        out
    }

    /// What a corpus of `locales` needs, and nothing more: for any reader's
    /// tags, matching against those locales (or any of them) gives what the
    /// whole table gives.
    ///
    /// * the rules whose application's side can be one of the corpus's
    ///   languages (a two-way rule's either side), and the defaults;
    /// * the likely subtags of the corpus's languages, and of every language
    ///   a kept language-level rule accepts for one of them: of any other
    ///   language, a reader is farther than the default language distance
    ///   from every locale, which no demotion or tie can bring back;
    /// * the variables the kept rules name, and the paradigm locales of the
    ///   corpus's languages.
    #[must_use]
    pub fn cut(&self, locales: &[&str]) -> Matching {
        let own: BTreeSet<String> = locales
            .iter()
            .map(|t| language_of(t).to_ascii_lowercase())
            .collect();
        let applies = |field: &str| field == "*" || own.contains(field);
        let mut readers = own.clone();
        let mut rules = Vec::new();
        for rule in &self.rules {
            let d = language_of(&rule.desired);
            let s = language_of(&rule.supported);
            let forward = applies(s);
            let backward = !rule.oneway && applies(d);
            if !(forward || backward) {
                continue;
            }
            if rule.level() == 1 {
                if forward && d != "*" {
                    readers.insert(d.to_owned());
                }
                if backward && s != "*" {
                    readers.insert(s.to_owned());
                }
            }
            rules.push(rule.clone());
        }
        let named: BTreeSet<&str> = rules
            .iter()
            .flat_map(|r| [r.desired.as_str(), r.supported.as_str()])
            .filter_map(|p| p.split('-').nth(2))
            .filter_map(|region| region.strip_prefix('$'))
            .map(|name| name.strip_prefix('!').unwrap_or(name))
            .collect();
        Matching {
            cldr: self.cldr.clone(),
            paradigms: self
                .paradigms
                .iter()
                .filter(|p| own.contains(language_of(p)))
                .cloned()
                .collect(),
            variables: self
                .variables
                .iter()
                .filter(|v| named.contains(v.name.trim_start_matches('$')))
                .cloned()
                .collect(),
            rules,
            likely: self
                .likely
                .iter()
                .filter(|(from, _)| readers.contains(language_of(from)))
                .map(|(from, to)| (from.clone(), to.clone()))
                .collect(),
        }
    }

    /// The table packed as `mf2`'s matcher reads it.
    pub fn encode(&self) -> Result<Encoded, Error> {
        let bad = |message: String| Error::Table { line: 0, message };
        let mut scripts = BTreeSet::new();
        let mut regions = BTreeSet::new();
        let mut filled = Vec::with_capacity(self.likely.len());
        for (from, to) in &self.likely {
            let key = split(from).ok_or_else(|| bad(format!("likely key {from:?}")))?;
            let [_, script, region] = split(to)
                .filter(|[_, s, r]| !s.is_empty() && !r.is_empty())
                .ok_or_else(|| bad(format!("likely value {to:?}")))?;
            let script = pack_script(script).ok_or_else(|| bad(format!("script of {to:?}")))?;
            let region = pack_region(region).ok_or_else(|| bad(format!("region of {to:?}")))?;
            scripts.insert(script);
            regions.insert(region);
            filled.push((key, script, region));
        }
        let scripts: Vec<u32> = scripts.into_iter().collect();
        let regions: Vec<u16> = regions.into_iter().collect();
        let index = |list: &[u32], v: u32| {
            list.binary_search(&v)
                .ok()
                .and_then(|i| u8::try_from(i).ok())
        };
        let region_index = |v: u16| {
            regions
                .binary_search(&v)
                .ok()
                .and_then(|i| u8::try_from(i).ok())
        };
        let mut languages = Vec::new();
        let mut tags = Vec::new();
        for ([language, script, region], s, r) in filled {
            let si = index(&scripts, s).ok_or_else(|| bad("more than 256 scripts".to_owned()))?;
            let ri = region_index(r).ok_or_else(|| bad("more than 256 regions".to_owned()))?;
            let l = pack_language(language)
                .ok_or_else(|| bad(format!("likely language {language:?}")))?;
            if script.is_empty() && region.is_empty() {
                languages.push((l, si, ri));
            } else {
                let ks = if script.is_empty() {
                    Some(0)
                } else {
                    pack_script(script)
                };
                let kr = if region.is_empty() {
                    Some(0)
                } else {
                    pack_region(region)
                };
                let (Some(ks), Some(kr)) = (ks, kr) else {
                    return Err(bad(format!("likely key {language}-{script}-{region}")));
                };
                tags.push((l, ks, kr, si, ri));
            }
        }
        languages.sort_unstable();
        tags.sort_unstable_by_key(|&(l, ks, kr, _, _)| tag_key(l, ks, kr));

        let mut defaults = [None::<u8>; 3];
        let mut language_pairs = BTreeMap::new();
        let mut script_pairs = BTreeMap::new();
        let mut region_rules = Vec::new();
        // The variables the region rules name, as indices into
        // `self.variables`, in the order first named.
        let mut variables: Vec<usize> = Vec::new();
        for rule in &self.rules {
            let level = rule.level();
            let default = defaults
                .get_mut(level - 1)
                .ok_or_else(|| bad(format!("rule {} has {level} fields", rule.desired)))?;
            if default.is_some() {
                return Err(bad(format!(
                    "rule {} {} after its level's default",
                    rule.desired, rule.supported
                )));
            }
            if rule.is_default() {
                *default = Some(rule.distance);
                continue;
            }
            let pattern = |p: &str, variables: &mut Vec<usize>| -> Result<(u16, u32, u16), Error> {
                let fields: Vec<&str> = p.split('-').collect();
                let field = |i: usize| fields.get(i).copied().unwrap_or("*");
                let language = match field(0) {
                    "*" => 0,
                    l => pack_language(l).ok_or_else(|| bad(format!("rule language {l:?}")))?,
                };
                let script = match field(1) {
                    "*" => 0,
                    s => pack_script(s).ok_or_else(|| bad(format!("rule script {s:?}")))?,
                };
                let region = match field(2) {
                    "*" => 0,
                    r if r.starts_with('$') => {
                        let (negated, name) = match r.strip_prefix("$!") {
                            Some(name) => (true, name),
                            None => (false, r.trim_start_matches('$')),
                        };
                        let index = self
                            .variables
                            .iter()
                            .position(|v| v.name.trim_start_matches('$') == name)
                            .ok_or_else(|| bad(format!("no variable {name}")))?;
                        let at = if let Some(at) = variables.iter().position(|&i| i == index) {
                            at
                        } else {
                            variables.push(index);
                            variables.len() - 1
                        };
                        let at = u16::try_from(at)
                            .ok()
                            .filter(|at| *at < NEGATED)
                            .ok_or_else(|| bad("too many variables".to_owned()))?;
                        VARIABLE | if negated { NEGATED } else { 0 } | at
                    }
                    r => pack_region(r).ok_or_else(|| bad(format!("rule region {r:?}")))?,
                };
                Ok((language, script, region))
            };
            let d = pattern(&rule.desired, &mut variables)?;
            let s = pattern(&rule.supported, &mut variables)?;
            match level {
                // The first rule in the data's order that matches an ordered
                // pair decides it; a two-way rule gives both orders. Only the
                // default has a `*` at these levels (checked below).
                1 | 2 if d.0 == 0 || s.0 == 0 || (level == 2 && (d.1 == 0 || s.1 == 0)) => {
                    return Err(bad(format!(
                        "rule {} {}: a `*` outside the default",
                        rule.desired, rule.supported
                    )));
                }
                1 => {
                    language_pairs.entry((d.0, s.0)).or_insert(rule.distance);
                    if !rule.oneway {
                        language_pairs.entry((s.0, d.0)).or_insert(rule.distance);
                    }
                }
                2 => {
                    script_pairs
                        .entry((d.0, d.1, s.0, s.1))
                        .or_insert(rule.distance);
                    if !rule.oneway {
                        script_pairs
                            .entry((s.0, s.1, d.0, d.1))
                            .or_insert(rule.distance);
                    }
                }
                _ => region_rules.push((d.0, d.1, d.2, s.0, s.1, s.2, rule.distance, rule.oneway)),
            }
        }
        let [Some(language), Some(script), Some(region)] = defaults else {
            return Err(bad("a level without its default rule".to_owned()));
        };
        let mut paradigms = Vec::new();
        for p in &self.paradigms {
            let [l, s, r] = self
                .maximize(p)
                .ok_or_else(|| bad(format!("paradigm {p:?}")))?;
            let packed = (pack_language(&l), pack_script(&s), pack_region(&r));
            let (Some(l), Some(s), Some(r)) = packed else {
                return Err(bad(format!("paradigm {p:?}")));
            };
            paradigms.push((l, s, r));
        }
        let variables = variables
            .into_iter()
            .filter_map(|i| self.variables.get(i))
            .map(|v| {
                let mut packed = v
                    .regions
                    .iter()
                    .map(|r| {
                        pack_region(r).ok_or_else(|| bad(format!("region {r:?} of {}", v.name)))
                    })
                    .collect::<Result<Vec<u16>, Error>>()?;
                packed.sort_unstable();
                Ok(packed)
            })
            .collect::<Result<Vec<_>, Error>>()?;
        Ok(Encoded {
            scripts,
            regions,
            languages: languages.iter().map(|e| e.0).collect(),
            language_values: languages.iter().map(|e| (e.1, e.2)).collect(),
            tags: tags
                .iter()
                .map(|&(l, ks, kr, _, _)| tag_key(l, ks, kr))
                .collect(),
            tag_values: tags.iter().map(|e| (e.3, e.4)).collect(),
            language_pairs: language_pairs
                .keys()
                .map(|&(d, s)| (u32::from(d) << 16) | u32::from(s))
                .collect(),
            language_distances: language_pairs.values().copied().collect(),
            script_pairs: script_pairs
                .into_iter()
                .map(|((dl, ds, sl, ss), n)| (dl, ds, sl, ss, n))
                .collect(),
            region_rules,
            variables,
            paradigms,
            defaults: [language, script, region],
        })
    }

    /// `crates/mf2/src/matching/cldr.rs`: the whole table as `mf2` carries
    /// it on a server and in a native application (`cargo xtask
    /// locale-data` writes it; `tests/table.rs` checks the committed copy).
    pub fn mf2_module(&self) -> Result<String, Error> {
        let table = self.encode()?.rust("super::LanguageMatching");
        Ok(format!(
            "// @generated by `cargo xtask locale-data` from third_party/cldr-json (CLDR {cldr}): the\n\
             // whole of CLDR's language-matching data, packed as `super::LanguageMatching` reads it\n\
             // (mf2-locale-data's `data/matching.txt`, `Matching::encode`). Do not edit.\n\
             \n\
             /// CLDR's whole table: what a server and a native application match with\n\
             /// (plans/19-native-and-terminal.md §9).\n\
             pub(super) static CLDR: super::LanguageMatching = {table};\n",
            cldr = self.cldr
        ))
    }

    /// §4.3's Add Likely Subtags on the table's own strings: language,
    /// script and region, the empty ones filled by the first of
    /// language-script, language-region and language the table has.
    fn maximize(&self, tag: &str) -> Option<[String; 3]> {
        let [language, script, region] = split(tag)?;
        if !script.is_empty() && !region.is_empty() {
            return Some([language.to_owned(), script.to_owned(), region.to_owned()]);
        }
        let keys = [
            (!script.is_empty()).then(|| format!("{language}-{script}")),
            (!region.is_empty()).then(|| format!("{language}-{region}")),
            Some(language.to_owned()),
        ];
        let found = keys
            .into_iter()
            .flatten()
            .find_map(|k| self.likely.get(&k))?;
        let [_, s, r] = split(found)?;
        Some([
            language.to_owned(),
            (if script.is_empty() { s } else { script }).to_owned(),
            (if region.is_empty() { r } else { region }).to_owned(),
        ])
    }
}

/// A table packed as `mf2`'s `LanguageMatching` holds it: each field is its
/// namesake there, in the same order.
#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(clippy::type_complexity)]
pub struct Encoded {
    /// Script codes, by index.
    pub scripts: Vec<u32>,
    /// Region codes, by index.
    pub regions: Vec<u16>,
    /// The bare languages with likely subtags, ascending…
    pub languages: Vec<u16>,
    /// …and the script and region each fills (indices).
    pub language_values: Vec<(u8, u8)>,
    /// A language with a script or a region ([`tag_key`]), ascending…
    pub tags: Vec<u64>,
    /// …and the script and region each fills (indices).
    pub tag_values: Vec<(u8, u8)>,
    /// The language level's ordered pairs, the reader's language above the
    /// application's, ascending…
    pub language_pairs: Vec<u32>,
    /// …and their distances.
    pub language_distances: Vec<u8>,
    /// The script level's ordered pairs.
    pub script_pairs: Vec<(u16, u32, u16, u32, u8)>,
    /// The region level's rules, in the data's order, without the default.
    pub region_rules: Vec<(u16, u32, u16, u16, u32, u16, u8, bool)>,
    /// Each variable the region rules name, its regions ascending.
    pub variables: Vec<Vec<u16>>,
    /// The paradigm locales, filled in.
    pub paradigms: Vec<(u16, u32, u16)>,
    /// The three levels' defaults.
    pub defaults: [u8; 3],
}

/// An integer as a Rust literal, its digits grouped by three from five
/// digits on (`394_894`), so that clippy's pedantic lints stay quiet in the
/// crate that includes the table.
struct Int(u64);

impl core::fmt::Display for Int {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let digits = self.0.to_string();
        if digits.len() < 5 {
            return f.write_str(&digits);
        }
        for (i, d) in digits.chars().enumerate() {
            if i > 0 && (digits.len() - i).is_multiple_of(3) {
                f.write_str("_")?;
            }
            write!(f, "{d}")?;
        }
        Ok(())
    }
}

impl Encoded {
    /// A Rust expression of the table: `{ty}::new(…)`, where `ty` is the
    /// path of `mf2`'s `LanguageMatching` (`__mf2::LanguageMatching` in the
    /// generated module).
    #[must_use]
    pub fn rust(&self, ty: &str) -> String {
        // Ten numbers or pairs to a line, a tuple of more to a line of its
        // own.
        fn list<T>(out: &mut String, items: &[T], item: impl Fn(&mut String, &T)) {
            if items.is_empty() {
                out.push_str("    &[],\n");
                return;
            }
            let per_line = if core::mem::size_of::<T>() <= 8 {
                10
            } else {
                1
            };
            out.push_str("    &[\n");
            for line in items.chunks(per_line) {
                out.push_str("       ");
                for i in line {
                    out.push(' ');
                    item(out, i);
                    out.push(',');
                }
                out.push('\n');
            }
            out.push_str("    ],\n");
        }
        let mut out = String::with_capacity(64 + 24 * self.languages.len());
        let _ = writeln!(out, "{ty}::new(");
        let n = |v: u32| Int(u64::from(v));
        list(&mut out, &self.scripts, |o, s| {
            let _ = write!(o, "{}", n(*s));
        });
        list(&mut out, &self.regions, |o, r| {
            let _ = write!(o, "{}", n((*r).into()));
        });
        let pair = |o: &mut String, (s, r): &(u8, u8)| {
            let _ = write!(o, "({s}, {r})");
        };
        list(&mut out, &self.languages, |o, l| {
            let _ = write!(o, "{}", n((*l).into()));
        });
        list(&mut out, &self.language_values, pair);
        list(&mut out, &self.tags, |o, key| {
            let _ = write!(o, "{}", Int(*key));
        });
        list(&mut out, &self.tag_values, pair);
        list(&mut out, &self.language_pairs, |o, key| {
            let _ = write!(o, "{}", n(*key));
        });
        list(&mut out, &self.language_distances, |o, d| {
            let _ = write!(o, "{d}");
        });
        list(&mut out, &self.script_pairs, |o, (dl, ds, sl, ss, dist)| {
            let _ = write!(
                o,
                "({}, {}, {}, {}, {dist})",
                n((*dl).into()),
                n(*ds),
                n((*sl).into()),
                n(*ss)
            );
        });
        list(
            &mut out,
            &self.region_rules,
            |o, (dl, ds, dr, sl, ss, sr, dist, one)| {
                let _ = write!(
                    o,
                    "({}, {}, {}, {}, {}, {}, {dist}, {one})",
                    n((*dl).into()),
                    n(*ds),
                    n((*dr).into()),
                    n((*sl).into()),
                    n(*ss),
                    n((*sr).into())
                );
            },
        );
        list(&mut out, &self.variables, |o, v| {
            o.push_str("&[");
            for (i, r) in v.iter().enumerate() {
                if i > 0 {
                    o.push_str(", ");
                }
                let _ = write!(o, "{}", n((*r).into()));
            }
            o.push(']');
        });
        list(&mut out, &self.paradigms, |o, (l, s, r)| {
            let _ = write!(o, "({}, {}, {})", n((*l).into()), n(*s), n((*r).into()));
        });
        let [l, s, r] = self.defaults;
        let _ = write!(out, "    [{l}, {s}, {r}],\n)");
        out
    }
}

/// A language with a script or a region, as `mf2`'s `tags` key it: the
/// language above the script above the region.
pub fn tag_key(language: u16, script: u32, region: u16) -> u64 {
    (u64::from(language) << 36) | (u64::from(script) << 16) | u64::from(region)
}

/// The language field of a tag or a rule's side.
fn language_of(tag: &str) -> &str {
    tag.split(['-', '_']).next().unwrap_or("")
}

/// A tag of the table as language, script and region, each `""` where it
/// has none.
fn split(tag: &str) -> Option<[&str; 3]> {
    let mut fields = tag.split('-');
    let language = fields.next().filter(|l| !l.is_empty())?;
    let mut next = fields.next();
    let mut script = "";
    if let Some(s) = next.filter(|s| s.len() == 4) {
        script = s;
        next = fields.next();
    }
    let mut region = "";
    if let Some(r) = next {
        region = r;
        next = fields.next();
    }
    next.is_none().then_some([language, script, region])
}

/// Letters packed 5 bits each (`a` is 1), in any case.
fn pack(letters: &str) -> Option<u64> {
    letters.bytes().try_fold(0, |packed, b| {
        b.is_ascii_alphabetic()
            .then(|| (packed << 5) | u64::from(b.to_ascii_lowercase() - b'a' + 1))
    })
}

/// A language of two or three letters, as `mf2`'s tables hold one.
pub fn pack_language(language: &str) -> Option<u16> {
    matches!(language.len(), 2 | 3)
        .then(|| pack(language))
        .flatten()
        .and_then(|p| u16::try_from(p).ok())
}

/// A script of four letters.
pub fn pack_script(script: &str) -> Option<u32> {
    (script.len() == 4)
        .then(|| pack(script))
        .flatten()
        .and_then(|p| u32::try_from(p).ok())
}

/// A region: two letters, or three digits as 1024 plus their number.
pub fn pack_region(region: &str) -> Option<u16> {
    match region.len() {
        2 => pack(region).and_then(|p| u16::try_from(p).ok()),
        3 => region
            .parse::<u16>()
            .ok()
            .filter(|_| region.bytes().all(|b| b.is_ascii_digit()))
            .map(|n| 1024 + n),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::{Int, Matching, TABLE};

    #[test]
    fn the_table_reads_back_as_written() {
        let m = Matching::parse(TABLE).expect("the shipped table parses");
        assert_eq!(m.text(), TABLE);
        assert_eq!(m.cldr, "48.2.1");
        assert_eq!(
            m.likely.get("zh-TW").map(String::as_str),
            Some("zh-Hant-TW")
        );
        assert!(!m.likely.keys().any(|k| k == "und" || k.starts_with("und-")));
    }

    #[test]
    fn a_cut_keeps_what_serves_the_corpus() {
        let m = Matching::shipped().expect("the shipped table");
        let cut = m.cut(&["ar", "en", "fr"]);
        let has = |d: &str, s: &str| cut.rules.iter().any(|r| r.desired == d && r.supported == s);
        // Readers of Acholi accept English, of Breton French: kept, with
        // those languages' likely subtags.
        assert!(has("ach", "en") && has("br", "fr"));
        assert!(cut.likely.contains_key("ach") && cut.likely.contains_key("br"));
        // Readers of Abkhazian accept Russian, which the corpus lacks.
        assert!(!has("ab", "ru") && !cut.likely.contains_key("ab"));
        // Each level's default, English's and Arabic's region rules and
        // their variables; Spanish's `$americas` not.
        assert!(has("*", "*") && has("*-*", "*-*") && has("*-*-*", "*-*-*"));
        let names: Vec<&str> = cut.variables.iter().map(|v| v.name.as_str()).collect();
        assert_eq!(names, ["$enUS", "$maghreb"]);
        assert_eq!(cut.paradigms, ["en", "en-GB"]);
        assert!(cut.encode().is_ok());
    }

    #[test]
    fn a_variable_holds_its_macroregions_when_all_their_contents_are_inside() {
        let m = Matching::shipped().expect("the shipped table");
        let americas = m
            .variables
            .iter()
            .find(|v| v.name == "$americas")
            .expect("$americas");
        for inside in ["019", "419", "005", "013", "021", "029", "MX", "US"] {
            assert!(americas.regions.iter().any(|r| r == inside), "{inside}");
        }
        for outside in ["001", "150", "ES", "EU"] {
            assert!(!americas.regions.iter().any(|r| r == outside), "{outside}");
        }
    }

    #[test]
    fn integers_are_grouped_from_five_digits() {
        assert_eq!(Int(0).to_string(), "0");
        assert_eq!(Int(1234).to_string(), "1234");
        assert_eq!(Int(12_345).to_string(), "12_345");
        assert_eq!(Int(394_894).to_string(), "394_894");
        assert_eq!(Int(1_048_575).to_string(), "1_048_575");
    }
}