Skip to content

Commit

Permalink
Add example of numbering system introspection (unicode-org#5939)
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Dec 20, 2024
1 parent c592b81 commit 451c30c
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 4 deletions.
66 changes: 64 additions & 2 deletions components/decimal/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 64 additions & 2 deletions components/decimal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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::<DecimalDigitsV1Marker>() {
//! *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
Expand Down

0 comments on commit 451c30c

Please sign in to comment.