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
//! `Intl.DateTimeFormat` (DESIGN 5.3, 5.4, spikes S9 / S10): the pattern of every supported
//! option combination was taken from Node's ICU by `scripts/cldr.mjs` and sits in the data
//! (`build.rs` compiled it: `\x01`, the CLDR letter, the width digit per field); this module
//! only picks the pattern and renders it. A date and a time are joined by the locale's glue
//! of the date part's width, unless ICU joins that pair otherwise (an override).
//!
//! The instant is split in the zone (`tz`) as proleptic Gregorian, as V8 sets up ICU; years
//! show as years of the era (2 BC for -1). U+202F of ICU is a space here, as in V8's
//! `format()`.

use super::data::{
    self, DATE_B, DATE_NAMES, DATE_OVER, DATE_OVER_KEYS, DATE_PATS, DATE_TIMES, Loc,
};
use std::fmt;

/// An instant for [`I18n::td`](crate::I18n::td) and [`I18n::tdr`](crate::I18n::tdr), held
/// as a JS `Date` holds it: whole milliseconds since 1970-01-01 UTC, within ±8.64e15
/// (±100 000 000 days). Anything else is an invalid date: it formats as `Invalid Date`.
///
/// From `i64` (milliseconds), `f64` (milliseconds, truncated like `new Date(ms)`; `NaN`
/// is invalid), off `wasm32-unknown-unknown` `SystemTime`; with feature `time`
/// `OffsetDateTime`, `UtcDateTime` and `PrimitiveDateTime` (as UTC). Sub-millisecond
/// parts are cut toward zero, as for `SystemTime`.
///
/// ```
/// use i18n_next::Timestamp;
///
/// assert_eq!(Timestamp::from(1.9), Timestamp::from(1i64));
/// assert!(!Timestamp::from(f64::NAN).is_valid());
/// assert!(!Timestamp::from(8_640_000_000_000_001i64).is_valid());
/// assert_eq!(Timestamp::from(1_700_000_000_000i64).millis(), Some(1_700_000_000_000));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Timestamp(Option<i64>);

/// ECMAScript `TimeClip`: ±8.64e15 ms.
const MAX_MS: i64 = 8_640_000_000_000_000;

impl Timestamp {
    /// The milliseconds, `None` for an invalid date.
    #[must_use]
    pub fn millis(self) -> Option<i64> {
        self.0
    }

    /// Whether the date is valid (JS `!isNaN(date)`).
    #[must_use]
    pub fn is_valid(self) -> bool {
        self.0.is_some()
    }
}

impl From<i64> for Timestamp {
    fn from(ms: i64) -> Self {
        Timestamp((-MAX_MS..=MAX_MS).contains(&ms).then_some(ms))
    }
}

impl From<f64> for Timestamp {
    #[allow(
        clippy::cast_possible_truncation,
        reason = "the range is checked first"
    )]
    fn from(ms: f64) -> Self {
        // `TimeClip`: NaN and out of range are invalid, the rest truncated toward zero
        if ms.abs() <= MAX_MS as f64 {
            Timestamp(Some(ms.trunc() as i64))
        } else {
            Timestamp(None)
        }
    }
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl From<std::time::SystemTime> for Timestamp {
    fn from(t: std::time::SystemTime) -> Self {
        let ms = match t.duration_since(std::time::UNIX_EPOCH) {
            Ok(d) => i64::try_from(d.as_millis()).ok(),
            // before 1970: whole milliseconds toward zero, as `Date` reads them
            Err(e) => i64::try_from(e.duration().as_millis()).ok().map(|m| -m),
        };
        ms.map_or(Timestamp(None), Timestamp::from)
    }
}

/// Whole milliseconds toward zero of nanoseconds since 1970 (as `From<SystemTime>`).
#[cfg(feature = "time")]
fn from_nanos(ns: i128) -> Timestamp {
    i64::try_from(ns / 1_000_000).map_or(Timestamp(None), Timestamp::from)
}

/// The instant of a `time` date-time with an offset (feature `time`).
#[cfg(feature = "time")]
impl From<time::OffsetDateTime> for Timestamp {
    fn from(t: time::OffsetDateTime) -> Self {
        from_nanos(t.unix_timestamp_nanos())
    }
}

