mf2_runtime/lib.rs
1//! `mf2-runtime` — the MessageFormat 2 evaluator of Rust MF2: it formats a message **from a catalog**, walking
2//! `mf2-catalog`'s views in place — resolution, declarations (lazily, each at
3//! most once), selection, fallback, the Default Bidi Strategy, format to
4//! parts, markup, the `u:` options — and it holds the function registry, the
5//! custom-function API and the core functions: `:string`, and `:number`,
6//! `:integer`, `:offset` with their complete semantics and neutral symbols.
7//!
8//! | Item | What |
9//! |---|---|
10//! | [`Formatter`] | the entry point: `simple`, `write`, `parts`, `*_named` |
11//! | [`Sink`], [`PartSink`], [`ErrorSink`] | where output and errors go |
12//! | [`Arg`], [`Value`], [`Number`] | arguments and resolved values |
13//! | [`Function`], [`Registry`], [`functions`] | handlers; a registry names only the handlers its corpus uses, and no other is linked |
14//! | [`Host`] | NFC, float text, zone offsets, a date formatter, a number formatter (`intl`) from the platform |
15//! | [`DateTime`], [`TimeZone`], [`NumberSpec`], [`Digits`], [`Measure`] | what the function crates and custom functions build on |
16//!
17//! Client-path code: `no_std` + `alloc`, `forbid(unsafe_code)`, no
18//! `core::fmt`, no panicking operation; built-in handlers never allocate.
19//!
20//! # The user guide
21//!
22//! The [Rust MF2 book](https://evancarroll.github.io/rust-mf2/) is the user
23//! guide: how the crates fit together, web and native applications, the
24//! command line, and what 2.x promises.
25//! An application reaches this crate through
26//! [`mf2`](https://docs.rs/mf2), which re-exports it; a custom function is
27//! written against [`Function`].
28
29#![warn(missing_docs, missing_debug_implementations)]
30// docs.rs (`cargo xtask docs-rs`): each feature-gated item says which features it needs.
31#![cfg_attr(docsrs, feature(doc_cfg))]
32#![no_std]
33#![forbid(unsafe_code)]
34#![deny(
35 clippy::unwrap_used,
36 clippy::expect_used,
37 clippy::indexing_slicing,
38 clippy::panic
39)]
40
41extern crate alloc;
42
43mod datetime;
44mod error;
45mod eval;
46mod format;
47mod function;
48pub mod functions;
49mod host;
50mod number;
51mod parts;
52mod plural;
53mod scratch;
54mod sink;
55mod text;
56mod unannotated;
57mod value;
58
59#[doc(hidden)]
60pub use mf2_catalog::StrRef;
61pub use mf2_catalog::{Catalog, Dir, MsgId};
62pub use mf2_model::MarkupKind;
63
64pub use datetime::{
65 Date, DateFields, DateLength, DateStyle, DateTime, DateTimeOptions, DateTimeRequest, Time,
66 TimePrecision, TimeZone, ZoneOption, ZoneStyle, is_zone_name,
67};
68pub use error::FormatError;
69pub use format::{BidiStrategy, FormatContext, Formatter};
70pub use function::{FnContext, Function, OptionValue, Options, Registry};
71pub use host::{Host, NumberFormatter};
72pub use number::{
73 CurrencyDisplay, DigitOptions, Digits, Grouping, Measure, MeasureUnit, Number, NumberOut,
74 NumberRequest, NumberSpec, NumberStyle, RoundingMode, RoundingPriority, Sign, SignDisplay,
75 UnitDisplay,
76};
77pub use parts::{
78 ExpressionPart, FallbackSource, Isolation, MarkupOptions, MarkupPart, Part, PartSink,
79};
80#[doc(hidden)]
81pub use plural::select as plural_category;
82pub use plural::{Category, Operands};
83pub use sink::{ErrorSink, NoErrors, Sink, SubPartSink};
84pub use value::{Arg, CustomValue, Value};
85
86/// Whether numbers format through the host (`plans/03-runtime.md` §2.7,
87/// §5.3): feature `intl`, on `wasm32-unknown-unknown` only. The numeric
88/// functions — the core's and `mf2-fn-number`'s — then take the display,
89/// `:integer`'s rounding and the plural category from
90/// the host's [`NumberFormatter`] ([`Host::numbers`]: the browser's
91/// `Intl.NumberFormat` and `Intl.PluralRules`) instead of the Rust digit
92/// plan, rounding and plural evaluator, which are not linked. Everywhere
93/// else — servers, `wasm32-wasip1`, native tests — `false`: the Rust path.
94#[doc(hidden)]
95pub const INTL_NUMBERS: bool = cfg!(all(
96 feature = "intl",
97 target_arch = "wasm32",
98 target_os = "unknown"
99));