use fixed_decimal::FixedDecimal;
use icu_decimal::options::FixedDecimalFormatterOptions;
use icu_decimal::{FixedDecimalFormatter, FixedDecimalFormatterPreferences};
use icu_locale_core::preferences::{
define_preferences, extensions::unicode::keywords::NumberingSystem, prefs_convert,
};
use icu_provider::{
DataError, DataIdentifierBorrowed, DataLocale, DataPayload, DataProvider, DataRequest,
};
use super::super::provider::percent::PercentEssentialsV1Marker;
use super::format::FormattedPercent;
use super::options::PercentFormatterOptions;
extern crate alloc;
define_preferences!(
[Copy]
PercentFormatterPreferences,
{
numbering_system: NumberingSystem
}
);
prefs_convert!(
PercentFormatterPreferences,
FixedDecimalFormatterPreferences,
{ numbering_system }
);
pub struct PercentFormatter<R> {
essential: DataPayload<PercentEssentialsV1Marker>,
options: PercentFormatterOptions,
fixed_decimal_formatter: R,
}
impl PercentFormatter<FixedDecimalFormatter> {
icu_provider::gen_any_buffer_data_constructors!(
(prefs: PercentFormatterPreferences, options: PercentFormatterOptions) -> 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: PercentFormatterPreferences,
options: PercentFormatterOptions,
) -> Result<Self, DataError> {
let fixed_decimal_formatter = FixedDecimalFormatter::try_new(
(&prefs).into(),
FixedDecimalFormatterOptions::default(),
)?;
PercentFormatter::try_new_with_fixed_decimal_formatter(
prefs,
fixed_decimal_formatter,
options,
)
}
#[doc = icu_provider::gen_any_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<super::super::provider::percent::PercentEssentialsV1Marker>
+ DataProvider<icu_decimal::provider::DecimalSymbolsV2Marker>
+ DataProvider<icu_decimal::provider::DecimalDigitsV1Marker>,
{
let fixed_decimal_formatter = FixedDecimalFormatter::try_new_unstable(
provider,
(&prefs).into(),
FixedDecimalFormatterOptions::default(),
)?;
PercentFormatter::try_new_with_fixed_decimal_formatter_unstable(
provider,
prefs,
fixed_decimal_formatter,
options,
)
}
pub fn format<'l>(&'l self, value: &'l FixedDecimal) -> FormattedPercent<'l> {
FormattedPercent {
value,
essential: self.essential.get(),
fixed_decimal_formatter: &self.fixed_decimal_formatter,
options: &self.options,
}
}
}
impl<R> PercentFormatter<R>
where
R: AsRef<FixedDecimalFormatter>,
{
#[cfg(feature = "compiled_data")]
pub fn try_new_with_fixed_decimal_formatter(
prefs: PercentFormatterPreferences,
fixed_decimal_formatter: R,
options: PercentFormatterOptions,
) -> Result<Self, DataError> {
let locale =
DataLocale::from_preferences_locale::<PercentEssentialsV1Marker>(prefs.locale_prefs);
let essential = crate::provider::Baked
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
})?
.payload;
Ok(Self {
essential,
options,
fixed_decimal_formatter,
})
}
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_with_fixed_decimal_formatter_unstable(
provider: &(impl DataProvider<PercentEssentialsV1Marker> + ?Sized),
prefs: PercentFormatterPreferences,
fixed_decimal_formatter: R,
options: PercentFormatterOptions,
) -> Result<Self, DataError> {
let locale =
DataLocale::from_preferences_locale::<PercentEssentialsV1Marker>(prefs.locale_prefs);
let essential = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
})?
.payload;
Ok(Self {
essential,
options,
fixed_decimal_formatter,
})
}
}