/// The instant of a `time` UTC date-time (feature `time`).
#[cfg(feature = "time")]
impl From<time::UtcDateTime> for Timestamp {
    fn from(t: time::UtcDateTime) -> Self {
        from_nanos(t.unix_timestamp_nanos())
    }
}

/// A `time` date-time without an offset, read as UTC (feature `time`).
#[cfg(feature = "time")]
impl From<time::PrimitiveDateTime> for Timestamp {
    fn from(t: time::PrimitiveDateTime) -> Self {
        Timestamp::from(t.assume_utc())
    }
}

/// The time zone of a format: with the `tz` feature the crate's own (IANA through the
/// platform, `DESIGN 5.4`); without it only UTC.
#[cfg(feature = "tz")]
pub(crate) use crate::tz::Zone;

#[cfg(feature = "tz")]
use crate::tz::{Civil, civil_from_days, civil_from_unix, days_from_civil};

/// UTC alone (the `tz` feature is off).
#[cfg(not(feature = "tz"))]
#[derive(Debug, Clone)]
pub(crate) struct Zone;

#[cfg(not(feature = "tz"))]
impl Zone {
    pub(crate) fn utc() -> Zone {
        Zone
    }

    pub(crate) fn system() -> Zone {
        Zone
    }

    #[allow(clippy::unused_self, reason = "the `tz` zone's signature")]
    pub(crate) fn offset_at(&self, _: i64) -> i32 {
        0
    }

    pub(crate) fn named(_: &str) -> Option<Zone> {
        None
    }
}

// The calendar arithmetic of `tz`, for a build without it.
#[cfg(not(feature = "tz"))]
#[path = "../tz/civil.rs"]
mod civil;
#[cfg(not(feature = "tz"))]
use civil::{Civil, civil_from_days, civil_from_unix, days_from_civil};

/// A zone by its IANA name as `Intl` takes it (`UTC`, `Etc/UTC`, `GMT` in any case are UTC),
/// `None` when the platform has no such zone (JS throws a `RangeError`).
pub(crate) fn zone_named(name: &str) -> Option<Zone> {
    const UTC: [&str; 6] = ["UTC", "Etc/UTC", "GMT", "Etc/GMT", "Etc/UCT", "UCT"];
    if UTC.iter().any(|u| u.eq_ignore_ascii_case(name)) {
        return Some(Zone::utc());
    }
    Zone::named(name)
}

/// The ISO week year (`Y`): the year of the Thursday of the date's week. Out of line: only
/// `gd`'s patterns read it, and inlined LLVM hoists its arithmetic into every render.
#[inline(never)]
fn week_year(c: &Civil) -> i64 {
    let days = days_from_civil(c.year, c.month, c.day);
    let monday_based = (i64::from(c.weekday) + 6) % 7;
    civil_from_days(days - monday_based + 3).0
}

/// The hour cycle asked for: none (the locale's, table `j`), one of `h11` `h12` `h23` `h24`
/// (tables `K` `h` `H` `k`), or `hour12` (the locale's 12- or 24-hour cycle).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Cycle {
    Locale,
    H11,
    H12,
    H23,
    H24,
    Hour12(bool),
}

/// What a date format shows: components (a date combination `0..216`, a time combination
/// `0..27`, as `scripts/cldr.mjs` numbers them) or styles (`0..4`: full, long, medium,
/// short; [`NO_STYLE`] for none; a time style is medium or short).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Shape {
    Components { date: u8, time: u8 },
    Styles { date: u8, time: u8 },
}

pub(crate) const NO_STYLE: u8 = u8::MAX;

/// Resolved `Intl.DateTimeFormat` options.
#[derive(Debug, Clone)]
pub(crate) struct DateOptions {
    pub(crate) shape: Shape,
    pub(crate) cycle: Cycle,
    /// The format's own `timeZone`; else the instance's.
    pub(crate) zone: Option<Zone>,
}

impl DateOptions {
    /// `new Intl.DateTimeFormat(locale)`: year, month and day, numeric.
    pub(crate) const DEFAULT: DateOptions = DateOptions {
        shape: Shape::Components {
            date: DATE_NUMERIC,
            time: 0,
        },
        cycle: Cycle::Locale,
        zone: None,
    };
}

/// Year, month and day numeric: `((1 * 6 + 1) * 3 + 1) * 4`.
const DATE_NUMERIC: u8 = 88;

