i18n-next 0.2.0

i18n for Rust and Tauri on the same locale files as nuxt-i18n-micro: pages, plurals, Intl-compatible formats, no runtime dependencies
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
//! `Intl.NumberFormat` options of DESIGN 5.3, read from `numberFormats` (the Vue I18n shape
//! of `i18n.json` and [`Builder::formats`](crate::Builder::formats)) and resolved once, the
//! way ECMA-402 `SetNumberFormatDigitOptions` resolves them: currency digits, defaults per
//! style, `minimumFractionDigits` > `maximumFractionDigits` refused. An option outside the
//! subset, or a value `Intl` would throw on, is [`Error::UnsupportedFormat`] at `build`:
//! never a quiet difference from the front end. Unknown keys are ignored, as `Intl` ignores
//! them. Options at their default value (`notation:
//! "standard"`, `signDisplay: "auto"`, `numberingSystem` of the locale…) change nothing and are
//! accepted. Values are read as `Intl` reads them: `ToNumber` for digits (`"0x2"`, `null` → 0,
//! `[2]`), `ToString` for strings, `ToBoolean` first for `useGrouping` (`0`, `""` → off); a key
//! given twice takes its last value.

use super::data::{self, Loc};
use super::date::{Cycle, DateOptions, NO_STYLE, Shape, zone_named};
use super::number::currency_digits;
use crate::catalog::is_js_space;
use crate::error::Error;
use crate::hash::FastMap;
use crate::json::{Event, Reader, push_js_number};
use std::borrow::Cow;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Style {
    Decimal,
    Percent,
    Currency,
}

/// `currencyDisplay`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Display {
    Symbol,
    Narrow,
    Code,
    /// The plural display name (`{0} {1}` of the locale); feature `currency-names`.
    #[cfg_attr(not(feature = "currency-names"), allow(dead_code))]
    Name,
}

/// `useGrouping`: `"auto"` (the locale's `minimumGroupingDigits`), `"min2"`, `"always"`
/// (`true`), off (`false`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Grouping {
    Auto,
    Min2,
    Always,
    Off,
}

/// Resolved options: what the formatter reads.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct NumberOptions {
    pub(crate) style: Style,
    pub(crate) currency: [u8; 3],
    pub(crate) display: Display,
    pub(crate) min_frac: u8,
    pub(crate) max_frac: u8,
    pub(crate) min_int: u8,
    pub(crate) grouping: Grouping,
    pub(crate) compact: bool,
    /// Compact with fraction digits given: rounds by them, else to two significant digits.
    pub(crate) frac_given: bool,
}

impl NumberOptions {
    /// `new Intl.NumberFormat(locale)`.
    pub(crate) const DEFAULT: NumberOptions = NumberOptions {
        style: Style::Decimal,
        currency: *b"XXX",
        display: Display::Symbol,
        min_frac: 0,
        max_frac: 3,
        min_int: 1,
        grouping: Grouping::Auto,
        compact: false,
        frac_given: false,
    };
}

/// One raw option value.
enum Val<'a> {
    Str(Cow<'a, str>),
    Num(f64),
    Bool(bool),
    Null,
    /// An array, as JS `ToString` gives it (`[2]` → `"2"`, `[]` → `""`, `[1, [2]]` → `"1,2"`).
    Arr(String),
    Obj,
}

impl Val<'_> {
    /// ECMAScript `ToString`.
    fn string(&self) -> Cow<'_, str> {
        match self {
            Val::Str(s) => Cow::Borrowed(&**s),
            Val::Num(n) => {
                let mut out = String::new();
                push_js_number(&mut out, *n);
                Cow::Owned(out)
            }
            Val::Bool(b) => Cow::Borrowed(if *b { "true" } else { "false" }),
            Val::Null => Cow::Borrowed("null"),
            Val::Arr(s) => Cow::Borrowed(s),
            Val::Obj => Cow::Borrowed("[object Object]"),
        }
    }

    /// ECMAScript `ToNumber`.
    fn number(&self) -> f64 {
        match self {
            Val::Num(n) => *n,
            Val::Str(s) => js_number(s),
            Val::Arr(s) => js_number(s),
            Val::Bool(b) => f64::from(u8::from(*b)),
            Val::Null => 0.0,
            Val::Obj => f64::NAN,
        }
    }

    /// ECMAScript `ToBoolean`.
    fn truthy(&self) -> bool {
        match self {
            Val::Str(s) => !s.is_empty(),
            Val::Num(n) => *n != 0.0 && !n.is_nan(),
            Val::Bool(b) => *b,
            Val::Null => false,
            Val::Arr(_) | Val::Obj => true,
        }
    }
}

