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 ).
7 use crate::errors::ffi::ICU4XError;
8 use crate::provider::ffi::ICU4XDataProvider;
11 TimeZoneBcp47Id, TimeZoneIdMapper, TimeZoneIdMapperWithFastCanonicalization,
13 use tinystr::TinyAsciiStr;
15 /// A mapper between IANA time zone identifiers and BCP-47 time zone identifiers.
17 /// This mapper supports two-way mapping, but it is optimized for the case of IANA to BCP-47.
18 /// It also supports normalizing and canonicalizing the IANA strings.
20 #[diplomat::rust_link(icu::timezone::TimeZoneIdMapper, Struct)]
21 #[diplomat::rust_link(icu::timezone::TimeZoneIdMapper::as_borrowed, FnInStruct, hidden)]
22 #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperBorrowed, Struct, hidden)]
23 #[diplomat::rust_link(icu::timezone::NormalizedIana, Struct, hidden)]
24 pub struct ICU4XTimeZoneIdMapper(pub TimeZoneIdMapper);
26 impl ICU4XTimeZoneIdMapper {
27 #[diplomat::rust_link(icu::timezone::TimeZoneIdMapper::new, FnInStruct)]
28 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
30 provider: &ICU4XDataProvider,
31 ) -> Result<Box<ICU4XTimeZoneIdMapper>, ICU4XError> {
32 Ok(Box::new(ICU4XTimeZoneIdMapper(call_constructor!(
33 TimeZoneIdMapper::new [r => Ok(r)],
34 TimeZoneIdMapper::try_new_with_any_provider,
35 TimeZoneIdMapper::try_new_with_buffer_provider,
40 #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperBorrowed::iana_to_bcp47, FnInStruct)]
41 #[diplomat::rust_link(
42 icu::timezone::TimeZoneIdMapperBorrowed::iana_bytes_to_bcp47,
49 write: &mut diplomat_runtime::DiplomatWriteable,
50 ) -> Result<(), ICU4XError> {
51 use writeable::Writeable;
52 let handle = self.0.as_borrowed();
53 if let Some(s) = handle.iana_bytes_to_bcp47(value) {
54 Ok(s.0.write_to(write)?)
56 Err(ICU4XError::TimeZoneInvalidIdError)
60 #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperBorrowed::normalize_iana, FnInStruct)]
61 pub fn normalize_iana(
64 write: &mut diplomat_runtime::DiplomatWriteable,
65 ) -> Result<(), ICU4XError> {
66 use writeable::Writeable;
67 let handle = self.0.as_borrowed();
68 // Validate the UTF-8 here because it gets echoed back to the writeable
69 let value = core::str::from_utf8(value)?;
70 if let Some(s) = handle.normalize_iana(value) {
71 Ok(s.0.write_to(write)?)
73 Err(ICU4XError::TimeZoneInvalidIdError)
77 #[diplomat::rust_link(
78 icu::timezone::TimeZoneIdMapperBorrowed::canonicalize_iana,
81 pub fn canonicalize_iana(
84 write: &mut diplomat_runtime::DiplomatWriteable,
85 ) -> Result<(), ICU4XError> {
86 use writeable::Writeable;
87 let handle = self.0.as_borrowed();
88 // Validate the UTF-8 here because it gets echoed back to the writeable
89 let value = core::str::from_utf8(value)?;
90 if let Some(s) = handle.canonicalize_iana(value) {
91 Ok(s.0.write_to(write)?)
93 Err(ICU4XError::TimeZoneInvalidIdError)
97 #[diplomat::rust_link(
98 icu::timezone::TimeZoneIdMapperBorrowed::find_canonical_iana_from_bcp47,
101 pub fn find_canonical_iana_from_bcp47(
104 write: &mut diplomat_runtime::DiplomatWriteable,
105 ) -> Result<(), ICU4XError> {
106 use writeable::Writeable;
107 let handle = self.0.as_borrowed();
108 let bcp47_id = TimeZoneBcp47Id(
109 TinyAsciiStr::from_bytes(value).map_err(|_| ICU4XError::TimeZoneInvalidIdError)?,
111 if let Some(s) = handle.find_canonical_iana_from_bcp47(bcp47_id) {
112 Ok(s.write_to(write)?)
114 Err(ICU4XError::TimeZoneInvalidIdError)
119 /// A mapper between IANA time zone identifiers and BCP-47 time zone identifiers.
121 /// This mapper supports two-way mapping, but it is optimized for the case of IANA to BCP-47.
122 /// It also supports normalizing and canonicalizing the IANA strings.
124 #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperWithFastCanonicalization, Struct)]
125 #[diplomat::rust_link(
126 icu::timezone::TimeZoneIdMapperWithFastCanonicalization::as_borrowed,
130 #[diplomat::rust_link(
131 icu::timezone::TimeZoneIdMapperWithFastCanonicalization::inner,
135 #[diplomat::rust_link(
136 icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed,
140 #[diplomat::rust_link(
141 icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed::inner,
145 pub struct ICU4XTimeZoneIdMapperWithFastCanonicalization(
146 pub TimeZoneIdMapperWithFastCanonicalization<TimeZoneIdMapper>,
149 impl ICU4XTimeZoneIdMapperWithFastCanonicalization {
150 #[diplomat::rust_link(
151 icu::timezone::TimeZoneIdMapperWithFastCanonicalization::new,
154 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
156 provider: &ICU4XDataProvider,
157 ) -> Result<Box<ICU4XTimeZoneIdMapperWithFastCanonicalization>, ICU4XError> {
158 Ok(Box::new(ICU4XTimeZoneIdMapperWithFastCanonicalization(
160 TimeZoneIdMapperWithFastCanonicalization::new [r => Ok(r)],
161 TimeZoneIdMapperWithFastCanonicalization::try_new_with_any_provider,
162 TimeZoneIdMapperWithFastCanonicalization::try_new_with_buffer_provider,
168 #[diplomat::rust_link(
169 icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed::canonicalize_iana,
172 pub fn canonicalize_iana(
175 write: &mut diplomat_runtime::DiplomatWriteable,
176 ) -> Result<(), ICU4XError> {
177 use writeable::Writeable;
178 let handle = self.0.as_borrowed();
179 // Validate the UTF-8 here because it gets echoed back to the writeable
180 let value = core::str::from_utf8(value)?;
181 if let Some(s) = handle.canonicalize_iana(value) {
182 Ok(s.0.write_to(write)?)
184 Err(ICU4XError::TimeZoneInvalidIdError)
188 #[diplomat::rust_link(
189 icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed::canonical_iana_from_bcp47,
192 pub fn canonical_iana_from_bcp47(
195 write: &mut diplomat_runtime::DiplomatWriteable,
196 ) -> Result<(), ICU4XError> {
197 use writeable::Writeable;
198 let handle = self.0.as_borrowed();
199 let bcp47_id = TimeZoneBcp47Id(
200 TinyAsciiStr::from_bytes(value).map_err(|_| ICU4XError::TimeZoneInvalidIdError)?,
202 if let Some(s) = handle.canonical_iana_from_bcp47(bcp47_id) {
203 Ok(s.write_to(write)?)
205 Err(ICU4XError::TimeZoneInvalidIdError)