// Slots of the date row and the time row (`build.rs` `pattern_slots`).
const DATE_STYLES: usize = 216;
const GLUE: usize = 220;
const STYLE_GLUE: usize = 224;
const HOUR_TABLES: usize = 9;
const TIME_STYLES: usize = 99;

/// The glue of a date combination (ICU's pattern generator): a long month with a weekday is
/// full, a long month long, a short one medium, the rest short.
fn glue_width(date: u8) -> usize {
    match date / 12 % 6 {
        3 if !date.is_multiple_of(4) => 0,
        3 => 1,
        4 => 2,
        _ => 3,
    }
}

/// The hour table of a cycle: 0 `j`, 1 `K`, 2 `h`, 3 `H`, 4 `k`.
fn table(cycle: Cycle, loc: &Loc) -> usize {
    let letter = match cycle {
        Cycle::Locale => return 0,
        Cycle::H11 => b'K',
        Cycle::H12 => b'h',
        Cycle::H23 => b'H',
        Cycle::H24 => b'k',
        Cycle::Hour12(true) => loc.hc12,
        Cycle::Hour12(false) => loc.hc24,
    };
    match letter {
        b'K' => 1,
        b'h' => 2,
        b'H' => 3,
        _ => 4,
    }
}

/// The override of `(kind, a, b, table)` among the locale's, as `build.rs` keys them.
fn override_of(loc: &Loc, kind: u32, a: u8, b: u8, mode: usize) -> Option<&'static str> {
    let key = (kind << 20) | (u32::from(a) << 12) | (u32::from(b) << 4) | mode as u32;
    let start = loc.dover as usize;
    let keys = DATE_OVER_KEYS.get(start..start + usize::from(loc.dover_len))?;
    let i = keys.binary_search(&key).ok()?;
    DATE_OVER.get(start + i).map(|&p| data::dpat(p))
}

/// What to render: one pattern, or a glue with its date and time parts.
struct Picked {
    pattern: &'static str,
    date: &'static str,
    time: &'static str,
}

fn pick(o: &DateOptions, loc: &Loc) -> Picked {
    let row = DATE_PATS.get(usize::from(loc.dpats));
    let times = DATE_TIMES.get(usize::from(loc.dtimes));
    let p = |slot: usize| data::dpat(row.and_then(|r| r.get(slot)).copied().unwrap_or(0));
    let t = |slot: usize| data::dpat(times.and_then(|r| r.get(slot)).copied().unwrap_or(0));
    let mode = table(o.cycle, loc);
    let one = |pattern| Picked {
        pattern,
        date: "",
        time: "",
    };
    match o.shape {
        Shape::Components { date, time } => {
            let time_pattern = || match time {
                1..9 => t(usize::from(time)),
                _ => t(HOUR_TABLES + mode * 18 + usize::from(time) - 9),
            };
            match (date, time) {
                (_, 0) => one(p(usize::from(date))),
                (0, _) => one(time_pattern()),
                _ => {
                    let m = if time < 9 { 0 } else { mode };
                    match override_of(loc, 0, date, time, m) {
                        Some(full) => one(full),
                        None => Picked {
                            pattern: p(GLUE + glue_width(date)),
                            date: p(usize::from(date)),
                            time: time_pattern(),
                        },
                    }
                }
            }
        }
        Shape::Styles { date, time } => {
            let time_pattern = || t(TIME_STYLES + mode * 2 + usize::from(time.saturating_sub(2)));
            match (date, time) {
                (_, NO_STYLE) => one(p(DATE_STYLES + usize::from(date))),
                (NO_STYLE, _) => one(time_pattern()),
                _ => match override_of(loc, 1, date, time, mode) {
                    Some(full) => one(full),
                    None => Picked {
                        pattern: p(STYLE_GLUE + usize::from(date)),
                        date: p(DATE_STYLES + usize::from(date)),
                        time: time_pattern(),
                    },
                },
            }
        }
    }
}

