1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#[diplomat::bridge]
#[diplomat::abi_rename = "icu4x_{0}_mv1"]
#[diplomat::attr(auto, namespace = "icu4x")]
pub mod ffi {
    use alloc::boxed::Box;

    #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
    use crate::locale_core::ffi::Locale;
    #[cfg(feature = "buffer_provider")]
    use crate::provider::ffi::DataProvider;
    use crate::{errors::ffi::DataError, fixed_decimal::ffi::SignedFixedDecimal};
    use icu_decimal::options::FixedDecimalFormatterOptions;
    #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
    use icu_decimal::FixedDecimalFormatterPreferences;

    use writeable::Writeable;

    #[diplomat::opaque]
    /// An ICU4X Fixed Decimal Format object, capable of formatting a [`SignedFixedDecimal`] as a string.
    #[diplomat::rust_link(icu::decimal::FixedDecimalFormatter, Struct)]
    #[diplomat::rust_link(icu::datetime::FormattedFixedDecimal, Struct, hidden)]
    pub struct FixedDecimalFormatter(pub icu_decimal::FixedDecimalFormatter);

    #[diplomat::rust_link(icu::decimal::options::GroupingStrategy, Enum)]
    #[diplomat::enum_convert(icu_decimal::options::GroupingStrategy, needs_wildcard)]
    pub enum FixedDecimalGroupingStrategy {
        Auto,
        Never,
        Always,
        Min2,
    }

    impl FixedDecimalFormatter {
        /// Creates a new [`FixedDecimalFormatter`], using compiled data
        #[diplomat::rust_link(icu::decimal::FixedDecimalFormatter::try_new, FnInStruct)]
        #[diplomat::attr(supports = fallible_constructors, named_constructor = "with_grouping_strategy")]
        #[diplomat::demo(default_constructor)]
        #[cfg(feature = "compiled_data")]
        pub fn create_with_grouping_strategy(
            locale: &Locale,
            grouping_strategy: Option<FixedDecimalGroupingStrategy>,
        ) -> Result<Box<FixedDecimalFormatter>, DataError> {
            let prefs = FixedDecimalFormatterPreferences::from(&locale.0);

            let mut options = FixedDecimalFormatterOptions::default();
            options.grouping_strategy = grouping_strategy
                .map(Into::into)
                .unwrap_or(options.grouping_strategy);
            Ok(Box::new(FixedDecimalFormatter(
                icu_decimal::FixedDecimalFormatter::try_new(prefs, options)?,
            )))
        }

        /// Creates a new [`FixedDecimalFormatter`], using a particular data source.
        #[diplomat::rust_link(icu::decimal::FixedDecimalFormatter::try_new, FnInStruct)]
        #[diplomat::attr(supports = fallible_constructors, named_constructor = "with_grouping_strategy_and_provider")]
        #[diplomat::demo(default_constructor)]
        #[cfg(feature = "buffer_provider")]
        pub fn create_with_grouping_strategy_and_provider(
            provider: &DataProvider,
            locale: &Locale,
            grouping_strategy: Option<FixedDecimalGroupingStrategy>,
        ) -> Result<Box<FixedDecimalFormatter>, DataError> {
            let prefs = FixedDecimalFormatterPreferences::from(&locale.0);

            let mut options = FixedDecimalFormatterOptions::default();
            options.grouping_strategy = grouping_strategy
                .map(Into::into)
                .unwrap_or(options.grouping_strategy);
            Ok(Box::new(FixedDecimalFormatter(
                provider.call_constructor_custom_err(move |provider| {
                    icu_decimal::FixedDecimalFormatter::try_new_with_buffer_provider(
                        provider, prefs, options,
                    )
                })?,
            )))
        }

