windows_parser.rs (3024B)
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 use diplomat_runtime::DiplomatStr; 11 12 use crate::unstable::timezone::ffi::TimeZone; 13 #[cfg(feature = "buffer_provider")] 14 use crate::unstable::{errors::ffi::DataError, provider::ffi::DataProvider}; 15 16 /// A mapper between Windows time zone identifiers and BCP-47 time zone identifiers. 17 /// 18 /// This mapper supports two-way mapping, but it is optimized for the case of Windows to BCP-47. 19 /// It also supports normalizing and canonicalizing the Windows strings. 20 #[diplomat::opaque] 21 #[diplomat::rust_link(icu::time::zone::windows::WindowsParser, Struct)] 22 #[diplomat::rust_link(icu::time::zone::windows::WindowsParserBorrowed, Struct, hidden)] 23 #[diplomat::rust_link( 24 icu::time::zone::windows::WindowsParserBorrowed::new, 25 FnInStruct, 26 hidden 27 )] 28 pub struct WindowsParser(pub icu_time::zone::windows::WindowsParser); 29 30 impl WindowsParser { 31 /// Create a new [`WindowsParser`] using compiled data 32 #[diplomat::rust_link(icu::time::zone::windows::WindowsParser::new, FnInStruct)] 33 #[diplomat::attr(auto, constructor)] 34 #[cfg(feature = "compiled_data")] 35 pub fn create() -> Box<WindowsParser> { 36 Box::new(WindowsParser( 37 icu_time::zone::windows::WindowsParser::new().static_to_owned(), 38 )) 39 } 40 41 /// Create a new [`WindowsParser`] using a particular data source 42 #[diplomat::rust_link(icu::time::zone::windows::WindowsParser::new, FnInStruct)] 43 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")] 44 #[cfg(feature = "buffer_provider")] 45 pub fn create_with_provider( 46 provider: &DataProvider, 47 ) -> Result<Box<WindowsParser>, DataError> { 48 Ok(Box::new(WindowsParser( 49 icu_time::zone::windows::WindowsParser::try_new_with_buffer_provider( 50 provider.get()?, 51 )?, 52 ))) 53 } 54 55 #[diplomat::rust_link(icu::time::zone::windows::WindowsParserBorrowed::parse, FnInStruct)] 56 #[diplomat::rust_link( 57 icu::time::zone::windows::WindowsParserBorrowed::parse_from_utf8, 58 FnInStruct, 59 hidden 60 )] 61 pub fn parse(&self, value: &DiplomatStr, region: &DiplomatStr) -> Option<Box<TimeZone>> { 62 self.0 63 .as_borrowed() 64 .parse_from_utf8( 65 value, 66 Some(icu_locale_core::subtags::Region::try_from_utf8(region).ok()?), 67 ) 68 .map(TimeZone) 69 .map(Box::new) 70 } 71 } 72 }