| 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 |
#![cfg(feature = "icu_decimal")] |
| 6 |
|
| 7 |
use alloc::borrow::Cow; |
| 8 |
|
| 9 |
#[diplomat::bridge] |
| 10 |
pub mod ffi { |
| 11 |
|
| 12 |
#[cfg(feature = "icu_decimal")] |
| 13 |
use crate::errors::ffi::ICU4XError; |
| 14 |
use alloc::boxed::Box; |
| 15 |
use icu_provider::AnyPayload; |
| 16 |
#[cfg(feature = "icu_decimal")] |
| 17 |
use icu_provider::DataPayload; |
| 18 |
|
| 19 |
#[diplomat::opaque] |
| 20 |
/// A generic data struct to be used by ICU4X |
| 21 |
/// |
| 22 |
/// This can be used to construct a StructDataProvider. |
| 23 |
#[diplomat::attr(*, disable)] |
| 24 |
pub struct ICU4XDataStruct(#[allow(dead_code)] AnyPayload); |
| 25 |
|
| 26 |
impl ICU4XDataStruct { |
| 27 |
/// Construct a new DecimalSymbolsV1 data struct. |
| 28 |
/// |
| 29 |
/// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according |
| 30 |
/// to the WHATWG Encoding Standard. |
| 31 |
#[diplomat::rust_link(icu::decimal::provider::DecimalSymbolsV1, Struct)] |
| 32 |
#[allow(clippy::too_many_arguments)] |
| 33 |
pub fn create_decimal_symbols_v1( |
| 34 |
plus_sign_prefix: &DiplomatStr, |
| 35 |
plus_sign_suffix: &DiplomatStr, |
| 36 |
minus_sign_prefix: &DiplomatStr, |
| 37 |
minus_sign_suffix: &DiplomatStr, |
| 38 |
decimal_separator: &DiplomatStr, |
| 39 |
grouping_separator: &DiplomatStr, |
| 40 |
primary_group_size: u8, |
| 41 |
secondary_group_size: u8, |
| 42 |
min_group_size: u8, |
| 43 |
digits: &[DiplomatChar], |
| 44 |
) -> Result<Box<ICU4XDataStruct>, ICU4XError> { |
| 45 |
use super::str_to_cow; |
| 46 |
use icu_decimal::provider::{ |
| 47 |
AffixesV1, DecimalSymbolsV1, DecimalSymbolsV1Marker, GroupingSizesV1, |
| 48 |
}; |
| 49 |
let digits = if digits.len() == 10 { |
| 50 |
let mut new_digits = ['\0'; 10]; |
| 51 |
for (old, new) in digits.iter().zip(new_digits.iter_mut()) { |
| 52 |
*new = char::from_u32(*old).ok_or(ICU4XError::DataStructValidityError)?; |
| 53 |
} |
| 54 |
new_digits |
| 55 |
} else { |
| 56 |
return Err(ICU4XError::DataStructValidityError); |
| 57 |
}; |
| 58 |
let plus_sign_affixes = AffixesV1 { |
| 59 |
prefix: str_to_cow(plus_sign_prefix), |
| 60 |
suffix: str_to_cow(plus_sign_suffix), |
| 61 |
}; |
| 62 |
let minus_sign_affixes = AffixesV1 { |
| 63 |
prefix: str_to_cow(minus_sign_prefix), |
| 64 |
suffix: str_to_cow(minus_sign_suffix), |
| 65 |
}; |
| 66 |
let grouping_sizes = GroupingSizesV1 { |
| 67 |
primary: primary_group_size, |
| 68 |
secondary: secondary_group_size, |
| 69 |
min_grouping: min_group_size, |
| 70 |
}; |
| 71 |
|
| 72 |
let symbols = DecimalSymbolsV1 { |
| 73 |
plus_sign_affixes, |
| 74 |
minus_sign_affixes, |
| 75 |
decimal_separator: str_to_cow(decimal_separator), |
| 76 |
grouping_separator: str_to_cow(grouping_separator), |
| 77 |
grouping_sizes, |
| 78 |
digits, |
| 79 |
}; |
| 80 |
|
| 81 |
let payload: DataPayload<DecimalSymbolsV1Marker> = DataPayload::from_owned(symbols); |
| 82 |
Ok(Box::new(ICU4XDataStruct(payload.wrap_into_any_payload()))) |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
fn str_to_cow(s: &diplomat_runtime::DiplomatStr) -> Cow<'static, str> { |
| 88 |
if s.is_empty() { |
| 89 |
Cow::default() |
| 90 |
} else { |
| 91 |
Cow::Owned(alloc::string::String::from_utf8_lossy(s).into_owned()) |
| 92 |
} |
| 93 |
} |