Module provider

Source
Expand description

🚧 [Unstable] Data provider struct definitions for this ICU4X component.

🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, including in SemVer minor releases. While the serde representation of data structs is guaranteed to be stable, their Rust representation might not be. Use with caution.

Read more about data providers: icu_provider

§Examples

§Get the resolved numbering system

In a constructor call, the last request for DecimalDigitsV1 contains the resolved numbering system as its attribute:

use icu::decimal::provider::DecimalDigitsV1;
use icu::decimal::DecimalFormatter;
use icu::locale::locale;
use icu_provider::prelude::*;
use std::any::TypeId;
use std::cell::RefCell;

struct NumberingSystemInspectionProvider<P> {
    inner: P,
    numbering_system: RefCell<Option<Box<DataMarkerAttributes>>>,
}

impl<M, P> DataProvider<M> for NumberingSystemInspectionProvider<P>
where
    M: DataMarker,
    P: DataProvider<M>,
{
    fn load(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
        if TypeId::of::<M>() == TypeId::of::<DecimalDigitsV1>() {
            *self.numbering_system.try_borrow_mut().unwrap() =
                Some(req.id.marker_attributes.to_owned());
        }
        self.inner.load(req)
    }
}

let provider = NumberingSystemInspectionProvider {
    inner: icu::decimal::provider::Baked,
    numbering_system: RefCell::new(None),
};

let formatter = DecimalFormatter::try_new_unstable(
    &provider,
    locale!("th").into(),
    Default::default(),
)
.unwrap();

assert_eq!(
    provider
        .numbering_system
        .borrow()
        .as_ref()
        .map(|x| x.as_str()),
    Some("latn")
);

let formatter = DecimalFormatter::try_new_unstable(
    &provider,
    locale!("th-u-nu-thai").into(),
    Default::default(),
)
.unwrap();

assert_eq!(
    provider
        .numbering_system
        .borrow()
        .as_ref()
        .map(|x| x.as_str()),
    Some("thai")
);

let formatter = DecimalFormatter::try_new_unstable(
    &provider,
    locale!("th-u-nu-adlm").into(),
    Default::default(),
)
.unwrap();

assert_eq!(
    provider
        .numbering_system
        .borrow()
        .as_ref()
        .map(|x| x.as_str()),
    Some("adlm")
);

Structs§

Baked
Baked data
DecimalDigitsV1
The digits for a given numbering system. This data ought to be stored in the und locale with an auxiliary key set to the numbering system code.
DecimalSymbolStrsBuilder
A stack representation of the strings used in DecimalSymbols, i.e. a builder type for DecimalSymbolsStrs. This type can be obtained from a DecimalSymbolsStrs the From/Into traits.
DecimalSymbols
Symbols and metadata required for formatting a Decimal.
DecimalSymbolsStrs
VarULE type for DecimalSymbolStrsBuilder. See DecimalSymbolStrsBuilder for documentation.
DecimalSymbolsV1
Data marker for decimal symbols
GroupingSizes
A collection of settings expressing where to put grouping separators in a decimal number. For example, 1,000,000 has two grouping separators, positioned along every 3 digits.