use fixed_decimal::Decimal;
use icu_decimal::options::DecimalFormatterOptions;
use icu_decimal::{DecimalFormatter, DecimalFormatterPreferences};
use icu_locale_core::preferences::{define_preferences, prefs_convert};
use icu_provider::prelude::*;
use super::super::provider::percent::PercentEssentialsV1;
use super::format::FormattedPercent;
use super::options::PercentFormatterOptions;
extern crate alloc;
define_preferences!(
[Copy]
PercentFormatterPreferences,
{
numbering_system: crate::dimension::preferences::NumberingSystem
}
);
prefs_convert!(PercentFormatterPreferences, DecimalFormatterPreferences, {
numbering_system
});
#[derive(Debug)]
pub struct PercentFormatter<R> {
essential: DataPayload<PercentEssentialsV1>,
options: PercentFormatterOptions,
decimal_formatter: R,
}
impl PercentFormatter<DecimalFormatter> {
icu_provider::gen_buffer_data_constructors!(
(prefs: PercentFormatterPreferences, options: PercentFormatterOptions) -> error: DataError,
functions: [
try_new: skip,
try_new_with_buffer_provider,
try_new_unstable,
Self
]
);
#[cfg(feature = "compiled_data")]
pub fn try_new(
prefs: PercentFormatterPreferences,
options: PercentFormatterOptions,
) -> Result<Self, DataError> {
let decimal_formatter =
DecimalFormatter::try_new((&prefs).into(), DecimalFormatterOptions::default())?;
PercentFormatter::try_new_with_decimal_formatter(prefs, decimal_formatter, options)
}
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_unstable<D>(
provider: &D,
prefs: PercentFormatterPreferences,
options: PercentFormatterOptions,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<PercentEssentialsV1>
+ DataProvider<icu_decimal::provider::DecimalSymbolsV1>
+ DataProvider<icu_decimal::provider::DecimalDigitsV1>,
{
let decimal_formatter = DecimalFormatter::try_new_unstable(
provider,
(&prefs).into(),
DecimalFormatterOptions::default(),
)?;
PercentFormatter::try_new_with_decimal_formatter_unstable(
provider,
prefs,
decimal_formatter,
options,
)
}
pub fn format<'l>(&'l self, value: &'l Decimal) -> FormattedPercent<'l> {
FormattedPercent {
value,
essential: self.essential.get(),
decimal_formatter: &self.decimal_formatter,
options: &self.options,
}
}
}
impl<R> PercentFormatter<R>
where
R: AsRef<DecimalFormatter>,
{
#[cfg(feature = "compiled_data")]
pub fn try_new_with_decimal_formatter(
prefs: PercentFormatterPreferences,
decimal_formatter: R,
options: PercentFormatterOptions,
) -> Result<Self, DataError> {
let locale = PercentEssentialsV1::make_locale(prefs.locale_preferences);
let essential = crate::provider::Baked
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
})?
.payload;
Ok(Self {
essential,
options,
decimal_formatter,
})
}
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_with_decimal_formatter_unstable(
provider: &(impl DataProvider<PercentEssentialsV1> + ?Sized),
prefs: PercentFormatterPreferences,
decimal_formatter: R,
options: PercentFormatterOptions,
) -> Result<Self, DataError> {
let locale = PercentEssentialsV1::make_locale(prefs.locale_preferences);
let essential = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
})?
.payload;
Ok(Self {
essential,
options,
decimal_formatter,
})
}
}