/// ECMAScript `StringToNumber`: JS white space trimmed, empty → 0, `Infinity`, decimal
/// literals (`.5`, `5.`, `1e0`, signed), `0x` / `0o` / `0b` integers (unsigned); else `NaN`.
fn js_number(s: &str) -> f64 {
    let t = s.trim_matches(is_js_space);
    if t.is_empty() {
        return 0.0;
    }
    let radix = match t.get(..2) {
        Some("0x" | "0X") => 16,
        Some("0o" | "0O") => 8,
        Some("0b" | "0B") => 2,
        _ => 10,
    };
    if radix != 10 {
        let digits = t.get(2..).unwrap_or("");
        if digits.is_empty() {
            return f64::NAN;
        }
        let mut v = 0.0;
        for c in digits.chars() {
            match c.to_digit(radix) {
                Some(d) => v = v * f64::from(radix) + f64::from(d),
                None => return f64::NAN,
            }
        }
        return v;
    }
    let body = t.strip_prefix(['+', '-']).unwrap_or(t);
    if body == "Infinity" {
        return if t.starts_with('-') {
            f64::NEG_INFINITY
        } else {
            f64::INFINITY
        };
    }
    // Rust also reads `inf` and `nan`: only digits, `.` and an exponent go to it
    if !body.starts_with(|c: char| c.is_ascii_digit() || c == '.')
        || !body
            .bytes()
            .all(|b| b.is_ascii_digit() || matches!(b, b'.' | b'e' | b'E' | b'+' | b'-'))
    {
        return f64::NAN;
    }
    t.parse().unwrap_or(f64::NAN)
}

/// An array's `ToString` from the reader, after its `[`.
fn array_string<'a>(r: &mut Reader<'a>, path: &str) -> Result<String, Error> {
    let mut out = String::new();
    let mut first = true;
    loop {
        let e = next_event(r, path)?;
        if matches!(e, Event::ArrayEnd) {
            return Ok(out);
        }
        if !first {
            out.push(',');
        }
        first = false;
        match e {
            Event::Str(s) => out.push_str(&s),
            Event::Num(n) => push_js_number(&mut out, n.parse().unwrap_or(f64::NAN)),
            Event::Bool(b) => out.push_str(if b { "true" } else { "false" }),
            Event::ArrayStart => out.push_str(&array_string(r, path)?),
            Event::ObjectStart => {
                skip_object(r, path)?;
                out.push_str("[object Object]");
            }
            // `null` (and `undefined`) join as nothing
            _ => {}
        }
    }
}

/// Skips the rest of an object, after its `{`.
fn skip_object(r: &mut Reader<'_>, path: &str) -> Result<(), Error> {
    let mut depth = 1;
    while depth > 0 {
        match next_event(r, path)? {
            Event::ObjectStart | Event::ArrayStart => depth += 1,
            Event::ObjectEnd | Event::ArrayEnd => depth -= 1,
            _ => {}
        }
    }
    Ok(())
}

/// Named formats by locale code, then name.
pub(crate) type Formats = FastMap<Box<str>, FastMap<Box<str>, NumberOptions>>;

/// Named date formats by locale code, then name (`datetimeFormats`).
pub(crate) type DateFormats = FastMap<Box<str>, FastMap<Box<str>, DateOptions>>;

/// A `numberingSystem` as given (lowercase, zero-padded), by locale code and format name:
/// accepted only when it is the locale's default, checked once the locales' `iso` are known
/// ([`check_numbering`]). Kept out of [`NumberOptions`], which every format call reads.
pub(crate) type Numbering = Vec<(Box<str>, Box<str>, [u8; 8])>;

