mf2 2.0.0

Unicode MessageFormat 2 for Rust, for Leptos web applications and native CLI and terminal applications: the one crate an application names — the call-site types, the Leptos layer, the native application support and its Ratatui text behind features, the formatter, and compile_str for ad-hoc messages.
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
//! The owned call-site argument value: what `tr!` builds, borrowed into the
//! runtime's [`Arg`] at format time.
//!
//! Client-path code: no `core::fmt`, no panicking operation, no allocation
//! that is not the value's own.

use alloc::boxed::Box;
use alloc::string::String;
use alloc::sync::Arc;

use mf2_runtime::{Arg, CustomValue, Date, DateTime, Time};

/// Text a call site owns: a literal costs nothing, anything else is counted,
/// so cloning a description — which every re-format after a locale change
/// does — never copies the text.
#[derive(Clone)]
pub enum Text {
    /// A `&'static str`: the program's own text.
    Static(&'static str),
    /// Counted text, shared by every clone.
    Shared(Arc<str>),
}

/// The text itself, quoted: whether it is shared is not what a reader of
/// `{:?}` wants to know.
impl core::fmt::Debug for Text {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        crate::debug::quoted(f, self.as_str())
    }
}

impl Text {
    /// The text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Text::Static(s) => s,
            Text::Shared(s) => s,
        }
    }
}

impl From<&'static str> for Text {
    fn from(s: &'static str) -> Text {
        Text::Static(s)
    }
}

impl From<String> for Text {
    fn from(s: String) -> Text {
        Text::Shared(Arc::from(s))
    }
}

impl From<Arc<str>> for Text {
    fn from(s: Arc<str>) -> Text {
        Text::Shared(s)
    }
}

/// A value read at format time — the extension point for a reactive
/// argument.
///
/// The Leptos layer implements it for a wrapped `Signal<T>`: [`arg_value`] is
/// called inside whatever reactive context is formatting, which is exactly
/// where a signal has to be read for the node to subscribe to it. The
/// runtime never sees a signal — it sees what this returned.
///
/// [`arg_value`]: ArgSource::arg_value
pub trait ArgSource: Send + Sync {
    /// Its value now. Called once per format.
    fn arg_value(&self) -> ArgValue;
}

/// A date/time a call site owns: [`Arg::DateTime`] with its zone and
/// calendar owned.
#[derive(Clone)]
pub struct DateTimeValue {
    date: Date,
    time: Time,
    offset: Option<i32>,
    zone: Option<Text>,
    calendar: Option<Text>,
}

impl DateTimeValue {
    /// The instant `epoch_ms` milliseconds after 1970-01-01T00:00:00Z;
    /// `None` past the year limit of [`Date`].
    #[must_use]
    pub fn instant(epoch_ms: i64) -> Option<DateTimeValue> {
        DateTime::from_epoch_ms(epoch_ms).map(DateTimeValue::from)
    }

    /// A floating date and time: no offset, so the formatting time zone
    /// applies without conversion.
    #[must_use]
    pub fn floating(date: Date, time: Time) -> DateTimeValue {
        DateTimeValue {
            date,
            time,
            offset: None,
            zone: None,
            calendar: None,
        }
    }

    /// The wall time `date` `time` in the IANA zone `zone` — 09:00 in
    /// Paris, whatever instant that is. A date/time function finds its
    /// offset through the host's zone data, and converts it from there to
    /// the zone it shows.
    #[must_use]
    pub fn wall_time(date: Date, time: Time, zone: impl Into<Text>) -> DateTimeValue {
        DateTimeValue::floating(date, time).with_zone(zone)
    }

    /// The same instant, in the IANA zone `zone`: `timeZone=input` shows it
    /// at that zone's wall time, which a date/time function works out
    /// through the host's zone data. A floating value has no instant to
    /// convert: its wall time is placed in `zone`, as
    /// [`DateTimeValue::wall_time`] does.
    #[must_use]
    pub fn with_zone(mut self, zone: impl Into<Text>) -> DateTimeValue {
        self.zone = Some(zone.into());
        self
    }