/// What fields read besides the instant.
struct Cx {
    names: &'static [u16],
    digits: [&'static str; 10],
    /// The digits are `0`–`9` (`digits` holds them too).
    latin: bool,
    loc: &'static Loc,
}

/// Formats `t` (in `zone`, unless the options have their own) into `out`; an invalid date
/// writes `Invalid Date`, as `FormatService.formatDate`.
pub(crate) fn write(
    t: Timestamp,
    o: &DateOptions,
    loc: &'static Loc,
    zone: &Zone,
    out: &mut dyn fmt::Write,
) -> fmt::Result {
    let Some(ms) = t.0 else {
        return out.write_str("Invalid Date");
    };
    let zone = o.zone.as_ref().unwrap_or(zone);
    let secs = ms.div_euclid(1000);
    let c = civil_from_unix(secs + i64::from(zone.offset_at(secs)));
    let cx = Cx {
        names: DATE_NAMES
            .get(usize::from(loc.dnames))
            .map_or(&[][..], |r| &r[..]),
        digits: super::number::digit_table(loc),
        latin: data::s(loc.digits).is_empty(),
        loc,
    };
    let picked = pick(o, loc);
    render(picked.pattern, &c, &cx, (picked.date, picked.time), out)
}

/// Writes a compiled pattern; a glue's `{1}` is `parts.0`, `{0}` `parts.1`.
fn render(
    p: &str,
    c: &Civil,
    cx: &Cx,
    parts: (&str, &str),
    out: &mut dyn fmt::Write,
) -> fmt::Result {
    let b = p.as_bytes();
    let mut from = 0;
    let mut i = 0;
    while i < b.len() {
        if b.get(i) != Some(&1) {
            i += 1;
            continue;
        }
        if from < i {
            out.write_str(p.get(from..i).unwrap_or(""))?;
        }
        let letter = b.get(i + 1).copied().unwrap_or(0);
        let width = b.get(i + 2).map_or(1, |w| w.wrapping_sub(b'0'));
        i += 3;
        from = i;
        match (letter, width) {
            (b'{', 1) => render(parts.0, c, cx, ("", ""), out)?,
            (b'{', _) => render(parts.1, c, cx, ("", ""), out)?,
            _ => field(letter, width, p, c, cx, out)?,
        }
    }
    match p.get(from..) {
        Some(rest) if !rest.is_empty() => out.write_str(rest),
        _ => Ok(()),
    }
}

/// `00`, `01`, … `99`: two Latin digits per write.
static PAIRS: &str = match std::str::from_utf8(&PAIR_BYTES) {
    Ok(s) => s,
    Err(_) => "",
};
const PAIR_BYTES: [u8; 200] = {
    let mut a = [0u8; 200];
    let mut i = 0;
    while i < 100 {
        a[2 * i] = b'0' + (i / 10) as u8;
        a[2 * i + 1] = b'0' + (i % 10) as u8;
        i += 1;
    }
    a
};

/// Digits of `v`, at least `width` of them, in the locale's digits.
fn number(v: u64, width: u8, cx: &Cx, out: &mut dyn fmt::Write) -> fmt::Result {
    if cx.latin && v < 10_000 && width <= 4 {
        // every calendar field but a long year: at most two writes of `PAIRS`
        let v = v as usize;
        let len = match v {
            0..10 => 1,
            10..100 => 2,
            100..1000 => 3,
            _ => 4,
        }
        .max(usize::from(width));
        let pair = |n: usize| PAIRS.get(2 * n..2 * n + 2).unwrap_or("00");
        let (hi, lo) = (pair(v / 100), pair(v % 100));
        if len > 2 {
            out.write_str(if len == 3 {
                hi.get(1..).unwrap_or("")
            } else {
                hi
            })?;
        }
        return out.write_str(if len == 1 {
            lo.get(1..).unwrap_or("")
        } else {
            lo
        });
    }
    let mut buf = [0u8; 20];
    let mut n = v;
    let mut len = 0;
    while (n > 0 || len < usize::from(width.max(1))) && len < buf.len() {
        if let Some(slot) = buf.get_mut(len) {
            *slot = (n % 10) as u8;
        }
        n /= 10;
        len += 1;
    }
    for d in buf.get(..len).unwrap_or(&[]).iter().rev() {
        out.write_str(cx.digits.get(usize::from(*d)).copied().unwrap_or("0"))?;
    }
    Ok(())
}

/// A name of the row: list `set` (`build.rs` `NAME_SETS` order), entry `i`.
fn name(cx: &Cx, set: usize, i: usize) -> &'static str {
    // months: 6 lists of 12; weekdays: 8 of 7 from 72; AM / PM and eras: 6 of 2 from 128
    let at = match set {
        0..6 => set * 12 + i,
        6..14 => 72 + (set - 6) * 7 + i,
        _ => 128 + (set - 14) * 2 + i,
    };
    data::dname(cx.names.get(at).copied().unwrap_or(0))
}