/// The raw options of one named format, in order, a repeated key at its last value.
type Raw<'a> = [(Box<str>, Val<'a>)];

/// Reads `{ "<locale>": { "<name>": { <options> } } }` into `into` (a later name wins), the
/// `numberingSystem`s given into `numbering`. `path` names the source in a JSON or shape error.
pub(crate) fn parse_formats(
    json: &str,
    path: &str,
    into: &mut Formats,
    numbering: &mut Numbering,
) -> Result<(), Error> {
    parse_named(json, path, NUMBER_SHAPES, &mut |locale, name, raw| {
        store(locale, name, resolve(raw), into, numbering)
    })
}

/// [`parse_formats`] for `datetimeFormats` (`Intl.DateTimeFormat` options).
pub(crate) fn parse_date_formats(
    json: &str,
    path: &str,
    into: &mut DateFormats,
    numbering: &mut Numbering,
) -> Result<(), Error> {
    parse_named(json, path, DATE_SHAPES, &mut |locale, name, raw| {
        store(locale, name, resolve_date(raw), into, numbering)
    })
}

const NUMBER_SHAPES: [&str; 3] = [
    "numberFormats must be an object",
    "numberFormats.<locale> must be an object",
    "numberFormats.<locale>.<name> must be an object",
];
const DATE_SHAPES: [&str; 3] = [
    "datetimeFormats must be an object",
    "datetimeFormats.<locale> must be an object",
    "datetimeFormats.<locale>.<name> must be an object",
];

/// Keeps one resolved named format (`Err(option)`: the option that is not supported).
fn store<T>(
    locale: &str,
    name: &str,
    resolved: Result<(T, Option<[u8; 8]>), &'static str>,
    into: &mut FastMap<Box<str>, FastMap<Box<str>, T>>,
    numbering: &mut Numbering,
) -> Result<(), Error> {
    let (options, nu) = resolved.map_err(|option| Error::UnsupportedFormat {
        locale: locale.to_owned(),
        name: name.to_owned(),
        option: option.to_owned(),
    })?;
    if name != "__proto__" && locale != "__proto__" {
        numbering.retain(|(l, n, _)| (&**l, &**n) != (locale, name));
        if let Some(nu) = nu {
            numbering.push((Box::from(locale), Box::from(name), nu));
        }
        into.entry(Box::from(locale))
            .or_default()
            .insert(Box::from(name), options);
    }
    Ok(())
}