    /// The same value with a `calendar` override (a Unicode calendar
    /// identifier), which travels with it into the date/time functions.
    #[must_use]
    pub fn with_calendar(mut self, calendar: impl Into<Text>) -> DateTimeValue {
        self.calendar = Some(calendar.into());
        self
    }

    /// The runtime's borrowed form.
    pub(crate) fn borrow(&self) -> DateTime<'_> {
        let mut dt = DateTime::floating(self.date, self.time);
        if let Some(seconds) = self.offset
            && let Some(with) = dt.with_offset(seconds)
        {
            dt = with;
        }
        if let Some(zone) = &self.zone {
            dt = dt.in_zone(zone.as_str());
        }
        if let Some(calendar) = &self.calendar {
            dt.options.calendar = Some(calendar.as_str());
        }
        dt
    }
}

/// `DateTimeValue(2026-01-01T00:00:00.000)`, then the offset in seconds,
/// the zone and the calendar when there are any.
impl core::fmt::Debug for DateTimeValue {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        use crate::debug::{fixed, int};
        f.write_str("DateTimeValue(")?;
        int(f, i64::from(self.date.year()))?;
        f.write_str("-")?;
        fixed(f, u64::from(self.date.month()), 2)?;
        f.write_str("-")?;
        fixed(f, u64::from(self.date.day()), 2)?;
        f.write_str("T")?;
        fixed(f, u64::from(self.time.hour()), 2)?;
        f.write_str(":")?;
        fixed(f, u64::from(self.time.minute()), 2)?;
        f.write_str(":")?;
        fixed(f, u64::from(self.time.second()), 2)?;
        f.write_str(".")?;
        fixed(f, u64::from(self.time.millisecond()), 3)?;
        if let Some(offset) = self.offset {
            f.write_str(" offset ")?;
            int(f, i64::from(offset))?;
        }
        if let Some(zone) = &self.zone {
            f.write_str(" ")?;
            f.write_str(zone.as_str())?;
        }
        if let Some(calendar) = &self.calendar {
            f.write_str(" u-ca=")?;
            f.write_str(calendar.as_str())?;
        }
        f.write_str(")")
    }
}

impl From<DateTime<'_>> for DateTimeValue {
    fn from(d: DateTime<'_>) -> DateTimeValue {
        DateTimeValue {
            date: d.date,
            time: d.time,
            offset: d.offset,
            zone: d.zone.map(|z| Text::Shared(Arc::from(z))),
            calendar: d.options.calendar.map(|c| Text::Shared(Arc::from(c))),
        }
    }
}

/// One positional argument of a call site: owned, `'static` and
/// `Send + Sync`, so a description can sit in a `const`, cross a thread, or
/// wait in a registry until something renders it.
///
/// Every variant is borrowed into one [`Arg`].
#[derive(Clone)]
#[non_exhaustive]
pub enum ArgValue {
    /// A string.
    Str(Text),
    /// An integer.
    Int(i64),
    /// A float.
    Float(f64),
    /// An exact decimal as `number-literal` text (`-12.50`, `1e3`).
    Decimal(Text),
    /// A date/time.
    DateTime(Arc<DateTimeValue>),
    /// An application value: what carries a measure, a date or a value a
    /// custom function downcasts ([`CustomValue`]).
    Custom(Arc<dyn CustomValue + Send + Sync>),
    /// A value read at format time ([`ArgSource`]).
    Source(Arc<dyn ArgSource>),
    /// No value: the message reports an Unresolved Variable, as the spec
    /// requires.
    Unset,
}

impl ArgValue {
    /// A `&'static str` argument without an allocation — what the macro
    /// emits for a string literal at a call site.
    #[must_use]
    #[doc(hidden)]
    pub const fn str_static(s: &'static str) -> ArgValue {
        ArgValue::Str(Text::Static(s))
    }

