#[diplomat::bridge]
pub mod ffi {
use alloc::boxed::Box;
use icu_decimal::{
options::{FixedDecimalFormatterOptions, GroupingStrategy},
provider::DecimalSymbolsV1Marker,
FixedDecimalFormatter,
};
use icu_provider_adapters::any_payload::AnyPayloadProvider;
use writeable::Writeable;
use crate::{
data_struct::ffi::ICU4XDataStruct, errors::ffi::ICU4XError,
fixed_decimal::ffi::ICU4XFixedDecimal, locale::ffi::ICU4XLocale,
provider::ffi::ICU4XDataProvider,
};
#[diplomat::opaque]
#[diplomat::rust_link(icu::decimal::FixedDecimalFormatter, Struct)]
#[diplomat::rust_link(icu::datetime::FormattedFixedDecimal, Struct, hidden)]
pub struct ICU4XFixedDecimalFormatter(pub FixedDecimalFormatter);
#[diplomat::rust_link(icu::decimal::options::GroupingStrategy, Enum)]
pub enum ICU4XFixedDecimalGroupingStrategy {
Auto,
Never,
Always,
Min2,
}
impl ICU4XFixedDecimalFormatter {
#[diplomat::rust_link(icu::decimal::FixedDecimalFormatter::try_new, FnInStruct)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_grouping_strategy")]
pub fn create_with_grouping_strategy(
provider: &ICU4XDataProvider,
locale: &ICU4XLocale,
grouping_strategy: ICU4XFixedDecimalGroupingStrategy,
) -> Result<Box<ICU4XFixedDecimalFormatter>, ICU4XError> {
let locale = locale.to_datalocale();
let grouping_strategy = match grouping_strategy {
ICU4XFixedDecimalGroupingStrategy::Auto => GroupingStrategy::Auto,
ICU4XFixedDecimalGroupingStrategy::Never => GroupingStrategy::Never,
ICU4XFixedDecimalGroupingStrategy::Always => GroupingStrategy::Always,
ICU4XFixedDecimalGroupingStrategy::Min2 => GroupingStrategy::Min2,
};
let mut options = FixedDecimalFormatterOptions::default();
options.grouping_strategy = grouping_strategy;
Ok(Box::new(ICU4XFixedDecimalFormatter(call_constructor!(
FixedDecimalFormatter::try_new,
FixedDecimalFormatter::try_new_with_any_provider,
FixedDecimalFormatter::try_new_with_buffer_provider,
provider,
&locale,
options,
)?)))
}
#[diplomat::attr(*, disable)]
pub fn create_with_decimal_symbols_v1(
data_struct: &ICU4XDataStruct,
grouping_strategy: ICU4XFixedDecimalGroupingStrategy,
) -> Result<Box<ICU4XFixedDecimalFormatter>, ICU4XError> {
let grouping_strategy = match grouping_strategy {
ICU4XFixedDecimalGroupingStrategy::Auto => GroupingStrategy::Auto,
ICU4XFixedDecimalGroupingStrategy::Never => GroupingStrategy::Never,
ICU4XFixedDecimalGroupingStrategy::Always => GroupingStrategy::Always,
ICU4XFixedDecimalGroupingStrategy::Min2 => GroupingStrategy::Min2,
};
let mut options = FixedDecimalFormatterOptions::default();
options.grouping_strategy = grouping_strategy;
Ok(Box::new(ICU4XFixedDecimalFormatter(
FixedDecimalFormatter::try_new_with_any_provider(
&AnyPayloadProvider::from_any_payload::<DecimalSymbolsV1Marker>(
data_struct.0.clone(),
),
&Default::default(),
options,
)?,
)))
}
#[diplomat::rust_link(icu::decimal::FixedDecimalFormatter::format, FnInStruct)]
#[diplomat::rust_link(
icu::decimal::FixedDecimalFormatter::format_to_string,
FnInStruct,
hidden
)]
#[diplomat::rust_link(icu::decimal::FormattedFixedDecimal, Struct, hidden)]
#[diplomat::rust_link(icu::decimal::FormattedFixedDecimal::write_to, FnInStruct, hidden)]
pub fn format(
&self,
value: &ICU4XFixedDecimal,
write: &mut diplomat_runtime::DiplomatWriteable,
) -> Result<(), ICU4XError> {
self.0.format(&value.0).write_to(write)?;
Ok(())
}
}
}