Skip to content
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
106 changes: 106 additions & 0 deletions components/experimental/src/dimension/currency/compact_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
]
);

icu_provider::gen_buffer_data_constructors!(
(prefs: CurrencyFormatterPreferences, currency_code: &CurrencyCode) -> error: DataError,
functions: [
try_new_compact_code: skip,
try_new_compact_code_with_buffer_provider,
try_new_compact_code_unstable,
Self
]
);

icu_provider::gen_buffer_data_constructors!(
(prefs: CurrencyFormatterPreferences, currency_code: &CurrencyCode) -> error: DataError,
functions: [
Expand Down Expand Up @@ -60,6 +70,16 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
]
);

icu_provider::gen_buffer_data_constructors!(
(prefs: CurrencyFormatterPreferences, currency_code: &CurrencyCode) -> error: DataError,
functions: [
try_new_compact_long_code: skip,
try_new_compact_long_code_with_buffer_provider,
try_new_compact_long_code_unstable,
Self
]
);

icu_provider::gen_buffer_data_constructors!(
(prefs: CurrencyFormatterPreferences, currency_code: &CurrencyCode) -> error: DataError,
functions: [
Expand Down Expand Up @@ -106,6 +126,23 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
)
}

/// Creates a new [`CurrencyFormatter`] for compact short number formatting using the 3-letter ISO currency code from compiled locale data.
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
///
/// [📚 Help choosing a constructor](icu_provider::constructors)
#[cfg(feature = "compiled_data")]
pub fn try_new_compact_code(
prefs: CurrencyFormatterPreferences,
currency_code: &CurrencyCode,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue, pre-existing: we should take CurrencyCode by value, it's a small Copy struct

) -> Result<Self, DataError> {
Self::try_new_code_internal(
CompactDecimalFormatter::try_new_short((&prefs).into(), Default::default())?,
prefs,
*currency_code,
)
}

/// Creates a new [`CurrencyFormatter`] for compact short number formatting with full currency display names from compiled locale data.
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
Expand Down Expand Up @@ -159,6 +196,23 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
)
}

/// Creates a new [`CurrencyFormatter`] for compact long number formatting using the 3-letter ISO currency code from compiled locale data.
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
///
/// [📚 Help choosing a constructor](icu_provider::constructors)
#[cfg(feature = "compiled_data")]
pub fn try_new_compact_long_code(
prefs: CurrencyFormatterPreferences,
currency_code: &CurrencyCode,
) -> Result<Self, DataError> {
Self::try_new_code_internal(
CompactDecimalFormatter::try_new_long((&prefs).into(), Default::default())?,
prefs,
*currency_code,
)
}

/// Creates a new [`CurrencyFormatter`] for compact long number formatting with full currency display names from compiled locale data.
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
Expand Down Expand Up @@ -341,4 +395,56 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
*currency_code,
)
}

#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_compact_code)]
pub fn try_new_compact_code_unstable<D>(
provider: &D,
prefs: CurrencyFormatterPreferences,
currency_code: &CurrencyCode,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<CurrencyEssentialsV1>
+ DataProvider<icu_decimal::provider::DecimalCompactShortV1>
+ DataProvider<icu_decimal::provider::DecimalSymbolsV1>
+ DataProvider<icu_decimal::provider::DecimalDigitsV1>
+ DataProvider<icu_plurals::provider::PluralsCardinalV1>,
{
Self::try_new_code_internal_unstable(
provider,
CompactDecimalFormatter::try_new_short_unstable(
provider,
(&prefs).into(),
Default::default(),
)?,
prefs,
*currency_code,
)
}

#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_compact_long_code)]
pub fn try_new_compact_long_code_unstable<D>(
provider: &D,
prefs: CurrencyFormatterPreferences,
currency_code: &CurrencyCode,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<CurrencyEssentialsV1>
+ DataProvider<icu_decimal::provider::DecimalCompactLongV1>
+ DataProvider<icu_decimal::provider::DecimalSymbolsV1>
+ DataProvider<icu_decimal::provider::DecimalDigitsV1>
+ DataProvider<icu_plurals::provider::PluralsCardinalV1>,
{
Self::try_new_code_internal_unstable(
provider,
CompactDecimalFormatter::try_new_long_unstable(
provider,
(&prefs).into(),
Default::default(),
)?,
prefs,
*currency_code,
)
}
}
32 changes: 32 additions & 0 deletions components/experimental/src/dimension/currency/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,36 @@ mod tests {
CurrencyFormatter::try_new_symbol_narrow(prefs, &currency_code).unwrap();
assert_writeable_eq!(fmt_symbol_narrow.format_fixed_decimal(&value), "$12,345.67");
}

#[test]
pub fn test_code() {
let prefs_en = locale!("en-US").into();
let currency_usd = CurrencyCode(tinystr!(3, "USD"));
let value = "12345.67".parse().unwrap();

let fmt_code_en = CurrencyFormatter::try_new_code(prefs_en, &currency_usd).unwrap();
assert_writeable_eq!(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test compact as well?

fmt_code_en.format_fixed_decimal(&value),
"USD\u{a0}12,345.67"
);

let prefs_fr = locale!("fr-FR").into();
let currency_eur = CurrencyCode(tinystr!(3, "EUR"));
let fmt_code_fr = CurrencyFormatter::try_new_code(prefs_fr, &currency_eur).unwrap();
assert_writeable_eq!(
fmt_code_fr.format_fixed_decimal(&value),
"12\u{202f}345,67\u{a0}EUR"
);
}

#[test]
pub fn test_name_fallback_to_iso_name() {
let prefs_en = locale!("en-US").into();
// Unknown currency code should gracefully fall back to IsoName instead of DataError(IdentifierNotFound)
let currency_xyz = CurrencyCode(tinystr!(3, "XYZ"));
let value = "12345.67".parse().unwrap();

let fmt_name = CurrencyFormatter::try_new_name(prefs_en, &currency_xyz).unwrap();
assert_writeable_eq!(fmt_name.format_fixed_decimal(&value), "12,345.67 XYZ");
}
}
Loading
Loading