measure_unit_parser.rs (3053B)
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 #[diplomat::abi_rename = "icu4x_{0}_mv1"] 7 #[diplomat::attr(auto, namespace = "icu4x")] 8 pub mod ffi { 9 use alloc::boxed::Box; 10 11 #[cfg(feature = "buffer_provider")] 12 use crate::unstable::errors::ffi::DataError; 13 #[cfg(feature = "buffer_provider")] 14 use crate::unstable::provider::ffi::DataProvider; 15 16 #[diplomat::opaque] 17 /// An ICU4X Measurement Unit object which represents a single unit of measurement 18 /// such as `meter`, `second`, `kilometer-per-hour`, `square-meter`, etc. 19 /// 20 /// You can create an instance of this object using [`MeasureUnitParser`] by calling the `parse` method. 21 #[diplomat::rust_link(icu::experimental::measure::measureunit::MeasureUnit, Struct)] 22 pub struct MeasureUnit(pub icu_experimental::measure::measureunit::MeasureUnit); 23 24 #[diplomat::opaque] 25 /// An ICU4X Measure Unit Parser object, capable of parsing the CLDR unit identifier (e.g. `meter-per-square-second`) and get the [`MeasureUnit`]. 26 #[diplomat::rust_link(icu::experimental::measure::parser::MeasureUnitParser, Struct)] 27 pub struct MeasureUnitParser(pub icu_experimental::measure::parser::MeasureUnitParser); 28 29 impl MeasureUnitParser { 30 /// Construct a new [`MeasureUnitParser`] instance using compiled data. 31 #[diplomat::rust_link( 32 icu::experimental::measure::parser::MeasureUnitParser::new, 33 FnInStruct 34 )] 35 #[diplomat::attr(auto, constructor)] 36 #[cfg(feature = "compiled_data")] 37 pub fn create() -> Box<MeasureUnitParser> { 38 Box::new(MeasureUnitParser( 39 icu_experimental::measure::parser::MeasureUnitParser::default(), 40 )) 41 } 42 /// Construct a new [`MeasureUnitParser`] instance using a particular data source. 43 #[diplomat::rust_link( 44 icu::experimental::measure::parser::MeasureUnitParser::new, 45 FnInStruct 46 )] 47 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")] 48 #[cfg(feature = "buffer_provider")] 49 pub fn create_with_provider( 50 provider: &DataProvider, 51 ) -> Result<Box<MeasureUnitParser>, DataError> { 52 Ok(Box::new(MeasureUnitParser( 53 icu_experimental::measure::parser::MeasureUnitParser::try_new_with_buffer_provider( 54 provider.get()?, 55 )?, 56 ))) 57 } 58 59 #[diplomat::rust_link( 60 icu::experimental::measure::parser::MeasureUnitParser::parse, 61 FnInStruct 62 )] 63 pub fn parse(&self, unit_id: &DiplomatStr) -> Option<Box<MeasureUnit>> { 64 self.0 65 .try_from_utf8(unit_id) 66 .ok() 67 .map(MeasureUnit) 68 .map(Box::new) 69 } 70 } 71 }