Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for resolved decimal and datetime prefs #5941

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions components/datetime/src/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,22 @@ define_preferences!(
/// The user's preferred numbering system.
///
/// Corresponds to the `-u-nu` in Unicode Locale Identifier.
///
/// To get the resolved numbering system, you can inspect the data provider.
/// See the [`icu_decimal::provider`] module for an example.
numbering_system: NumberingSystem,
/// The user's preferred hour cycle.
///
/// Corresponds to the `-u-hc` in Unicode Locale Identifier.
///
/// To get the resolved hour cycle, you can inspect the formatting pattern.
/// See [`DateTimePattern`](crate::pattern::DateTimePattern) for an example.
hour_cycle: HourCycle,
/// The user's preferred calendar system
///
/// Corresponds to the `-u-ca` in Unicode Locale Identifier.
///
/// To get the resolved calendar system, use [`DateTimeFormatter::calendar_kind()`].
calendar_algorithm: CalendarAlgorithm
}
);
Expand Down
65 changes: 1 addition & 64 deletions components/decimal/README.md

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

70 changes: 5 additions & 65 deletions components/decimal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//! assert_writeable_eq!(fdf.format(&fixed_decimal), "2,000.50");
//! ```
//!
//! ### Format a number using an alternative numbering system
//! ## Format a number using an alternative numbering system
//!
//! Numbering systems specified in the `-u-nu` subtag will be followed.
//!
Expand All @@ -73,69 +73,6 @@
//! 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 Expand Up @@ -185,6 +122,9 @@ define_preferences!(
/// The user's preferred numbering system.
///
/// Corresponds to the `-u-nu` in Unicode Locale Identifier.
///
/// To get the resolved numbering system, you can inspect the data provider.
/// See the [`provider`] module for an example.
numbering_system: NumberingSystem
}
);
Expand All @@ -197,7 +137,7 @@ define_preferences!(
/// 2. Locale-sensitive grouping separator positions
/// 3. Locale-sensitive plus and minus signs
///
/// Read more about the options in the [`options`] module.
/// To get the resolved numbering system, see [`provider`].
///
/// See the crate-level documentation for examples.
#[doc = fixed_decimal_formatter_size!()]
Expand Down
66 changes: 66 additions & 0 deletions components/decimal/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,72 @@
//! </div>
//!
//! Read more about data providers: [`icu_provider`]
//!
//! # Examples
//!
//! ## Get the resolved numbering system
//!
//! In a constructor call, the _last_ request for [`DecimalDigitsV1Marker`]
//! contains the resolved numbering system as its attribute:
//!
//! ```
//! 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"));
//! ```

// Provider structs must be stable
#![allow(clippy::exhaustive_structs)]
Expand Down
Loading