Skip to content

Commit

Permalink
Add numbering_system getter to FixedDecimalFormatter (unicode-org#5904)
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Dec 16, 2024
1 parent d845fb6 commit 7e5dcec
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 0 deletions.
44 changes: 44 additions & 0 deletions components/decimal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub use format::FormattedFixedDecimal;

use alloc::string::String;
use fixed_decimal::SignedFixedDecimal;
use icu_locale_core::extensions::unicode::Value;
use icu_locale_core::locale;
use icu_locale_core::preferences::{
define_preferences, extensions::unicode::keywords::NumberingSystem,
Expand Down Expand Up @@ -214,4 +215,47 @@ impl FixedDecimalFormatter {
pub fn format_to_string(&self, value: &SignedFixedDecimal) -> String {
self.format(value).write_to_string().into_owned()
}

/// Gets the resolved numbering system identifier of this formatter.
///
/// # Examples
///
/// ```
/// use icu::decimal::FixedDecimalFormatter;
/// use icu::locale::locale;
///
/// let fmt_en = FixedDecimalFormatter::try_new(
/// locale!("en").into(),
/// Default::default()
/// )
/// .unwrap();
///
/// let fmt_bn = FixedDecimalFormatter::try_new(
/// locale!("bn").into(),
/// Default::default()
/// )
/// .unwrap();
///
/// let fmt_zh_nu = FixedDecimalFormatter::try_new(
/// locale!("zh-u-nu-hanidec").into(),
/// Default::default()
/// )
/// .unwrap();
///
/// assert_eq!(fmt_en.numbering_system(), "latn");
/// assert_eq!(fmt_bn.numbering_system(), "beng");
/// assert_eq!(fmt_zh_nu.numbering_system(), "hanidec");
/// ```
pub fn numbering_system(&self) -> Value {
match Value::try_from_str(self.symbols.get().numsys()) {
Ok(v) => v,
Err(e) => {
debug_assert!(
false,
"Problem converting numbering system ID to Value: {e}"
);
Value::new_empty()
}
}
}
}
13 changes: 13 additions & 0 deletions components/locale_core/src/extensions/unicode/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ impl Value {
self.0.len()
}

/// Creates an empty [`Value`], which corresponds to a "true" value.
///
/// # Examples
///
/// ```
/// use icu::locale::extensions::unicode::{value, Value};
///
/// assert_eq!(value!("true"), Value::new_empty());
/// ```
pub const fn new_empty() -> Self {
Self(ShortBoxSlice::new())
}

/// Returns `true` if the Value has no subtags.
///
/// # Examples
Expand Down
2 changes: 2 additions & 0 deletions ffi/capi/bindings/c/FixedDecimalFormatter.h

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

2 changes: 2 additions & 0 deletions ffi/capi/bindings/cpp/icu4x/FixedDecimalFormatter.d.hpp

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

10 changes: 10 additions & 0 deletions ffi/capi/bindings/cpp/icu4x/FixedDecimalFormatter.hpp

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

12 changes: 12 additions & 0 deletions ffi/capi/bindings/dart/FixedDecimalFormatter.g.dart

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

1 change: 1 addition & 0 deletions ffi/capi/bindings/demo_gen/FixedDecimalFormatter.d.ts

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

19 changes: 19 additions & 0 deletions ffi/capi/bindings/demo_gen/FixedDecimalFormatter.mjs

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

21 changes: 21 additions & 0 deletions ffi/capi/bindings/demo_gen/index.mjs

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

2 changes: 2 additions & 0 deletions ffi/capi/bindings/js/FixedDecimalFormatter.d.ts

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

13 changes: 13 additions & 0 deletions ffi/capi/bindings/js/FixedDecimalFormatter.mjs

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

5 changes: 5 additions & 0 deletions ffi/capi/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,10 @@ pub mod ffi {
) {
let _infallible = self.0.format(&value.0).write_to(write);
}

#[diplomat::rust_link(icu::decimal::FixedDecimalFormatter::numbering_system, FnInStruct)]
pub fn numbering_system(&self, write: &mut diplomat_runtime::DiplomatWrite) {
let _infallible = self.0.numbering_system().write_to(write);
}
}
}

0 comments on commit 7e5dcec

Please sign in to comment.