    /// An exact decimal from its `number-literal` text. The runtime formats
    /// and selects on the digits as written, with no float in between.
    #[must_use]
    pub fn decimal(text: impl Into<Text>) -> ArgValue {
        ArgValue::Decimal(text.into())
    }

    /// An application value.
    #[must_use]
    pub fn custom<C: CustomValue + Send + Sync + 'static>(value: C) -> ArgValue {
        ArgValue::Custom(Arc::new(value))
    }

    /// A value read at format time ([`ArgSource`]).
    #[must_use]
    pub fn source<S: ArgSource + 'static>(source: S) -> ArgValue {
        ArgValue::Source(Arc::new(source))
    }

    /// Itself, unless it is an [`ArgValue::Source`]: then what the source
    /// gives now, resolved until it is not a source (bounded, so a source
    /// that returns itself cannot loop).
    pub(crate) fn resolve(&self) -> ArgValue {
        let mut value = self.clone();
        for _ in 0..4 {
            let ArgValue::Source(source) = &value else {
                return value;
            };
            value = source.arg_value();
        }
        ArgValue::Unset
    }
}

/// Every variant as written, but for the two whose value is the
/// application's own and has no `Debug` to call: `Custom(..)`, `Source(..)`.
impl core::fmt::Debug for ArgValue {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        use crate::debug::{float, int, quoted};
        match self {
            ArgValue::Str(t) => {
                f.write_str("Str(")?;
                quoted(f, t.as_str())?;
                f.write_str(")")
            }
            ArgValue::Int(n) => {
                f.write_str("Int(")?;
                int(f, *n)?;
                f.write_str(")")
            }
            ArgValue::Float(x) => {
                f.write_str("Float(")?;
                float(f, *x)?;
                f.write_str(")")
            }
            ArgValue::Decimal(t) => {
                f.write_str("Decimal(")?;
                quoted(f, t.as_str())?;
                f.write_str(")")
            }
            ArgValue::DateTime(d) => {
                f.write_str("DateTime(")?;
                core::fmt::Debug::fmt(&**d, f)?;
                f.write_str(")")
            }
            ArgValue::Custom(_) => f.write_str("Custom(..)"),
            ArgValue::Source(_) => f.write_str("Source(..)"),
            ArgValue::Unset => f.write_str("Unset"),
        }
    }
}

impl From<Text> for ArgValue {
    fn from(t: Text) -> ArgValue {
        ArgValue::Str(t)
    }
}

/// Any string slice, copied: a call site's `&str` is rarely `'static`
/// (`user.name()`), and a description outlives the call. A literal keeps its
/// `&'static str` through [`ArgValue::str_static`], which is what the macro
/// emits for one.
impl From<&str> for ArgValue {
    fn from(s: &str) -> ArgValue {
        ArgValue::Str(Text::Shared(Arc::from(s)))
    }
}

impl From<String> for ArgValue {
    fn from(s: String) -> ArgValue {
        ArgValue::Str(Text::Shared(Arc::from(s)))
    }
}

impl From<&String> for ArgValue {
    fn from(s: &String) -> ArgValue {
        ArgValue::Str(Text::Shared(Arc::from(s.as_str())))
    }
}

impl From<Arc<str>> for ArgValue {
    fn from(s: Arc<str>) -> ArgValue {
        ArgValue::Str(Text::Shared(s))
    }
}

impl From<char> for ArgValue {
    fn from(c: char) -> ArgValue {
        let mut buf = [0u8; 4];
        ArgValue::Str(Text::Shared(Arc::from(&*c.encode_utf8(&mut buf))))
    }
}

macro_rules! int_arg {
    ($($t:ty),*) => {$(
        impl From<$t> for ArgValue {
            #[inline]
            fn from(n: $t) -> ArgValue {
                ArgValue::Int(i64::from(n))
            }
        }
    )*};
}
int_arg!(i8, i16, i32, i64, u8, u16, u32);

