use fixed_decimal::SignedFixedDecimal;
use icu_plurals::{PluralRules, PluralRulesPreferences};
use icu_provider::prelude::*;
use crate::{
compactdecimal::CompactDecimalFormatter,
compactdecimal::CompactDecimalFormatterOptions,
compactdecimal::CompactDecimalFormatterPreferences,
dimension::provider::{
currency_patterns::CurrencyPatternsDataV1Marker,
extended_currency::CurrencyExtendedDataV1Marker,
},
};
use icu_locale_core::preferences::{
define_preferences, extensions::unicode::keywords::NumberingSystem, prefs_convert,
};
use super::{long_compact_format::FormattedLongCompactCurrency, CurrencyCode};
extern crate alloc;
define_preferences!(
[Copy]
LongCompactCurrencyFormatterPreferences,
{
numbering_system: NumberingSystem
}
);
prefs_convert!(
LongCompactCurrencyFormatterPreferences,
CompactDecimalFormatterPreferences,
{ numbering_system }
);
prefs_convert!(
LongCompactCurrencyFormatterPreferences,
PluralRulesPreferences
);
pub struct LongCompactCurrencyFormatter {
extended: DataPayload<CurrencyExtendedDataV1Marker>,
patterns: DataPayload<CurrencyPatternsDataV1Marker>,
compact_decimal_formatter: CompactDecimalFormatter,
plural_rules: PluralRules,
}
impl LongCompactCurrencyFormatter {
icu_provider::gen_any_buffer_data_constructors!(
(
prefs: LongCompactCurrencyFormatterPreferences,
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: LongCompactCurrencyFormatterPreferences,
currency_code: &CurrencyCode,
) -> Result<Self, DataError> {
let compact_decimal_formatter = CompactDecimalFormatter::try_new_long(
(&prefs).into(),
CompactDecimalFormatterOptions::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 locale = &DataLocale::from_preferences_locale::<CurrencyPatternsDataV1Marker>(
prefs.locale_prefs,
);
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,
compact_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: LongCompactCurrencyFormatterPreferences,
currency_code: &CurrencyCode,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<
crate::dimension::provider::extended_currency::CurrencyExtendedDataV1Marker,
> + DataProvider<
crate::dimension::provider::currency_patterns::CurrencyPatternsDataV1Marker,
> + DataProvider<icu_decimal::provider::DecimalSymbolsV2Marker>
+ DataProvider<icu_decimal::provider::DecimalDigitsV1Marker>
+ DataProvider<icu_plurals::provider::CardinalV1Marker>
+ DataProvider<crate::compactdecimal::provider::LongCompactDecimalFormatDataV1Marker>,
{
let locale =
DataLocale::from_preferences_locale::<CurrencyPatternsDataV1Marker>(prefs.locale_prefs);
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())?;
let compact_decimal_formatter = CompactDecimalFormatter::try_new_long_unstable(
provider,
(&prefs).into(),
CompactDecimalFormatterOptions::default(),
)?;
Ok(Self {
extended,
patterns,
compact_decimal_formatter,
plural_rules,
})
}
pub fn format_fixed_decimal<'l>(
&'l self,
value: &'l SignedFixedDecimal,
currency_code: CurrencyCode,
) -> FormattedLongCompactCurrency<'l> {
FormattedLongCompactCurrency {
signed_fixed_decimal: value,
_currency_code: currency_code,
extended: self.extended.get(),
patterns: self.patterns.get(),
compact_decimal_formatter: &self.compact_decimal_formatter,
plural_rules: &self.plural_rules,
}
}
}