/// The shape of named formats, each handed to `each` with its raw options: one copy of the
/// reader for number and date formats.
fn parse_named<'a>(
    json: &'a str,
    path: &str,
    shapes: [&'static str; 3],
    each: &mut dyn FnMut(&str, &str, &Raw<'_>) -> Result<(), Error>,
) -> Result<(), Error> {
    let mut r = Reader::new(json.as_bytes());
    let shape = |reason| Error::Config {
        path: path.to_owned(),
        reason,
    };
    let next = |r: &mut Reader<'a>| next_event(r, path);
    if !matches!(next(&mut r)?, Event::ObjectStart) {
        return Err(shape(shapes[0]));
    }
    while let Event::Key(locale) = next(&mut r)? {
        if !matches!(next(&mut r)?, Event::ObjectStart) {
            return Err(shape(shapes[1]));
        }
        while let Event::Key(name) = next(&mut r)? {
            if !matches!(next(&mut r)?, Event::ObjectStart) {
                return Err(shape(shapes[2]));
            }
            let mut raw: Vec<(Box<str>, Val<'_>)> = Vec::new();
            while let Event::Key(key) = next(&mut r)? {
                let v = match next(&mut r)? {
                    Event::Str(s) => Val::Str(s),
                    Event::Num(n) => Val::Num(n.parse().unwrap_or(f64::NAN)),
                    Event::Bool(b) => Val::Bool(b),
                    Event::ArrayStart => Val::Arr(array_string(&mut r, path)?),
                    Event::ObjectStart => {
                        skip_object(&mut r, path)?;
                        Val::Obj
                    }
                    _ => Val::Null,
                };
                // a key given again: its last value, as a JS object has it
                raw.retain(|(k, _)| **k != *key);
                raw.push((Box::from(&*key), v));
            }
            each(&locale, &name, &raw)?;
        }
    }
    match r.next() {
        Ok(_) => Ok(()),
        Err(e) => Err(Error::Json {
            path: path.to_owned(),
            offset: e.offset,
            kind: e.kind.as_str(),
        }),
    }
}

/// Refuses a `numberingSystem` other than the default of its format's locale (`loc`: the
/// data locale of a locale code, by its `iso`): ICU would format with it, this crate formats
/// with the default digits only. The first refused format by locale, then name.
pub(crate) fn check_numbering(
    numbering: &Numbering,
    loc: impl Fn(&str) -> &'static Loc,
) -> Result<(), Error> {
    let mut refused: Option<(&str, &str)> = None;
    for (locale, name, nu) in numbering {
        let given = nu.split(|&b| b == 0).next().unwrap_or(&[]);
        if given == data::numbering(loc(locale).digits).as_bytes() {
            continue;
        }
        if refused.is_none_or(|r| (&**locale, &**name) < r) {
            refused = Some((locale, name));
        }
    }
    match refused {
        Some((locale, name)) => Err(Error::UnsupportedFormat {
            locale: locale.to_owned(),
            name: name.to_owned(),
            option: "numberingSystem".to_owned(),
        }),
        None => Ok(()),
    }
}

/// The next event; the end of input or a syntax error is an error naming `path`.
fn next_event<'a>(r: &mut Reader<'a>, path: &str) -> Result<Event<'a>, Error> {
    match r.next() {
        Ok(Some(e)) => Ok(e),
        Ok(None) => Err(Error::Config {
            path: path.to_owned(),
            reason: "unexpected end",
        }),
        Err(e) => Err(Error::Json {
            path: path.to_owned(),
            offset: e.offset,
            kind: e.kind.as_str(),
        }),
    }
}

/// ECMA-402 `DefaultNumberOption`: `ToNumber` in `lo..=hi` (`NaN` and out of range: `None`),
/// floored.
fn number(v: &Val<'_>, lo: u8, hi: u8) -> Option<u8> {
    let n = v.number();
    (n >= f64::from(lo) && n <= f64::from(hi)).then(|| n.floor() as u8)
}

/// A `numberingSystem` as given: the `type` subtag of a Unicode locale id (3..8
/// alphanumerics, lowercased), else `Intl` throws.
fn numbering_system(nu: Option<&str>) -> Result<Option<[u8; 8]>, &'static str> {
    let Some(nu) = nu else { return Ok(None) };
    let b = nu.as_bytes();
    if !(3..=8).contains(&b.len()) || !b.iter().all(u8::is_ascii_alphanumeric) {
        return Err("numberingSystem");
    }
    let mut given = [0u8; 8];
    for (slot, c) in given.iter_mut().zip(b) {
        *slot = c.to_ascii_lowercase();
    }
    Ok(Some(given))
}

