| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
#[diplomat::bridge] |
| 6 |
pub mod ffi { |
| 7 |
use alloc::boxed::Box; |
| 8 |
use icu_decimal::{ |
| 9 |
options::{FixedDecimalFormatterOptions, GroupingStrategy}, |
| 10 |
provider::DecimalSymbolsV1Marker, |
| 11 |
FixedDecimalFormatter, |
| 12 |
}; |
| 13 |
use icu_provider_adapters::any_payload::AnyPayloadProvider; |
| 14 |
use writeable::Writeable; |
| 15 |
|
| 16 |
use crate::{ |
| 17 |
data_struct::ffi::ICU4XDataStruct, errors::ffi::ICU4XError, |
| 18 |
fixed_decimal::ffi::ICU4XFixedDecimal, locale::ffi::ICU4XLocale, |
| 19 |
provider::ffi::ICU4XDataProvider, |
| 20 |
}; |
| 21 |
|
| 22 |
#[diplomat::opaque] |
| 23 |
/// An ICU4X Fixed Decimal Format object, capable of formatting a [`ICU4XFixedDecimal`] as a string. |
| 24 |
#[diplomat::rust_link(icu::decimal::FixedDecimalFormatter, Struct)] |
| 25 |
#[diplomat::rust_link(icu::datetime::FormattedFixedDecimal, Struct, hidden)] |
| 26 |
pub struct ICU4XFixedDecimalFormatter(pub FixedDecimalFormatter); |
| 27 |
|
| 28 |
#[diplomat::rust_link(icu::decimal::options::GroupingStrategy, Enum)] |
| 29 |
pub enum ICU4XFixedDecimalGroupingStrategy { |
| 30 |
Auto, |
| 31 |
Never, |
| 32 |
Always, |
| 33 |
Min2, |
| 34 |
} |
| 35 |
|
| 36 |
impl ICU4XFixedDecimalFormatter { |
| 37 |
/// Creates a new [`ICU4XFixedDecimalFormatter`] from locale data. |
| 38 |
#[diplomat::rust_link(icu::decimal::FixedDecimalFormatter::try_new, FnInStruct)] |
| 39 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_grouping_strategy")] |
| 40 |
pub fn create_with_grouping_strategy( |
| 41 |
provider: &ICU4XDataProvider, |
| 42 |
locale: &ICU4XLocale, |
| 43 |
grouping_strategy: ICU4XFixedDecimalGroupingStrategy, |
| 44 |
) -> Result<Box<ICU4XFixedDecimalFormatter>, ICU4XError> { |
| 45 |
let locale = locale.to_datalocale(); |
| 46 |
|
| 47 |
let grouping_strategy = match grouping_strategy { |
| 48 |
ICU4XFixedDecimalGroupingStrategy::Auto => GroupingStrategy::Auto, |
| 49 |
ICU4XFixedDecimalGroupingStrategy::Never => GroupingStrategy::Never, |
| 50 |
ICU4XFixedDecimalGroupingStrategy::Always => GroupingStrategy::Always, |
| 51 |
ICU4XFixedDecimalGroupingStrategy::Min2 => GroupingStrategy::Min2, |
| 52 |
}; |
| 53 |
let mut options = FixedDecimalFormatterOptions::default(); |
| 54 |
options.grouping_strategy = grouping_strategy; |
| 55 |
Ok(Box::new(ICU4XFixedDecimalFormatter(call_constructor!( |
| 56 |
FixedDecimalFormatter::try_new, |
| 57 |
FixedDecimalFormatter::try_new_with_any_provider, |
| 58 |
FixedDecimalFormatter::try_new_with_buffer_provider, |
| 59 |
provider, |
| 60 |
&locale, |
| 61 |
options, |
| 62 |
)?))) |
| 63 |
} |
| 64 |
|
| 65 |
/// Creates a new [`ICU4XFixedDecimalFormatter`] from preconstructed locale data in the form of an [`ICU4XDataStruct`] |
| 66 |
/// constructed from `ICU4XDataStruct::create_decimal_symbols()`. |
| 67 |
/// |
| 68 |
/// The contents of the data struct will be consumed: if you wish to use the struct again it will have to be reconstructed. |
| 69 |
/// Passing a consumed struct to this method will return an error. |
| 70 |
#[diplomat::attr(*, disable)] |
| 71 |
pub fn create_with_decimal_symbols_v1( |
| 72 |
data_struct: &ICU4XDataStruct, |
| 73 |
grouping_strategy: ICU4XFixedDecimalGroupingStrategy, |
| 74 |
) -> Result<Box<ICU4XFixedDecimalFormatter>, ICU4XError> { |
| 75 |
let grouping_strategy = match grouping_strategy { |
| 76 |
ICU4XFixedDecimalGroupingStrategy::Auto => GroupingStrategy::Auto, |
| 77 |
ICU4XFixedDecimalGroupingStrategy::Never => GroupingStrategy::Never, |
| 78 |
ICU4XFixedDecimalGroupingStrategy::Always => GroupingStrategy::Always, |
| 79 |
ICU4XFixedDecimalGroupingStrategy::Min2 => GroupingStrategy::Min2, |
| 80 |
}; |
| 81 |
let mut options = FixedDecimalFormatterOptions::default(); |
| 82 |
options.grouping_strategy = grouping_strategy; |
| 83 |
Ok(Box::new(ICU4XFixedDecimalFormatter( |
| 84 |
FixedDecimalFormatter::try_new_with_any_provider( |
| 85 |
&AnyPayloadProvider::from_any_payload::<DecimalSymbolsV1Marker>( |
| 86 |
// Note: This clone is free, since cloning AnyPayload is free. |
| 87 |
data_struct.0.clone(), |
| 88 |
), |
| 89 |
&Default::default(), |
| 90 |
options, |
| 91 |
)?, |
| 92 |
))) |
| 93 |
} |
| 94 |
|
| 95 |
/// Formats a [`ICU4XFixedDecimal`] to a string. |
| 96 |
#[diplomat::rust_link(icu::decimal::FixedDecimalFormatter::format, FnInStruct)] |
| 97 |
#[diplomat::rust_link( |
| 98 |
icu::decimal::FixedDecimalFormatter::format_to_string, |
| 99 |
FnInStruct, |
| 100 |
hidden |
| 101 |
)] |
| 102 |
#[diplomat::rust_link(icu::decimal::FormattedFixedDecimal, Struct, hidden)] |
| 103 |
#[diplomat::rust_link(icu::decimal::FormattedFixedDecimal::write_to, FnInStruct, hidden)] |
| 104 |
pub fn format( |
| 105 |
&self, |
| 106 |
value: &ICU4XFixedDecimal, |
| 107 |
write: &mut diplomat_runtime::DiplomatWriteable, |
| 108 |
) -> Result<(), ICU4XError> { |
| 109 |
self.0.format(&value.0).write_to(write)?; |
| 110 |
Ok(()) |
| 111 |
} |
| 112 |
} |
| 113 |
} |