From 8495ff572b8f4a16990ecc1f51645e95e59032f7 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Thu, 19 Dec 2024 18:16:37 -0800 Subject: [PATCH] Add example of numbering system introspection --- components/decimal/src/lib.rs | 66 +++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/components/decimal/src/lib.rs b/components/decimal/src/lib.rs index e62d96826a5..47220f0a6c9 100644 --- a/components/decimal/src/lib.rs +++ b/components/decimal/src/lib.rs @@ -54,8 +54,7 @@ //! //! ### Format a number using an alternative numbering system //! -//! Numbering systems specified in the `-u-nu` subtag will be followed as long as the locale has -//! symbols for that numbering system. +//! Numbering systems specified in the `-u-nu` subtag will be followed. //! //! ``` //! use fixed_decimal::SignedFixedDecimal; @@ -74,6 +73,69 @@ //! assert_writeable_eq!(fdf.format(&fixed_decimal), "๑,๐๐๐,๐๐๗"); //! ``` //! +//! ### Get the resolved numbering system +//! +//! Inspect the data request to get the resolved numbering system (public but unstable): +//! +//! ``` +//! use icu_provider::prelude::*; +//! use icu::decimal::FixedDecimalFormatter; +//! use icu::decimal::provider::DecimalDigitsV1Marker; +//! use icu::locale::locale; +//! use std::any::TypeId; +//! use std::cell::RefCell; +//! +//! struct NumberingSystemInspectionProvider

{ +//! inner: P, +//! numbering_system: RefCell>>, +//! } +//! +//! impl DataProvider for NumberingSystemInspectionProvider

+//! where +//! M: DataMarker, +//! P: DataProvider, +//! { +//! fn load(&self, req: DataRequest) -> Result, DataError> { +//! if TypeId::of::() == TypeId::of::() { +//! *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 fdf = FixedDecimalFormatter::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 fdf = FixedDecimalFormatter::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 fdf = FixedDecimalFormatter::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")); +//! ``` +//! //! [`FixedDecimalFormatter`]: FixedDecimalFormatter // https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations