| 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 crate::{errors::ffi::ICU4XError, provider::ffi::ICU4XDataProvider}; |
| 8 |
use alloc::boxed::Box; |
| 9 |
use diplomat_runtime::DiplomatStr; |
| 10 |
use icu_experimental::units::converter::UnitsConverter; |
| 11 |
use icu_experimental::units::converter_factory::ConverterFactory; |
| 12 |
use icu_experimental::units::measureunit::MeasureUnit; |
| 13 |
use icu_experimental::units::measureunit::MeasureUnitParser; |
| 14 |
|
| 15 |
#[diplomat::opaque] |
| 16 |
/// An ICU4X Units Converter Factory object, capable of creating converters a [`ICU4XUnitsConverter`] |
| 17 |
/// for converting between two [`ICU4XMeasureUnit`]s. |
| 18 |
/// Also, it can parse the CLDR unit identifier (e.g. `meter-per-square-second`) and get the [`ICU4XMeasureUnit`]. |
| 19 |
#[diplomat::rust_link(icu::experimental::units::converter_factory::ConverterFactory, Struct)] |
| 20 |
pub struct ICU4XUnitsConverterFactory(pub ConverterFactory); |
| 21 |
|
| 22 |
impl ICU4XUnitsConverterFactory { |
| 23 |
/// Construct a new [`ICU4XUnitsConverterFactory`] instance. |
| 24 |
#[diplomat::rust_link( |
| 25 |
icu::experimental::units::converter_factory::ConverterFactory::new, |
| 26 |
FnInStruct |
| 27 |
)] |
| 28 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
| 29 |
pub fn create( |
| 30 |
provider: &ICU4XDataProvider, |
| 31 |
) -> Result<Box<ICU4XUnitsConverterFactory>, ICU4XError> { |
| 32 |
Ok(Box::new(ICU4XUnitsConverterFactory(call_constructor!( |
| 33 |
ConverterFactory::new [r => Ok(r)], |
| 34 |
ConverterFactory::try_new_with_any_provider, |
| 35 |
ConverterFactory::try_new_with_buffer_provider, |
| 36 |
provider, |
| 37 |
)?))) |
| 38 |
} |
| 39 |
|
| 40 |
/// Creates a new [`ICU4XUnitsConverter`] from the input and output [`ICU4XMeasureUnit`]s. |
| 41 |
/// Returns nothing if the conversion between the two units is not possible. |
| 42 |
/// For example, conversion between `meter` and `second` is not possible. |
| 43 |
#[diplomat::rust_link( |
| 44 |
icu::experimental::units::converter_factory::ConverterFactory::converter, |
| 45 |
FnInStruct |
| 46 |
)] |
| 47 |
pub fn converter( |
| 48 |
&self, |
| 49 |
from: &ICU4XMeasureUnit, |
| 50 |
to: &ICU4XMeasureUnit, |
| 51 |
) -> Option<Box<ICU4XUnitsConverter>> { |
| 52 |
let converter: Option<UnitsConverter<f64>> = self.0.converter(&from.0, &to.0); |
| 53 |
Some(ICU4XUnitsConverter(converter?).into()) |
| 54 |
} |
| 55 |
|
| 56 |
/// Creates a parser to parse the CLDR unit identifier (e.g. `meter-per-square-second`) and get the [`ICU4XMeasureUnit`]. |
| 57 |
#[diplomat::rust_link( |
| 58 |
icu::experimental::units::converter_factory::ConverterFactory::parser, |
| 59 |
FnInStruct |
| 60 |
)] |
| 61 |
pub fn parser<'a>(&'a self) -> Box<ICU4XMeasureUnitParser<'a>> { |
| 62 |
ICU4XMeasureUnitParser(self.0.parser()).into() |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[diplomat::opaque] |
| 67 |
/// An ICU4X Measurement Unit parser object which is capable of parsing the CLDR unit identifier |
| 68 |
/// (e.g. `meter-per-square-second`) and get the [`ICU4XMeasureUnit`]. |
| 69 |
#[diplomat::rust_link(icu::experimental::units::measureunit::MeasureUnitParser, Struct)] |
| 70 |
pub struct ICU4XMeasureUnitParser<'a>(pub MeasureUnitParser<'a>); |
| 71 |
|
| 72 |
impl<'a> ICU4XMeasureUnitParser<'a> { |
| 73 |
/// Parses the CLDR unit identifier (e.g. `meter-per-square-second`) and returns the corresponding [`ICU4XMeasureUnit`]. |
| 74 |
/// Returns an error if the unit identifier is not valid. |
| 75 |
#[diplomat::rust_link( |
| 76 |
icu::experimental::units::measureunit::MeasureUnitParser::parse, |
| 77 |
FnInStruct |
| 78 |
)] |
| 79 |
pub fn parse(&self, unit_id: &DiplomatStr) -> Result<Box<ICU4XMeasureUnit>, ICU4XError> { |
| 80 |
Ok(Box::new(ICU4XMeasureUnit(self.0.try_from_bytes(unit_id)?))) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
#[diplomat::opaque] |
| 85 |
/// An ICU4X Measurement Unit object which represents a single unit of measurement |
| 86 |
/// such as `meter`, `second`, `kilometer-per-hour`, `square-meter`, etc. |
| 87 |
/// |
| 88 |
/// You can create an instance of this object using [`ICU4XMeasureUnitParser`] by calling the `parse_measure_unit` method. |
| 89 |
#[diplomat::rust_link(icu::experimental::units::measureunit::MeasureUnit, Struct)] |
| 90 |
pub struct ICU4XMeasureUnit(pub MeasureUnit); |
| 91 |
|
| 92 |
#[diplomat::opaque] |
| 93 |
/// An ICU4X Units Converter object, capable of converting between two [`ICU4XMeasureUnit`]s. |
| 94 |
/// |
| 95 |
/// You can create an instance of this object using [`ICU4XUnitsConverterFactory`] by calling the `converter` method. |
| 96 |
#[diplomat::rust_link(icu::experimental::units::converter::UnitsConverter, Struct)] |
| 97 |
pub struct ICU4XUnitsConverter(pub UnitsConverter<f64>); |
| 98 |
impl ICU4XUnitsConverter { |
| 99 |
/// Converts the input value in float from the input unit to the output unit (that have been used to create this converter). |
| 100 |
/// NOTE: |
| 101 |
/// The conversion using floating-point operations is not as accurate as the conversion using ratios. |
| 102 |
#[diplomat::rust_link( |
| 103 |
icu::experimental::units::converter::UnitsConverter::convert, |
| 104 |
FnInStruct |
| 105 |
)] |
| 106 |
#[diplomat::attr(dart, rename = "convert_double")] |
| 107 |
pub fn convert_f64(&self, value: f64) -> f64 { |
| 108 |
self.0.convert(&value) |
| 109 |
} |
| 110 |
|
| 111 |
/// Clones the current [`ICU4XUnitsConverter`] object. |
| 112 |
#[diplomat::rust_link( |
| 113 |
icu::experimental::units::converter::UnitsConverter::clone, |
| 114 |
FnInStruct |
| 115 |
)] |
| 116 |
pub fn clone(&self) -> Box<Self> { |
| 117 |
Box::new(ICU4XUnitsConverter(self.0.clone())) |
| 118 |
} |
| 119 |
} |
| 120 |
} |