        /// Creates a new [`FixedDecimalFormatter`] from preconstructed locale data.
        #[diplomat::rust_link(icu::decimal::provider::DecimalSymbolsV2, Struct)]
        #[allow(clippy::too_many_arguments)]
        pub fn create_with_manual_data(
            plus_sign_prefix: &DiplomatStr,
            plus_sign_suffix: &DiplomatStr,
            minus_sign_prefix: &DiplomatStr,
            minus_sign_suffix: &DiplomatStr,
            decimal_separator: &DiplomatStr,
            grouping_separator: &DiplomatStr,
            primary_group_size: u8,
            secondary_group_size: u8,
            min_group_size: u8,
            digits: &[DiplomatChar],
            grouping_strategy: Option<FixedDecimalGroupingStrategy>,
        ) -> Result<Box<FixedDecimalFormatter>, DataError> {
            use alloc::borrow::Cow;
            use icu_provider::any::AsDowncastingAnyProvider;
            use icu_provider_adapters::{fixed::FixedProvider, fork::ForkByMarkerProvider};
            use zerovec::VarZeroCow;

            fn str_to_cow(s: &'_ diplomat_runtime::DiplomatStr) -> Cow<'_, str> {
                if s.is_empty() {
                    Cow::default()
                } else if let Ok(s) = core::str::from_utf8(s) {
                    Cow::Borrowed(s)
                } else {
                    Cow::Owned(alloc::string::String::from_utf8_lossy(s).into_owned())
                }
            }

            use icu_decimal::provider::{
                DecimalDigitsV1, DecimalDigitsV1Marker, DecimalSymbolStrsBuilder, DecimalSymbolsV2,
                DecimalSymbolsV2Marker, GroupingSizesV1,
            };
            let mut new_digits = ['\0'; 10];
            for (old, new) in digits
                .iter()
                .copied()
                .chain(core::iter::repeat(char::REPLACEMENT_CHARACTER as u32))
                .zip(new_digits.iter_mut())
            {
                *new = char::from_u32(old).unwrap_or(char::REPLACEMENT_CHARACTER);
            }
            let digits = new_digits;
            let strings = DecimalSymbolStrsBuilder {
                plus_sign_prefix: str_to_cow(plus_sign_prefix),
                plus_sign_suffix: str_to_cow(plus_sign_suffix),
                minus_sign_prefix: str_to_cow(minus_sign_prefix),
                minus_sign_suffix: str_to_cow(minus_sign_suffix),
                decimal_separator: str_to_cow(decimal_separator),
                grouping_separator: str_to_cow(grouping_separator),
                numsys: "zyyy".into(),
            };

            let grouping_sizes = GroupingSizesV1 {
                primary: primary_group_size,
                secondary: secondary_group_size,
                min_grouping: min_group_size,
            };

            let mut options = FixedDecimalFormatterOptions::default();
            options.grouping_strategy = grouping_strategy
                .map(Into::into)
                .unwrap_or(options.grouping_strategy);
            let provider_symbols =
                FixedProvider::<DecimalSymbolsV2Marker>::from_owned(DecimalSymbolsV2 {
                    strings: VarZeroCow::from_encodeable(&strings),
                    grouping_sizes,
                });
            let provider_digits =
                FixedProvider::<DecimalDigitsV1Marker>::from_owned(DecimalDigitsV1 { digits });
            let provider = ForkByMarkerProvider::new(provider_symbols, provider_digits);
            Ok(Box::new(FixedDecimalFormatter(
                icu_decimal::FixedDecimalFormatter::try_new_unstable(
                    &provider.as_downcasting(),
                    Default::default(),
                    options,
                )?,
            )))
        }

        /// Formats a [`SignedFixedDecimal`] to a string.
        #[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)]
        #[diplomat::rust_link(icu::decimal::FormattedFixedDecimal::to_string, FnInStruct, hidden)]
        pub fn format(
            &self,
            value: &SignedFixedDecimal,
            write: &mut diplomat_runtime::DiplomatWrite,
        ) {
            let _infallible = self.0.format(&value.0).write_to(write);
        }
    }
}