/// A count: `usize` is 32-bit on the client, and no collection a server
/// holds is longer than `i64::MAX`.
impl From<usize> for ArgValue {
    #[inline]
    fn from(n: usize) -> ArgValue {
        ArgValue::Int(i64::try_from(n).unwrap_or(i64::MAX))
    }
}

impl From<f64> for ArgValue {
    #[inline]
    fn from(x: f64) -> ArgValue {
        ArgValue::Float(x)
    }
}

impl From<f32> for ArgValue {
    #[inline]
    fn from(x: f32) -> ArgValue {
        ArgValue::Float(f64::from(x))
    }
}

impl From<DateTimeValue> for ArgValue {
    fn from(d: DateTimeValue) -> ArgValue {
        ArgValue::DateTime(Arc::new(d))
    }
}

impl From<DateTime<'_>> for ArgValue {
    fn from(d: DateTime<'_>) -> ArgValue {
        ArgValue::DateTime(Arc::new(DateTimeValue::from(d)))
    }
}

impl<C: CustomValue + Send + Sync + 'static> From<Arc<C>> for ArgValue {
    fn from(c: Arc<C>) -> ArgValue {
        ArgValue::Custom(c)
    }
}

/// The values of a call site, in slot order: up to four inline — the
/// reference workload's maximum — and a boxed slice beyond that. Never a
/// const generic: one concrete type, whatever the arity.
#[derive(Clone)]
pub struct ArgList(Repr);

/// Inline capacity.
pub(crate) const INLINE: usize = 4;

#[derive(Clone)]
enum Repr {
    Inline(u8, [ArgValue; INLINE]),
    Spill(Box<[ArgValue]>),
}

impl ArgList {
    /// No arguments.
    pub(crate) const EMPTY: ArgList = ArgList(Repr::Inline(
        0,
        [
            ArgValue::Unset,
            ArgValue::Unset,
            ArgValue::Unset,
            ArgValue::Unset,
        ],
    ));

    /// Up to [`INLINE`] values, in slot order; `n` says how many of `values`
    /// are real.
    pub(crate) const fn inline(n: u8, values: [ArgValue; INLINE]) -> ArgList {
        ArgList(Repr::Inline(n, values))
    }

    /// Five or more values.
    pub(crate) fn spill(values: Box<[ArgValue]>) -> ArgList {
        ArgList(Repr::Spill(values))
    }

    /// The values, in slot order.
    #[must_use]
    pub fn as_slice(&self) -> &[ArgValue] {
        match &self.0 {
            Repr::Inline(n, values) => values.get(..usize::from(*n)).unwrap_or(&[]),
            Repr::Spill(values) => values,
        }
    }
}

/// The values, as a list: inline or spilled is storage, not content.
impl core::fmt::Debug for ArgList {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("[")?;
        for (i, value) in self.as_slice().iter().enumerate() {
            if i > 0 {
                f.write_str(", ")?;
            }
            core::fmt::Debug::fmt(value, f)?;
        }
        f.write_str("]")
    }
}

/// The borrowed form of one value: [`Arg`] borrows the text, the application
/// value and — through `date` — the date/time the caller resolved.
pub(crate) fn to_arg<'a>(value: &'a ArgValue, date: Option<&'a DateTime<'a>>) -> Arg<'a> {
    match value {
        ArgValue::Str(t) => Arg::Str(t.as_str()),
        ArgValue::Int(n) => Arg::Int(*n),
        ArgValue::Float(x) => Arg::Float(*x),
        ArgValue::Decimal(t) => Arg::Decimal(t.as_str()),
        ArgValue::DateTime(_) => match date {
            Some(d) => Arg::DateTime(d),
            None => Arg::Unset,
        },
        ArgValue::Custom(c) => Arg::Custom(&**c),
        // A source is resolved before this point; one that is still here
        // resolved to nothing.
        ArgValue::Source(_) | ArgValue::Unset => Arg::Unset,
    }
}