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
// 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(feature = "buffer_provider")]
    use crate::errors::ffi::DataError;
    #[cfg(feature = "buffer_provider")]
    use crate::provider::ffi::DataProvider;

    #[diplomat::opaque]
    /// An ICU4X Units Converter Factory object, capable of creating converters a [`UnitsConverter`]
    /// for converting between two [`MeasureUnit`]s.
    ///
    /// Also, it can parse the CLDR unit identifier (e.g. `meter-per-square-second`) and get the [`MeasureUnit`].
    #[diplomat::rust_link(icu::experimental::units::converter_factory::ConverterFactory, Struct)]
    pub struct UnitsConverterFactory(
        pub icu_experimental::units::converter_factory::ConverterFactory,
    );

    impl UnitsConverterFactory {
        /// Construct a new [`UnitsConverterFactory`] instance using compiled data.
        #[diplomat::rust_link(
            icu::experimental::units::converter_factory::ConverterFactory::new,
            FnInStruct
        )]
        #[diplomat::attr(auto, constructor)]
        #[cfg(feature = "compiled_data")]
        pub fn create() -> Box<UnitsConverterFactory> {
            Box::new(UnitsConverterFactory(
                icu_experimental::units::converter_factory::ConverterFactory::new(),
            ))
        }
        /// Construct a new [`UnitsConverterFactory`] instance using a particular data source.
        #[diplomat::rust_link(
            icu::experimental::units::converter_factory::ConverterFactory::new,
            FnInStruct
        )]
        #[diplomat::attr(auto, named_constructor = "with_provider")]
        #[cfg(feature = "buffer_provider")]
        pub fn create_with_provider(
            provider: &DataProvider,
        ) -> Result<Box<UnitsConverterFactory>, DataError> {
            Ok(Box::new(UnitsConverterFactory(provider.call_constructor(icu_experimental::units::converter_factory::ConverterFactory::try_new_with_buffer_provider)?)))
        }
        /// Creates a new [`UnitsConverter`] from the input and output [`MeasureUnit`]s.
        /// Returns nothing if the conversion between the two units is not possible.
        /// For example, conversion between `meter` and `second` is not possible.
        #[diplomat::rust_link(
            icu::experimental::units::converter_factory::ConverterFactory::converter,
            FnInStruct
        )]
        pub fn converter(
            &self,
            from: &MeasureUnit,
            to: &MeasureUnit,
        ) -> Option<Box<UnitsConverter>> {
            self.0
                .converter(&from.0, &to.0)
                .map(UnitsConverter)
                .map(Box::new)
        }

        /// Creates a parser to parse the CLDR unit identifier (e.g. `meter-per-square-second`) and get the [`MeasureUnit`].
        #[diplomat::rust_link(
            icu::experimental::units::converter_factory::ConverterFactory::parser,
            FnInStruct
        )]
        pub fn parser<'a>(&'a self) -> Box<MeasureUnitParser<'a>> {
            MeasureUnitParser(self.0.parser()).into()
        }
    }

    #[diplomat::opaque]
    /// An ICU4X Measurement Unit parser object which is capable of parsing the CLDR unit identifier
    /// (e.g. `meter-per-square-second`) and get the [`MeasureUnit`].
    #[diplomat::rust_link(icu::experimental::measure::parser::MeasureUnitParser, Struct)]
    pub struct MeasureUnitParser<'a>(pub icu_experimental::measure::parser::MeasureUnitParser<'a>);

    impl<'a> MeasureUnitParser<'a> {
        /// Parses the CLDR unit identifier (e.g. `meter-per-square-second`) and returns the corresponding [`MeasureUnit`],
        /// if the identifier is valid.
        #[diplomat::rust_link(
            icu::experimental::measure::parser::MeasureUnitParser::parse,
            FnInStruct
        )]
        pub fn parse(&self, unit_id: &DiplomatStr) -> Option<Box<MeasureUnit>> {
            self.0
                .try_from_utf8(unit_id)
                .ok()
                .map(MeasureUnit)
                .map(Box::new)
        }
    }

    #[diplomat::opaque]
    /// An ICU4X Measurement Unit object which represents a single unit of measurement
    /// such as `meter`, `second`, `kilometer-per-hour`, `square-meter`, etc.
    ///
    /// You can create an instance of this object using [`MeasureUnitParser`] by calling the `parse_measure_unit` method.
    #[diplomat::rust_link(icu::experimental::measure::measureunit::MeasureUnit, Struct)]
    pub struct MeasureUnit(pub icu_experimental::measure::measureunit::MeasureUnit);

    #[diplomat::opaque]
    /// An ICU4X Units Converter object, capable of converting between two [`MeasureUnit`]s.
    ///
    /// You can create an instance of this object using [`UnitsConverterFactory`] by calling the `converter` method.
    #[diplomat::rust_link(icu::experimental::units::converter::UnitsConverter, Struct)]
    pub struct UnitsConverter(pub icu_experimental::units::converter::UnitsConverter<f64>);
    impl UnitsConverter {
        /// Converts the input value from the input unit to the output unit (that have been used to create this converter).
        /// NOTE:
        ///   The conversion using floating-point operations is not as accurate as the conversion using ratios.
        #[diplomat::rust_link(
            icu::experimental::units::converter::UnitsConverter::convert,
            FnInStruct
        )]
        #[diplomat::attr(supports = method_overloading, rename = "convert")]
        #[diplomat::attr(js, rename = "convert_number")]
        pub fn convert_double(&self, value: f64) -> f64 {
            self.0.convert(&value)
        }

        /// Clones the current [`UnitsConverter`] object.
        #[diplomat::rust_link(
            icu::experimental::units::converter::UnitsConverter::clone,
            FnInStruct
        )]
        pub fn clone(&self) -> Box<Self> {
            Box::new(UnitsConverter(self.0.clone()))
        }
    }
}