iana_parser.rs (9211B)
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 IANA 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 IANA to BCP-47. 19 /// It also supports normalizing and canonicalizing the IANA strings. 20 #[diplomat::opaque] 21 #[diplomat::rust_link(icu::time::zone::iana::IanaParser, Struct)] 22 #[diplomat::rust_link(icu::time::zone::iana::IanaParserBorrowed, Struct, hidden)] 23 #[diplomat::rust_link(icu::time::zone::iana::IanaParserBorrowed::new, FnInStruct, hidden)] 24 pub struct IanaParser(pub icu_time::zone::iana::IanaParser); 25 26 impl IanaParser { 27 /// Create a new [`IanaParser`] using compiled data 28 #[diplomat::rust_link(icu::time::zone::iana::IanaParser::new, FnInStruct)] 29 #[diplomat::attr(auto, constructor)] 30 #[cfg(feature = "compiled_data")] 31 pub fn create() -> Box<IanaParser> { 32 Box::new(IanaParser( 33 icu_time::zone::iana::IanaParser::new().static_to_owned(), 34 )) 35 } 36 37 /// Create a new [`IanaParser`] using a particular data source 38 #[diplomat::rust_link(icu::time::zone::iana::IanaParser::new, FnInStruct)] 39 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")] 40 #[cfg(feature = "buffer_provider")] 41 pub fn create_with_provider(provider: &DataProvider) -> Result<Box<IanaParser>, DataError> { 42 Ok(Box::new(IanaParser( 43 icu_time::zone::iana::IanaParser::try_new_with_buffer_provider(provider.get()?)?, 44 ))) 45 } 46 47 #[diplomat::rust_link(icu::time::zone::iana::IanaParserBorrowed::parse, FnInStruct)] 48 #[diplomat::rust_link( 49 icu::time::zone::iana::IanaParserBorrowed::parse_from_utf8, 50 FnInStruct, 51 hidden 52 )] 53 pub fn parse(&self, value: &DiplomatStr) -> Box<TimeZone> { 54 Box::new(TimeZone(self.0.as_borrowed().parse_from_utf8(value))) 55 } 56 57 #[diplomat::rust_link(icu::time::zone::iana::IanaParserBorrowed::iter, FnInStruct)] 58 pub fn iter<'a>(&'a self) -> Box<TimeZoneIterator<'a>> { 59 Box::new(TimeZoneIterator(self.0.as_borrowed().iter())) 60 } 61 } 62 63 #[diplomat::opaque] 64 #[diplomat::rust_link(icu::time::zone::iana::TimeZoneIter, Struct)] 65 pub struct TimeZoneIterator<'a>(icu_time::zone::iana::TimeZoneIter<'a>); 66 67 impl<'a> TimeZoneIterator<'a> { 68 #[diplomat::attr(auto, iterator)] 69 #[diplomat::rust_link(icu::time::zone::iana::TimeZoneIter::next, FnInStruct)] 70 pub fn next(&mut self) -> Option<Box<TimeZone>> { 71 Some(Box::new(TimeZone(self.0.next()?))) 72 } 73 } 74 75 /// A mapper between IANA time zone identifiers and BCP-47 time zone identifiers. 76 /// 77 /// This mapper supports two-way mapping, but it is optimized for the case of IANA to BCP-47. 78 /// It also supports normalizing and canonicalizing the IANA strings. 79 #[diplomat::opaque] 80 #[diplomat::rust_link(icu::time::zone::iana::IanaParserExtended, Struct)] 81 #[diplomat::rust_link(icu::time::zone::iana::IanaParserExtendedBorrowed, Struct, hidden)] 82 #[diplomat::rust_link( 83 icu::time::zone::iana::IanaParserExtendedBorrowed::new, 84 FnInStruct, 85 hidden 86 )] 87 pub struct IanaParserExtended( 88 pub icu_time::zone::iana::IanaParserExtended<icu_time::zone::iana::IanaParser>, 89 ); 90 91 impl IanaParserExtended { 92 /// Create a new [`IanaParserExtended`] using compiled data 93 #[diplomat::rust_link(icu::time::zone::iana::IanaParserExtended::new, FnInStruct)] 94 #[diplomat::rust_link( 95 icu::time::zone::iana::IanaParserExtended::try_new_with_parser, 96 FnInStruct, 97 hidden 98 )] 99 #[diplomat::attr(auto, constructor)] 100 #[cfg(feature = "compiled_data")] 101 pub fn create() -> Box<IanaParserExtended> { 102 Box::new(IanaParserExtended( 103 icu_time::zone::iana::IanaParserExtended::new().static_to_owned(), 104 )) 105 } 106 107 /// Create a new [`IanaParserExtended`] using a particular data source 108 #[diplomat::rust_link(icu::time::zone::iana::IanaParserExtended::new, FnInStruct)] 109 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")] 110 #[cfg(feature = "buffer_provider")] 111 pub fn create_with_provider( 112 provider: &DataProvider, 113 ) -> Result<Box<IanaParserExtended>, DataError> { 114 Ok(Box::new(IanaParserExtended( 115 icu_time::zone::iana::IanaParserExtended::try_new_with_buffer_provider( 116 provider.get()?, 117 )?, 118 ))) 119 } 120 121 #[diplomat::rust_link(icu::time::zone::iana::IanaParserExtendedBorrowed::parse, FnInStruct)] 122 #[diplomat::rust_link( 123 icu::time::zone::iana::IanaParserExtendedBorrowed::parse_from_utf8, 124 FnInStruct, 125 hidden 126 )] 127 pub fn parse<'a>(&'a self, value: &DiplomatStr) -> TimeZoneAndCanonicalAndNormalized<'a> { 128 let icu_time::zone::iana::TimeZoneAndCanonicalAndNormalized { 129 time_zone, 130 canonical, 131 normalized, 132 .. 133 } = self.0.as_borrowed().parse_from_utf8(value); 134 TimeZoneAndCanonicalAndNormalized { 135 time_zone: Box::new(TimeZone(time_zone)), 136 canonical: canonical.into(), 137 normalized: normalized.into(), 138 } 139 } 140 141 #[diplomat::rust_link(icu::time::zone::iana::IanaParserExtendedBorrowed::iter, FnInStruct)] 142 pub fn iter<'a>(&'a self) -> Box<TimeZoneAndCanonicalIterator<'a>> { 143 Box::new(TimeZoneAndCanonicalIterator(self.0.as_borrowed().iter())) 144 } 145 146 #[diplomat::rust_link( 147 icu::time::zone::iana::IanaParserExtendedBorrowed::iter_all, 148 FnInStruct 149 )] 150 pub fn iter_all<'a>(&'a self) -> Box<TimeZoneAndCanonicalAndNormalizedIterator<'a>> { 151 Box::new(TimeZoneAndCanonicalAndNormalizedIterator( 152 self.0.as_borrowed().iter_all(), 153 )) 154 } 155 } 156 157 #[diplomat::out] 158 #[diplomat::rust_link(icu::time::zone::iana::TimeZoneAndCanonical, Struct)] 159 pub struct TimeZoneAndCanonical<'a> { 160 time_zone: Box<TimeZone>, 161 canonical: DiplomatUtf8StrSlice<'a>, 162 } 163 164 #[diplomat::opaque] 165 #[diplomat::rust_link(icu::time::zone::iana::TimeZoneAndCanonicalIter, Struct)] 166 pub struct TimeZoneAndCanonicalIterator<'a>(icu_time::zone::iana::TimeZoneAndCanonicalIter<'a>); 167 168 impl<'a> TimeZoneAndCanonicalIterator<'a> { 169 #[diplomat::attr(auto, iterator)] 170 #[diplomat::rust_link(icu::time::zone::iana::TimeZoneAndCanonicalIter::next, FnInStruct)] 171 pub fn next(&mut self) -> Option<TimeZoneAndCanonical<'a>> { 172 let icu_time::zone::iana::TimeZoneAndCanonical { 173 time_zone, 174 canonical, 175 .. 176 } = self.0.next()?; 177 Some(TimeZoneAndCanonical { 178 time_zone: Box::new(TimeZone(time_zone)), 179 canonical: canonical.into(), 180 }) 181 } 182 } 183 184 #[diplomat::out] 185 #[diplomat::rust_link(icu::time::zone::iana::TimeZoneAndCanonicalAndNormalized, Struct)] 186 pub struct TimeZoneAndCanonicalAndNormalized<'a> { 187 time_zone: Box<TimeZone>, 188 canonical: DiplomatUtf8StrSlice<'a>, 189 normalized: DiplomatUtf8StrSlice<'a>, 190 } 191 192 #[diplomat::opaque] 193 #[diplomat::rust_link(icu::time::zone::iana::TimeZoneAndCanonicalAndNormalizedIter, Struct)] 194 pub struct TimeZoneAndCanonicalAndNormalizedIterator<'a>( 195 icu_time::zone::iana::TimeZoneAndCanonicalAndNormalizedIter<'a>, 196 ); 197 198 impl<'a> TimeZoneAndCanonicalAndNormalizedIterator<'a> { 199 #[diplomat::attr(auto, iterator)] 200 #[diplomat::rust_link( 201 icu::time::zone::iana::TimeZoneAndCanonicalAndNormalizedIter::next, 202 FnInStruct 203 )] 204 pub fn next(&mut self) -> Option<TimeZoneAndCanonicalAndNormalized<'a>> { 205 let icu_time::zone::iana::TimeZoneAndCanonicalAndNormalized { 206 time_zone, 207 canonical, 208 normalized, 209 .. 210 } = self.0.next()?; 211 Some(TimeZoneAndCanonicalAndNormalized { 212 time_zone: Box::new(TimeZone(time_zone)), 213 canonical: canonical.into(), 214 normalized: normalized.into(), 215 }) 216 } 217 } 218 }