/// The options of one named format; `Err(option)` for the first one that is not supported.
/// With the `numberingSystem` given, for [`check_numbering`].
fn resolve(raw: &Raw<'_>) -> Result<(NumberOptions, Option<[u8; 8]>), &'static str> {
    let get = |k: &str| raw.iter().find(|(key, _)| &**key == k).map(|(_, v)| v);
    let string = |k: &str| get(k).map(Val::string);
    // Options that change nothing at these values are fine; any other value is not supported.
    const DEFAULTS: [(&str, &[&str]); 7] = [
        ("signDisplay", &["auto"]),
        ("currencySign", &["standard"]),
        ("localeMatcher", &["best fit", "lookup"]),
        ("roundingMode", &["halfExpand"]),
        ("roundingPriority", &["auto"]),
        ("trailingZeroDisplay", &["auto"]),
        ("notation", &["standard", "compact"]),
    ];
    for (key, v) in raw {
        let key = &**key;
        if let Some((name, ok)) = DEFAULTS.iter().find(|(k, _)| *k == key) {
            if !ok.contains(&&*v.string()) {
                return Err(name);
            }
            continue;
        }
        match key {
            // `GetNumberOption(…, 1, 5000)`, floored: only 1 rounds as without it
            "roundingIncrement" if (1.0..2.0).contains(&v.number()) => {}
            "roundingIncrement" => return Err("roundingIncrement"),
            "unit" => return Err("unit"),
            "unitDisplay" => return Err("unitDisplay"),
            "minimumSignificantDigits" => return Err("minimumSignificantDigits"),
            "maximumSignificantDigits" => return Err("maximumSignificantDigits"),
            // `Intl` reads only the options it knows; the others are below
            _ => {}
        }
    }
    let mut o = NumberOptions::DEFAULT;
    o.style = match string("style").as_deref() {
        None | Some("decimal") => Style::Decimal,
        Some("percent") => Style::Percent,
        Some("currency") => Style::Currency,
        Some(_) => return Err("style"),
    };
    o.compact = string("notation").as_deref() == Some("compact");
    if o.compact && o.style != Style::Decimal {
        return Err("notation");
    }
    // read whatever the notation (a bad value throws), used only by compact
    match string("compactDisplay").as_deref() {
        None | Some("short") => {}
        Some("long") if !o.compact => {}
        Some(_) => return Err("compactDisplay"),
    }
    let numbering = numbering_system(string("numberingSystem").as_deref())?;
    if let Some(c) = string("currency") {
        let b = c.as_bytes();
        if b.len() != 3 || !b.iter().all(u8::is_ascii_alphabetic) {
            return Err("currency");
        }
        o.currency = [
            b[0].to_ascii_uppercase(),
            b[1].to_ascii_uppercase(),
            b[2].to_ascii_uppercase(),
        ];
    } else if o.style == Style::Currency {
        return Err("currency");
    }
    o.display = match string("currencyDisplay").as_deref() {
        None | Some("symbol") => Display::Symbol,
        Some("narrowSymbol") => Display::Narrow,
        Some("code") => Display::Code,
        #[cfg(feature = "currency-names")]
        Some("name") => Display::Name,
        Some(_) => return Err("currencyDisplay"),
    };
    if let Some(v) = get("minimumIntegerDigits") {
        o.min_int = number(v, 1, 21).ok_or("minimumIntegerDigits")?;
    }
    let (min_default, max_default) = match o.style {
        Style::Decimal => (0, 3),
        Style::Percent => (0, 0),
        Style::Currency => {
            let d = currency_digits(o.currency);
            (d, d)
        }
    };
    let min = get("minimumFractionDigits")
        .map(|v| number(v, 0, 100).ok_or("minimumFractionDigits"))
        .transpose()?;
    let max = get("maximumFractionDigits")
        .map(|v| number(v, 0, 100).ok_or("maximumFractionDigits"))
        .transpose()?;
    o.frac_given = min.is_some() || max.is_some();
    (o.min_frac, o.max_frac) = match (min, max) {
        (None, None) => (min_default, max_default),
        (None, Some(max)) => (min_default.min(max), max),
        (Some(min), None) => (min, max_default.max(min)),
        (Some(min), Some(max)) if min > max => return Err("maximumFractionDigits"),
        (Some(min), Some(max)) => (min, max),
    };
    // `GetBooleanOrStringNumberFormatOption`: `true` → "always", falsy → off, else the
    // string, where "true" / "false" mean the default
    let fallback = if o.compact {
        Grouping::Min2
    } else {
        Grouping::Auto
    };
    o.grouping = match get("useGrouping") {
        None => fallback,
        Some(Val::Bool(true)) => Grouping::Always,
        Some(v) if !v.truthy() => Grouping::Off,
        Some(v) => match &*v.string() {
            "always" => Grouping::Always,
            "auto" => Grouping::Auto,
            "min2" => Grouping::Min2,
            "true" | "false" => fallback,
            _ => return Err("useGrouping"),
        },
    };
    // ICU groups compact numbers by `min2` when asked for `auto`
    if o.compact && o.grouping == Grouping::Auto {
        o.grouping = Grouping::Min2;
    }
    Ok((o, numbering))
}

/// The `Intl.DateTimeFormat` options of one named format (DESIGN 5.3), resolved as
/// ECMA-402 `CreateDateTimeFormat` reads them: each component one of its values (else
/// `RangeError`), styles not mixed with components (`TypeError`), nothing asked for → year,
/// month and day. `timeStyle: full / long`, `timeZoneName`, `era`, `dayPeriod`,
/// `fractionalSecondDigits` and calendars other than `gregory` are not supported; an unknown
/// `timeZone` is refused like JS refuses it.
fn resolve_date(raw: &Raw<'_>) -> Result<(DateOptions, Option<[u8; 8]>), &'static str> {
    let get = |k: &str| raw.iter().find(|(key, _)| &**key == k).map(|(_, v)| v);
    let string = |k: &str| get(k).map(Val::string);
    let pick = |k: &'static str, values: &[&str]| -> Result<u8, &'static str> {
        match string(k) {
            None => Ok(0),
            Some(v) => values
                .iter()
                .position(|x| *x == v)
                .map(|i| i as u8 + 1)
                .ok_or(k),
        }
    };
    for (key, v) in raw {
        match &**key {
            "era" | "timeZoneName" | "dayPeriod" | "fractionalSecondDigits" => {
                return Err(match &**key {
                    "era" => "era",
                    "timeZoneName" => "timeZoneName",
                    "dayPeriod" => "dayPeriod",
                    _ => "fractionalSecondDigits",
                });
            }
            "calendar" if v.string().eq_ignore_ascii_case("gregory") => {}
            "calendar" => return Err("calendar"),
            "localeMatcher" if matches!(&*v.string(), "best fit" | "lookup") => {}
            "localeMatcher" => return Err("localeMatcher"),
            "formatMatcher" if matches!(&*v.string(), "best fit" | "basic") => {}
            "formatMatcher" => return Err("formatMatcher"),
            _ => {}
        }
    }
    let numeric = ["numeric", "2-digit"];
    let weekday = pick("weekday", &["long", "short", "narrow"])?;
    let year = pick("year", &numeric)?;
    let month = pick("month", &["numeric", "2-digit", "long", "short", "narrow"])?;
    let day = pick("day", &numeric)?;
    let hour = pick("hour", &numeric)?;
    let minute = pick("minute", &numeric)?;
    let second = pick("second", &numeric)?;
    let styles = ["full", "long", "medium", "short"];
    let date_style = pick("dateStyle", &styles)?;
    let time_style = pick("timeStyle", &styles)?;
    let hour12 = get("hour12").map(Val::truthy);
    let cycle = match (hour12, pick("hourCycle", &["h11", "h12", "h23", "h24"])?) {
        (Some(h), _) => Cycle::Hour12(h),
        (None, 1) => Cycle::H11,
        (None, 2) => Cycle::H12,
        (None, 3) => Cycle::H23,
        (None, 4) => Cycle::H24,
        _ => Cycle::Locale,
    };
    let zone = match string("timeZone") {
        Some(name) => Some(zone_named(&name).ok_or("timeZone")?),
        None => None,
    };
    let components = weekday | year | month | day | hour | minute | second != 0;
    let shape = if date_style != 0 || time_style != 0 {
        if components {
            return Err(if date_style != 0 {
                "dateStyle"
            } else {
                "timeStyle"
            });
        }
        // full and long times show the zone's name
        if matches!(time_style, 1 | 2) {
            return Err("timeStyle");
        }
        Shape::Styles {
            date: date_style.checked_sub(1).unwrap_or(NO_STYLE),
            time: time_style.checked_sub(1).unwrap_or(NO_STYLE),
        }
    } else if components {
        Shape::Components {
            date: ((year * 6 + month) * 3 + day) * 4 + weekday,
            time: (hour * 3 + minute) * 3 + second,
        }
    } else {
        DateOptions::DEFAULT.shape
    };
    let numbering = numbering_system(string("numberingSystem").as_deref())?;
    Ok((DateOptions { shape, cycle, zone }, numbering))
}