Skip to content
Open
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
19 changes: 14 additions & 5 deletions components/decimal/src/abstract_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ pub trait AbstractFormatter: core::fmt::Debug + Sealed {
Self: 'a;

#[doc(hidden)]
fn format_unsigned<'a>(&'a self, value: &'a UnsignedDecimal) -> Self::FormattedUnsigned<'a>;
fn format_unsigned<'a>(
&'a self,
value: crate::Cow<'a, UnsignedDecimal>,
) -> Self::FormattedUnsigned<'a>;

#[doc(hidden)]
fn format_sign<'a, W: Writeable>(
Expand All @@ -45,8 +48,11 @@ impl Sealed for DecimalFormatter {}
impl AbstractFormatter for DecimalFormatter {
type FormattedUnsigned<'a> = FormattedUnsignedDecimal<'a>;

fn format_unsigned<'a>(&'a self, value: &'a UnsignedDecimal) -> Self::FormattedUnsigned<'a> {
self.format_unsigned(crate::Cow::Borrowed(value))
fn format_unsigned<'a>(
&'a self,
value: crate::Cow<'a, UnsignedDecimal>,
) -> Self::FormattedUnsigned<'a> {
self.format_unsigned(value)
}

fn format_sign<'a, W: Writeable>(
Expand All @@ -66,8 +72,11 @@ impl Sealed for CompactDecimalFormatter {}
impl AbstractFormatter for CompactDecimalFormatter {
type FormattedUnsigned<'a> = FormattedUnsignedCompactDecimal<'a>;

fn format_unsigned<'a>(&'a self, value: &'a UnsignedDecimal) -> Self::FormattedUnsigned<'a> {
self.format_unsigned(value)
fn format_unsigned<'a>(
&'a self,
value: crate::Cow<'a, UnsignedDecimal>,
) -> Self::FormattedUnsigned<'a> {
self.format_unsigned(&value)
}