/// Width letters to a list: 3 abbreviated, 4 wide, 5 narrow, 6 short.
fn width_index(width: u8) -> usize {
    match width {
        4 => 1,
        5 => 2,
        6 => 3,
        _ => 0,
    }
}

fn field(
    letter: u8,
    width: u8,
    p: &str,
    c: &Civil,
    cx: &Cx,
    out: &mut dyn fmt::Write,
) -> fmt::Result {
    match letter {
        b'y' => {
            // the year of the era: 2 BC for -1
            let y = if c.year > 0 { c.year } else { 1 - c.year }.unsigned_abs();
            if width == 2 {
                number(y % 100, 2, cx, out)
            } else {
                number(y, width, cx, out)
            }
        }
        b'Y' => {
            let y = week_year(c);
            if y < 0 {
                out.write_str(data::s(
                    data::PATTERNS
                        .get(usize::from(cx.loc.decimal_pat))
                        .map_or(0, |pat| pat.np),
                ))?;
            }
            let a = y.unsigned_abs();
            number(if width == 2 { a % 100 } else { a }, width, cx, out)
        }
        b'M' | b'L' if width <= 2 => number(u64::from(c.month), width, cx, out),
        b'M' | b'L' => {
            let set = usize::from(letter == b'L') * 3 + width_index(width).min(2);
            out.write_str(name(cx, set, usize::from(c.month).saturating_sub(1)))
        }
        b'r' => {
            // not CLDR's `r`: a numeric month in lowercase Roman numerals (`haw`)
            const ROMAN: [&str; 12] = [
                "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii",
            ];
            out.write_str(
                ROMAN
                    .get(usize::from(c.month).saturating_sub(1))
                    .copied()
                    .unwrap_or(""),
            )
        }
        b'd' => number(u64::from(c.day), width, cx, out),
        b'E' | b'c' => {
            let set = 6 + usize::from(letter == b'c') * 4 + width_index(width);
            out.write_str(name(cx, set, usize::from(c.weekday)))
        }
        b'h' => number(
            u64::from(match c.hour % 12 {
                0 => 12,
                h => h,
            }),
            width,
            cx,
            out,
        ),
        b'H' => number(u64::from(c.hour), width, cx, out),
        b'K' => number(u64::from(c.hour % 12), width, cx, out),
        b'k' => number(
            u64::from(if c.hour == 0 { 24 } else { c.hour }),
            width,
            cx,
            out,
        ),
        b'm' => number(u64::from(c.minute), width, cx, out),
        b's' => number(u64::from(c.second), width, cx, out),
        b'a' => out.write_str(name(
            cx,
            14 + width_index(width).min(2),
            usize::from(c.hour >= 12),
        )),
        b'B' => out.write_str(flexible(p, c, cx, width)),
        b'G' => out.write_str(name(
            cx,
            17 + width_index(width).min(2),
            usize::from(c.year > 0),
        )),
        _ => Ok(()),
    }
}

/// A flexible day period (`B`, ICU): `noon` at 12:00 exactly (minutes and seconds the pattern
/// shows are zero) when the locale has one, else the period of the hour.
fn flexible(p: &str, c: &Civil, cx: &Cx, width: u8) -> &'static str {
    let Some(row) = DATE_B.get(usize::from(cx.loc.db)) else {
        return name(cx, 14, usize::from(c.hour >= 12));
    };
    let base = width_index(width).min(2) * 25;
    let shows = |l: u8| p.as_bytes().windows(2).any(|w| w == [1, l]);
    let noon = row.get(base + 24).copied().unwrap_or(0);
    let exact = (!shows(b'm') || c.minute == 0) && (!shows(b's') || c.second == 0);
    let i = if c.hour == 12 && noon != 0 && exact {
        noon
    } else {
        row.get(base + usize::from(c.hour)).copied().unwrap_or(0)
    };
    data::dname(i)
}