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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// 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 ).

use ffi::FixedDecimalSignedRoundingMode;

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

    use crate::errors::ffi::{FixedDecimalLimitError, FixedDecimalParseError};

    use writeable::Writeable;

    #[diplomat::opaque]
    #[diplomat::rust_link(fixed_decimal::FixedDecimal, Struct)]
    pub struct SignedFixedDecimal(pub fixed_decimal::SignedFixedDecimal);

    /// The sign of a FixedDecimal, as shown in formatting.
    #[diplomat::rust_link(fixed_decimal::Sign, Enum)]
    #[diplomat::enum_convert(fixed_decimal::Sign, needs_wildcard)]
    pub enum FixedDecimalSign {
        /// No sign (implicitly positive, e.g., 1729).
        None,
        /// A negative sign, e.g., -1729.
        Negative,
        /// An explicit positive sign, e.g., +1729.
        Positive,
    }

    /// ECMA-402 compatible sign display preference.
    #[diplomat::rust_link(fixed_decimal::SignDisplay, Enum)]
    #[diplomat::enum_convert(fixed_decimal::SignDisplay, needs_wildcard)]
    pub enum FixedDecimalSignDisplay {
        Auto,
        Never,
        Always,
        ExceptZero,
        Negative,
    }

    /// Increment used in a rounding operation.
    #[diplomat::rust_link(fixed_decimal::RoundingIncrement, Enum)]
    #[diplomat::enum_convert(fixed_decimal::RoundingIncrement, needs_wildcard)]
    pub enum FixedDecimalRoundingIncrement {
        MultiplesOf1,
        MultiplesOf2,
        MultiplesOf5,
        MultiplesOf25,
    }

    /// Mode used in a rounding operation for signed numbers.
    #[diplomat::rust_link(fixed_decimal::SignedRoundingMode, Enum)]
    pub enum FixedDecimalSignedRoundingMode {
        Expand,
        Trunc,
        HalfExpand,
        HalfTrunc,
        HalfEven,
        Ceil,
        Floor,
        HalfCeil,
        HalfFloor,
    }

    impl SignedFixedDecimal {
        /// Construct an [`SignedFixedDecimal`] from an integer.
        #[diplomat::rust_link(fixed_decimal::FixedDecimal, Struct)]
        #[diplomat::attr(dart, disable)]
        #[diplomat::attr(js, rename = "from_number")]
        #[diplomat::attr(supports = method_overloading, rename = "from")]
        #[diplomat::attr(auto, named_constructor)]
        pub fn from_int32(v: i32) -> Box<SignedFixedDecimal> {
            Box::new(SignedFixedDecimal(fixed_decimal::SignedFixedDecimal::from(
                v,
            )))
        }

        /// Construct an [`SignedFixedDecimal`] from an integer.
        #[diplomat::rust_link(fixed_decimal::FixedDecimal, Struct)]
        #[diplomat::attr(dart, disable)]
        #[diplomat::attr(js, disable)]
        #[diplomat::attr(supports = method_overloading, rename = "from")]
        #[diplomat::attr(supports = fallible_constructors, named_constructor)]
        pub fn from_uint32(v: u32) -> Box<SignedFixedDecimal> {
            Box::new(SignedFixedDecimal(fixed_decimal::SignedFixedDecimal::from(
                v,
            )))
        }

        /// Construct an [`SignedFixedDecimal`] from an integer.
        #[diplomat::rust_link(fixed_decimal::FixedDecimal, Struct)]
        #[diplomat::attr(dart, rename = "from_int")]
        #[diplomat::attr(js, rename = "from_big_int")]
        #[diplomat::attr(supports = method_overloading, rename = "from")]
        #[diplomat::attr(auto, named_constructor)]
        pub fn from_int64(v: i64) -> Box<SignedFixedDecimal> {
            Box::new(SignedFixedDecimal(fixed_decimal::SignedFixedDecimal::from(
                v,
            )))
        }

        /// Construct an [`SignedFixedDecimal`] from an integer.
        #[diplomat::rust_link(fixed_decimal::FixedDecimal, Struct)]
        #[diplomat::attr(any(dart, js), disable)]
        #[diplomat::attr(supports = method_overloading, rename = "from")]
        #[diplomat::attr(auto, named_constructor)]
        pub fn from_uint64(v: u64) -> Box<SignedFixedDecimal> {
            Box::new(SignedFixedDecimal(fixed_decimal::SignedFixedDecimal::from(
                v,
            )))
        }

        /// Construct an [`SignedFixedDecimal`] from an integer-valued float
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::try_from_f64, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FloatPrecision, Enum)]
        #[diplomat::rust_link(fixed_decimal::DoublePrecision, Enum, hidden)]
        #[diplomat::attr(any(dart, js), disable)]
        #[diplomat::attr(supports = fallible_constructors, named_constructor)]
        pub fn from_double_with_integer_precision(
            f: f64,
        ) -> Result<Box<SignedFixedDecimal>, FixedDecimalLimitError> {
            let precision = fixed_decimal::DoublePrecision::Integer;
            Ok(Box::new(SignedFixedDecimal(
                fixed_decimal::SignedFixedDecimal::try_from_f64(f, precision)?,
            )))
        }

        /// Construct an [`SignedFixedDecimal`] from an float, with a given power of 10 for the lower magnitude
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::try_from_f64, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FloatPrecision, Enum)]
        #[diplomat::rust_link(fixed_decimal::DoublePrecision, Enum, hidden)]
        #[diplomat::attr(js, rename = "from_number_with_lower_magnitude")]
        #[diplomat::attr(supports = fallible_constructors, named_constructor)]
        #[diplomat::demo(default_constructor)]
        pub fn from_double_with_lower_magnitude(
            f: f64,
            magnitude: i16,
        ) -> Result<Box<SignedFixedDecimal>, FixedDecimalLimitError> {
            let precision = fixed_decimal::DoublePrecision::Magnitude(magnitude);
            Ok(Box::new(SignedFixedDecimal(
                fixed_decimal::SignedFixedDecimal::try_from_f64(f, precision)?,
            )))
        }

        /// Construct an [`SignedFixedDecimal`] from an float, for a given number of significant digits
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::try_from_f64, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FloatPrecision, Enum)]
        #[diplomat::rust_link(fixed_decimal::DoublePrecision, Enum, hidden)]
        #[diplomat::attr(js, rename = "from_number_with_significant_digits")]
        #[diplomat::attr(supports = fallible_constructors, named_constructor)]
        pub fn from_double_with_significant_digits(
            f: f64,
            digits: u8,
        ) -> Result<Box<SignedFixedDecimal>, FixedDecimalLimitError> {
            let precision = fixed_decimal::DoublePrecision::SignificantDigits(digits);
            Ok(Box::new(SignedFixedDecimal(
                fixed_decimal::SignedFixedDecimal::try_from_f64(f, precision)?,
            )))
        }

        /// Construct an [`SignedFixedDecimal`] from an float, with enough digits to recover
        /// the original floating point in IEEE 754 without needing trailing zeros
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::try_from_f64, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FloatPrecision, Enum)]
        #[diplomat::rust_link(fixed_decimal::DoublePrecision, Enum, hidden)]
        #[diplomat::attr(js, rename = "from_number_with_round_trip_precision")]
        #[diplomat::attr(supports = fallible_constructors, named_constructor)]
        pub fn from_double_with_round_trip_precision(
            f: f64,
        ) -> Result<Box<SignedFixedDecimal>, FixedDecimalLimitError> {
            let precision = fixed_decimal::DoublePrecision::RoundTrip;
            Ok(Box::new(SignedFixedDecimal(
                fixed_decimal::SignedFixedDecimal::try_from_f64(f, precision)?,
            )))
        }

        /// Construct an [`SignedFixedDecimal`] from a string.
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::try_from_str, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::try_from_utf8, FnInStruct, hidden)]
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::from_str, FnInStruct, hidden)]
        #[diplomat::attr(supports = fallible_constructors, named_constructor)]
        pub fn from_string(
            v: &DiplomatStr,
        ) -> Result<Box<SignedFixedDecimal>, FixedDecimalParseError> {
            Ok(Box::new(SignedFixedDecimal(
                fixed_decimal::SignedFixedDecimal::try_from_utf8(v)?,
            )))
        }

        #[diplomat::rust_link(fixed_decimal::UnsignedFixedDecimal::digit_at, FnInStruct)]
        pub fn digit_at(&self, magnitude: i16) -> u8 {
            self.0.absolute.digit_at(magnitude)
        }

        #[diplomat::rust_link(fixed_decimal::UnsignedFixedDecimal::magnitude_range, FnInStruct)]
        #[diplomat::attr(auto, getter)]
        pub fn magnitude_start(&self) -> i16 {
            *self.0.absolute.magnitude_range().start()
        }

        #[diplomat::rust_link(fixed_decimal::UnsignedFixedDecimal::magnitude_range, FnInStruct)]
        #[diplomat::attr(auto, getter)]
        pub fn magnitude_end(&self) -> i16 {
            *self.0.absolute.magnitude_range().end()
        }

        #[diplomat::rust_link(
            fixed_decimal::UnsignedFixedDecimal::nonzero_magnitude_start,
            FnInStruct
        )]
        #[diplomat::attr(auto, getter)]
        pub fn nonzero_magnitude_start(&self) -> i16 {
            self.0.absolute.nonzero_magnitude_start()
        }

        #[diplomat::rust_link(
            fixed_decimal::UnsignedFixedDecimal::nonzero_magnitude_end,
            FnInStruct
        )]
        #[diplomat::attr(auto, getter)]
        pub fn nonzero_magnitude_end(&self) -> i16 {
            self.0.absolute.nonzero_magnitude_end()
        }

        #[diplomat::rust_link(fixed_decimal::UnsignedFixedDecimal::is_zero, FnInStruct)]
        #[diplomat::attr(auto, getter)]
        pub fn is_zero(&self) -> bool {
            self.0.absolute.is_zero()
        }

        /// Multiply the [`SignedFixedDecimal`] by a given power of ten.
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::multiply_pow10, FnInStruct)]
        #[diplomat::rust_link(
            fixed_decimal::SignedFixedDecimal::multiplied_pow10,
            FnInStruct,
            hidden
        )]
        pub fn multiply_pow10(&mut self, power: i16) {
            self.0.multiply_pow10(power)
        }

        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::sign, FnInStruct)]
        #[diplomat::attr(auto, getter)]
        pub fn sign(&self) -> FixedDecimalSign {
            self.0.sign().into()
        }

        /// Set the sign of the [`SignedFixedDecimal`].
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::set_sign, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::with_sign, FnInStruct, hidden)]
        #[diplomat::attr(auto, setter = "sign")]
        pub fn set_sign(&mut self, sign: FixedDecimalSign) {
            self.0.set_sign(sign.into())
        }

        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::apply_sign_display, FnInStruct)]
        #[diplomat::rust_link(
            fixed_decimal::SignedFixedDecimal::with_sign_display,
            FnInStruct,
            hidden
        )]
        pub fn apply_sign_display(&mut self, sign_display: FixedDecimalSignDisplay) {
            self.0.apply_sign_display(sign_display.into())
        }

        #[diplomat::rust_link(fixed_decimal::FixedDecimal::trim_start, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::trimmed_start, FnInStruct, hidden)]
        pub fn trim_start(&mut self) {
            self.0.absolute.trim_start()
        }

        #[diplomat::rust_link(fixed_decimal::FixedDecimal::trim_end, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::trimmed_end, FnInStruct, hidden)]
        pub fn trim_end(&mut self) {
            self.0.absolute.trim_end()
        }

        #[diplomat::rust_link(fixed_decimal::UnsignedFixedDecimal::trim_end_if_integer, FnInStruct)]
        #[diplomat::rust_link(
            fixed_decimal::UnsignedFixedDecimal::trimmed_end_if_integer,
            FnInStruct,
            hidden
        )]
        pub fn trim_end_if_integer(&mut self) {
            self.0.absolute.trim_end_if_integer()
        }

        /// Zero-pad the [`SignedFixedDecimal`] on the left to a particular position
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::pad_start, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::padded_start, FnInStruct, hidden)]
        pub fn pad_start(&mut self, position: i16) {
            self.0.absolute.pad_start(position)
        }

        /// Zero-pad the [`SignedFixedDecimal`] on the right to a particular position
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::pad_end, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::padded_end, FnInStruct, hidden)]
        pub fn pad_end(&mut self, position: i16) {
            self.0.absolute.pad_end(position)
        }

        /// Truncate the [`SignedFixedDecimal`] on the left to a particular position, deleting digits if necessary. This is useful for, e.g. abbreviating years
        /// ("2022" -> "22")
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::set_max_position, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::with_max_position, FnInStruct, hidden)]
        pub fn set_max_position(&mut self, position: i16) {
            self.0.absolute.set_max_position(position)
        }

        /// Round the number at a particular digit position.
        ///
        /// This uses half to even rounding, which resolves ties by selecting the nearest
        /// even integer to the original value.
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::round, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::rounded, FnInStruct, hidden)]
        pub fn round(&mut self, position: i16) {
            self.0.round(position)
        }

        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::ceil, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::ceiled, FnInStruct, hidden)]
        pub fn ceil(&mut self, position: i16) {
            self.0.ceil(position)
        }

        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::expand, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::expanded, FnInStruct, hidden)]
        pub fn expand(&mut self, position: i16) {
            self.0.expand(position)
        }

        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::floor, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::floored, FnInStruct, hidden)]
        pub fn floor(&mut self, position: i16) {
            self.0.floor(position)
        }

        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::trunc, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::trunced, FnInStruct, hidden)]
        pub fn trunc(&mut self, position: i16) {
            self.0.trunc(position)
        }

        #[diplomat::rust_link(fixed_decimal::SignedFixedDecimal::round_with_mode, FnInStruct)]
        #[diplomat::rust_link(
            fixed_decimal::SignedFixedDecimal::rounded_with_mode,
            FnInStruct,
            hidden
        )]
        pub fn round_with_mode(&mut self, position: i16, mode: FixedDecimalSignedRoundingMode) {
            self.0.round_with_mode(position, mode.into())
        }

        #[diplomat::rust_link(
            fixed_decimal::FixedDecimal::round_with_mode_and_increment,
            FnInStruct
        )]
        #[diplomat::rust_link(
            fixed_decimal::FixedDecimal::rounded_with_mode_and_increment,
            FnInStruct,
            hidden
        )]
        pub fn round_with_mode_and_increment(
            &mut self,
            position: i16,
            mode: FixedDecimalSignedRoundingMode,
            increment: FixedDecimalRoundingIncrement,
        ) {
            self.0
                .round_with_mode_and_increment(position, mode.into(), increment.into())
        }

        /// Concatenates `other` to the end of `self`.
        ///
        /// If successful, `other` will be set to 0 and a successful status is returned.
        ///
        /// If not successful, `other` will be unchanged and an error is returned.
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::concatenate_end, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::concatenated_end, FnInStruct, hidden)]
        pub fn concatenate_end(&mut self, other: &mut SignedFixedDecimal) -> Result<(), ()> {
            let x = core::mem::take(&mut other.0);
            self.0.absolute.concatenate_end(x.absolute).map_err(|y| {
                other.0.absolute = y;
            })
        }

        /// Format the [`SignedFixedDecimal`] as a string.
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::write_to, FnInStruct)]
        #[diplomat::rust_link(fixed_decimal::FixedDecimal::to_string, FnInStruct, hidden)]
        #[diplomat::attr(auto, stringifier)]
        pub fn to_string(&self, to: &mut diplomat_runtime::DiplomatWrite) {
            let _ = self.0.write_to(to);
        }
    }
}

