Coverage Report

Created: 2025-10-10 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/icu_capi-1.5.1/src/decimal.rs
Line
Count
Source
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
0
        pub fn create_with_grouping_strategy(
41
0
            provider: &ICU4XDataProvider,
42
0
            locale: &ICU4XLocale,
43
0
            grouping_strategy: ICU4XFixedDecimalGroupingStrategy,
44
0
        ) -> Result<Box<ICU4XFixedDecimalFormatter>, ICU4XError> {
45
0
            let locale = locale.to_datalocale();
46
47
0
            let grouping_strategy = match grouping_strategy {
48
0
                ICU4XFixedDecimalGroupingStrategy::Auto => GroupingStrategy::Auto,
49
0
                ICU4XFixedDecimalGroupingStrategy::Never => GroupingStrategy::Never,
50
0
                ICU4XFixedDecimalGroupingStrategy::Always => GroupingStrategy::Always,
51
0
                ICU4XFixedDecimalGroupingStrategy::Min2 => GroupingStrategy::Min2,
52
            };
53
0
            let mut options = FixedDecimalFormatterOptions::default();
54
0
            options.grouping_strategy = grouping_strategy;
55
0
            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
0
                &locale,
61
0
                options,
62
0
            )?)))
63
0
        }
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
0
        pub fn create_with_decimal_symbols_v1(
72
0
            data_struct: &ICU4XDataStruct,
73
0
            grouping_strategy: ICU4XFixedDecimalGroupingStrategy,
74
0
        ) -> Result<Box<ICU4XFixedDecimalFormatter>, ICU4XError> {
75
0
            let grouping_strategy = match grouping_strategy {
76
0
                ICU4XFixedDecimalGroupingStrategy::Auto => GroupingStrategy::Auto,
77
0
                ICU4XFixedDecimalGroupingStrategy::Never => GroupingStrategy::Never,
78
0
                ICU4XFixedDecimalGroupingStrategy::Always => GroupingStrategy::Always,
79
0
                ICU4XFixedDecimalGroupingStrategy::Min2 => GroupingStrategy::Min2,
80
            };
81
0
            let mut options = FixedDecimalFormatterOptions::default();
82
0
            options.grouping_strategy = grouping_strategy;
83
0
            Ok(Box::new(ICU4XFixedDecimalFormatter(
84
0
                FixedDecimalFormatter::try_new_with_any_provider(
85
0
                    &AnyPayloadProvider::from_any_payload::<DecimalSymbolsV1Marker>(
86
0
                        // Note: This clone is free, since cloning AnyPayload is free.
87
0
                        data_struct.0.clone(),
88
0
                    ),
89
0
                    &Default::default(),
90
0
                    options,
91
0
                )?,
92
            )))
93
0
        }
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
0
        pub fn format(
105
0
            &self,
106
0
            value: &ICU4XFixedDecimal,
107
0
            write: &mut diplomat_runtime::DiplomatWriteable,
108
0
        ) -> Result<(), ICU4XError> {
109
0
            self.0.format(&value.0).write_to(write)?;
110
0
            Ok(())
111
0
        }
112
    }
113
}