use fixed_decimal::FixedDecimal;
use icu_decimal::{options::FixedDecimalFormatterOptions, FixedDecimalFormatter};
use icu_plurals::PluralRules;
use icu_provider::prelude::*;
use crate::dimension::provider::{
currency_patterns::CurrencyPatternsDataV1Marker,
extended_currency::CurrencyExtendedDataV1Marker,
};
use super::{
formatter::CurrencyFormatterPreferences, long_format::LongFormattedCurrency, CurrencyCode,
};
extern crate alloc;
pub struct LongCurrencyFormatter {
extended: DataPayload<CurrencyExtendedDataV1Marker>,
patterns: DataPayload<CurrencyPatternsDataV1Marker>,
fixed_decimal_formatter: FixedDecimalFormatter,
plural_rules: PluralRules,
}
impl LongCurrencyFormatter {
icu_provider::gen_any_buffer_data_constructors!(
(prefs: CurrencyFormatterPreferences, currency_code: &CurrencyCode) -> error: DataError,
functions: [
try_new: skip,
try_new_with_any_provider,
try_new_with_buffer_provider,
try_new_unstable,
Self
]
);
#[cfg(feature = "compiled_data")]
pub fn try_new(
prefs: CurrencyFormatterPreferences,
currency_code: &CurrencyCode,
) -> Result<Self, DataError> {
let locale =
DataLocale::from_preferences_locale::<CurrencyPatternsDataV1Marker>(prefs.locale_prefs);
let fixed_decimal_formatter = FixedDecimalFormatter::try_new(
(&prefs).into(),
FixedDecimalFormatterOptions::default(),
)?;
let marker_attributes = DataMarkerAttributes::try_from_str(currency_code.0.as_str())
.map_err(|_| {
DataErrorKind::IdentifierNotFound
.into_error()
.with_debug_context("failed to get data marker attribute from a `CurrencyCode`")
})?;
let extended = crate::provider::Baked
.load(DataRequest {
id: DataIdentifierBorrowed::for_marker_attributes_and_locale(
marker_attributes,
&locale,
),
..Default::default()
})?
.payload;
let patterns = crate::provider::Baked.load(Default::default())?.payload;
let plural_rules = PluralRules::try_new_cardinal((&prefs).into())?;
Ok(Self {
extended,
patterns,
fixed_decimal_formatter,
plural_rules,
})
}
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_unstable<D>(
provider: &D,
prefs: CurrencyFormatterPreferences,
currency_code: &CurrencyCode,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<super::super::provider::extended_currency::CurrencyExtendedDataV1Marker>
+ DataProvider<super::super::provider::currency_patterns::CurrencyPatternsDataV1Marker>
+ DataProvider<icu_decimal::provider::DecimalSymbolsV2Marker>
+ DataProvider<icu_decimal::provider::DecimalDigitsV1Marker>
+ DataProvider<icu_plurals::provider::CardinalV1Marker>,
{
let locale =
DataLocale::from_preferences_locale::<CurrencyPatternsDataV1Marker>(prefs.locale_prefs);
let fixed_decimal_formatter = FixedDecimalFormatter::try_new_unstable(
provider,
(&prefs).into(),
FixedDecimalFormatterOptions::default(),
)?;
let marker_attributes = DataMarkerAttributes::try_from_str(currency_code.0.as_str())
.map_err(|_| {
DataErrorKind::IdentifierNotFound
.into_error()
.with_debug_context("failed to get data marker attribute from a `CurrencyCode`")
})?;
let extended = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_marker_attributes_and_locale(
marker_attributes,
&locale,
),
..Default::default()
})?
.payload;
let patterns = provider.load(Default::default())?.payload;
let plural_rules = PluralRules::try_new_cardinal_unstable(provider, (&prefs).into())?;
Ok(Self {
extended,
patterns,
fixed_decimal_formatter,
plural_rules,
})
}
pub fn format_fixed_decimal<'l>(
&'l self,
value: &'l FixedDecimal,
currency_code: CurrencyCode,
) -> LongFormattedCurrency<'l> {
LongFormattedCurrency {
value,
_currency_code: currency_code,
extended: self.extended.get(),
patterns: self.patterns.get(),
fixed_decimal_formatter: &self.fixed_decimal_formatter,
plural_rules: &self.plural_rules,
}
}
}