impl From<FixedDecimalSignedRoundingMode> for fixed_decimal::SignedRoundingMode {
    fn from(mode: FixedDecimalSignedRoundingMode) -> Self {
        match mode {
            FixedDecimalSignedRoundingMode::Expand => fixed_decimal::SignedRoundingMode::Unsigned(
                fixed_decimal::UnsignedRoundingMode::Expand,
            ),
            FixedDecimalSignedRoundingMode::Trunc => fixed_decimal::SignedRoundingMode::Unsigned(
                fixed_decimal::UnsignedRoundingMode::Trunc,
            ),
            FixedDecimalSignedRoundingMode::HalfExpand => {
                fixed_decimal::SignedRoundingMode::Unsigned(
                    fixed_decimal::UnsignedRoundingMode::HalfExpand,
                )
            }
            FixedDecimalSignedRoundingMode::HalfTrunc => {
                fixed_decimal::SignedRoundingMode::Unsigned(
                    fixed_decimal::UnsignedRoundingMode::HalfTrunc,
                )
            }
            FixedDecimalSignedRoundingMode::HalfEven => {
                fixed_decimal::SignedRoundingMode::Unsigned(
                    fixed_decimal::UnsignedRoundingMode::HalfEven,
                )
            }
            FixedDecimalSignedRoundingMode::Ceil => fixed_decimal::SignedRoundingMode::Ceil,
            FixedDecimalSignedRoundingMode::Floor => fixed_decimal::SignedRoundingMode::Floor,
            FixedDecimalSignedRoundingMode::HalfCeil => fixed_decimal::SignedRoundingMode::HalfCeil,
            FixedDecimalSignedRoundingMode::HalfFloor => {
                fixed_decimal::SignedRoundingMode::HalfFloor
            }
        }
    }
}