fn format_sign<'a, W: Writeable>(
Expand Down
22 changes: 12 additions & 10 deletions components/experimental/src/dimension/currency/compact_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod tests {
pub fn test_en_us() {
let prefs = locale!("en-US").into();
let currency_code = CurrencyCode(tinystr!(3, "USD"));
let fmt = CurrencyFormatter::try_new_compact_short(prefs, &currency_code).unwrap();
let fmt = CurrencyFormatter::try_new_compact_short(prefs, Default::default(), &currency_code).unwrap();

// Positive case
let positive_value = "12345.67".parse().unwrap();
Expand All @@ -31,7 +31,7 @@ mod tests {
pub fn test_fr_fr() {
let prefs = locale!("fr-FR").into();
let currency_code = CurrencyCode(tinystr!(3, "EUR"));
let fmt = CurrencyFormatter::try_new_compact_short(prefs, &currency_code).unwrap();
let fmt = CurrencyFormatter::try_new_compact_short(prefs, Default::default(), &currency_code).unwrap();

// Positive case
let positive_value = "12345.67".parse().unwrap();
Expand All @@ -48,7 +48,7 @@ mod tests {
pub fn test_zh_cn() {
let prefs = locale!("zh-CN").into();
let currency_code = CurrencyCode(tinystr!(3, "CNY"));
let fmt = CurrencyFormatter::try_new_compact_short(prefs, &currency_code).unwrap();
let fmt = CurrencyFormatter::try_new_compact_short(prefs, Default::default(), &currency_code).unwrap();

// Positive case
let positive_value = "12345.67".parse().unwrap();
Expand All @@ -65,7 +65,7 @@ mod tests {
pub fn test_ar_eg() {
let prefs = locale!("ar-EG").into();
let currency_code = CurrencyCode(tinystr!(3, "EGP"));
let fmt = CurrencyFormatter::try_new_compact_short(prefs, &currency_code).unwrap();
let fmt = CurrencyFormatter::try_new_compact_short(prefs, Default::default(), &currency_code).unwrap();

// Positive case
let positive_value = "12345.67".parse().unwrap();
Expand All @@ -87,7 +87,7 @@ mod tests {
let prefs = locale!("en-US").into();

let currency_code = CurrencyCode(tinystr!(3, "USD"));
let fmt = CurrencyFormatter::try_new_compact_long(prefs, &currency_code).unwrap();
let fmt = CurrencyFormatter::try_new_compact_long(prefs, Default::default(), &currency_code).unwrap();

// Positive case
let positive_value = "12345.67".parse().unwrap();
Expand All @@ -105,7 +105,7 @@ mod tests {
let prefs = locale!("en-US").into();

let currency_code = CurrencyCode(tinystr!(3, "USD"));
let fmt = CurrencyFormatter::try_new_compact_long(prefs, &currency_code).unwrap();
let fmt = CurrencyFormatter::try_new_compact_long(prefs, Default::default(), &currency_code).unwrap();

// Positive case
let positive_value = "12345000.67".parse().unwrap();
Expand All @@ -123,7 +123,7 @@ mod tests {
let prefs = locale!("fr-FR").into();

let currency_code = CurrencyCode(tinystr!(3, "USD"));
let fmt = CurrencyFormatter::try_new_compact_long(prefs, &currency_code).unwrap();
let fmt = CurrencyFormatter::try_new_compact_long(prefs, Default::default(), &currency_code).unwrap();

// Positive case
let positive_value = "12345.67".parse().unwrap();
Expand All @@ -141,7 +141,7 @@ mod tests {
let prefs = locale!("fr-FR").into();

let currency_code = CurrencyCode(tinystr!(3, "USD"));
let fmt = CurrencyFormatter::try_new_compact_long(prefs, &currency_code).unwrap();
let fmt = CurrencyFormatter::try_new_compact_long(prefs, Default::default(), &currency_code).unwrap();

// Positive case
let positive_value = "12345000.67".parse().unwrap();
Expand All @@ -160,8 +160,10 @@ mod tests {
let usd = CurrencyCode(tinystr!(3, "USD"));
let sek = CurrencyCode(tinystr!(3, "SEK"));

let fmt_usd = CurrencyFormatter::try_new_compact_short(prefs, &usd).unwrap();
let fmt_sek = CurrencyFormatter::try_new_compact_short(prefs, &sek).unwrap();
let fmt_usd =
CurrencyFormatter::try_new_compact_short(prefs, Default::default(), &usd).unwrap();
let fmt_sek =
CurrencyFormatter::try_new_compact_short(prefs, Default::default(), &sek).unwrap();

// Small number (magnitude < 3, no compact suffix): should fall back cleanly
let small_value = "123".parse().unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::dimension::provider::currency::{essentials::*, extended::*, patterns::*, symbols::*};
use crate::dimension::provider::currency::{
essentials::*, extended::*, fractions::*, patterns::*, symbols::*,
};
use icu_decimal::CompactDecimalFormatter;
use icu_provider::prelude::*;

use super::CurrencyCode;
use super::formatter::{CurrencyFormatter, CurrencyFormatterPreferences};
use super::{options::CurrencyFormatterOptions, CurrencyCode};

impl CurrencyFormatter<CompactDecimalFormatter> {
icu_provider::gen_buffer_data_constructors!(
(prefs: CurrencyFormatterPreferences, currency_code: &CurrencyCode) -> error: DataError,
(prefs: CurrencyFormatterPreferences, options: CurrencyFormatterOptions, currency_code: &CurrencyCode) -> error: DataError,
functions: [
try_new_compact_short: skip,
try_new_compact_short_with_buffer_provider,
Expand All @@ -21,7 +23,7 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
);

icu_provider::gen_buffer_data_constructors!(
(prefs: CurrencyFormatterPreferences, currency_code: &CurrencyCode) -> error: DataError,
(prefs: CurrencyFormatterPreferences, options: CurrencyFormatterOptions, currency_code: &CurrencyCode) -> error: DataError,
functions: [
try_new_compact_narrow: skip,
try_new_compact_narrow_with_buffer_provider,
Expand All @@ -38,11 +40,13 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
#[cfg(feature = "compiled_data")]
pub fn try_new_compact_short(
prefs: CurrencyFormatterPreferences,
options: CurrencyFormatterOptions,
currency_code: &CurrencyCode,
) -> Result<Self, DataError> {
Self::try_new_essential(
CompactDecimalFormatter::try_new_short((&prefs).into(), Default::default())?,
prefs,
options,
*currency_code,
Width::Short,
)
Expand All @@ -56,11 +60,13 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
#[cfg(feature = "compiled_data")]
pub fn try_new_compact_narrow(
prefs: CurrencyFormatterPreferences,
options: CurrencyFormatterOptions,
currency_code: &CurrencyCode,
) -> Result<Self, DataError> {
Self::try_new_essential(
CompactDecimalFormatter::try_new_short((&prefs).into(), Default::default())?,
prefs,
options,
*currency_code,
Width::Narrow,
)
Expand All @@ -70,12 +76,14 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
pub fn try_new_compact_short_unstable<D>(
provider: &D,
prefs: CurrencyFormatterPreferences,
options: CurrencyFormatterOptions,
currency_code: &CurrencyCode,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<CurrencyEssentialsV1>
+ DataProvider<CurrencySymbolsV1>
+ DataProvider<CurrencyFractionsV1>
+ DataProvider<icu_decimal::provider::DecimalCompactShortV1>
+ DataProvider<icu_decimal::provider::DecimalSymbolsV1>
+ DataProvider<icu_decimal::provider::DecimalDigitsV1>
Expand All @@ -89,6 +97,7 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
Default::default(),
)?,
prefs,
options,
*currency_code,
Width::Short,
)
Expand All @@ -98,12 +107,14 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
pub fn try_new_compact_narrow_unstable<D>(
provider: &D,
prefs: CurrencyFormatterPreferences,
options: CurrencyFormatterOptions,
currency_code: &CurrencyCode,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<CurrencyEssentialsV1>
+ DataProvider<CurrencySymbolsV1>
+ DataProvider<CurrencyFractionsV1>
+ DataProvider<icu_decimal::provider::DecimalCompactShortV1>
+ DataProvider<icu_decimal::provider::DecimalSymbolsV1>
+ DataProvider<icu_decimal::provider::DecimalDigitsV1>
Expand All @@ -117,6 +128,7 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
Default::default(),
)?,
prefs,
options,
*currency_code,
Width::Narrow,
)
Expand All @@ -125,6 +137,7 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
icu_provider::gen_buffer_data_constructors!(
(
prefs: CurrencyFormatterPreferences,
options: CurrencyFormatterOptions,
currency_code: &CurrencyCode
) -> error: DataError,
functions: [
Expand All @@ -143,11 +156,13 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
#[cfg(feature = "compiled_data")]
pub fn try_new_compact_long(
prefs: CurrencyFormatterPreferences,
options: CurrencyFormatterOptions,
currency_code: &CurrencyCode,
) -> Result<Self, DataError> {
Self::try_new_long_internal(
CompactDecimalFormatter::try_new_long((&prefs).into(), Default::default())?,
prefs,
options,
*currency_code,
)
}
Expand All @@ -156,12 +171,14 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
pub fn try_new_compact_long_unstable<D>(
provider: &D,
prefs: CurrencyFormatterPreferences,
options: CurrencyFormatterOptions,
currency_code: &CurrencyCode,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<CurrencyExtendedDataV1>
+ DataProvider<CurrencyPatternsDataV1>
+ DataProvider<CurrencyFractionsV1>
+ DataProvider<icu_decimal::provider::DecimalSymbolsV1>
+ DataProvider<icu_decimal::provider::DecimalDigitsV1>
+ DataProvider<icu_plurals::provider::PluralsCardinalV1>
Expand All @@ -175,6 +192,7 @@ impl CurrencyFormatter<CompactDecimalFormatter> {
Default::default(),
)?,
prefs,
options,
*currency_code,
)
}
Expand Down
Loading
Loading