Bug 1910110 - Return early when channel URI is void r=rpl a=Aryx
[gecko.git] / intl / icu_capi / src / timezone_mapper.rs
blobcd0d3d7f9a165840568280f429f17957569900e3
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 ).
5 #[diplomat::bridge]
6 pub mod ffi {
7     use crate::errors::ffi::ICU4XError;
8     use crate::provider::ffi::ICU4XDataProvider;
9     use alloc::boxed::Box;
10     use icu_timezone::{
11         TimeZoneBcp47Id, TimeZoneIdMapper, TimeZoneIdMapperWithFastCanonicalization,
12     };
13     use tinystr::TinyAsciiStr;
15     /// A mapper between IANA time zone identifiers and BCP-47 time zone identifiers.
16     ///
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.
19     #[diplomat::opaque]
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)]
29         pub fn create(
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,
36                 provider,
37             )?)))
38         }
40         #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperBorrowed::iana_to_bcp47, FnInStruct)]
41         #[diplomat::rust_link(
42             icu::timezone::TimeZoneIdMapperBorrowed::iana_bytes_to_bcp47,
43             FnInStruct,
44             hidden
45         )]
46         pub fn iana_to_bcp47(
47             &self,
48             value: &DiplomatStr,
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)?)
55             } else {
56                 Err(ICU4XError::TimeZoneInvalidIdError)
57             }
58         }
60         #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperBorrowed::normalize_iana, FnInStruct)]
61         pub fn normalize_iana(
62             &self,
63             value: &DiplomatStr,
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)?)
72             } else {
73                 Err(ICU4XError::TimeZoneInvalidIdError)
74             }
75         }
77         #[diplomat::rust_link(
78             icu::timezone::TimeZoneIdMapperBorrowed::canonicalize_iana,
79             FnInStruct
80         )]
81         pub fn canonicalize_iana(
82             &self,
83             value: &DiplomatStr,
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)?)
92             } else {
93                 Err(ICU4XError::TimeZoneInvalidIdError)
94             }
95         }
97         #[diplomat::rust_link(
98             icu::timezone::TimeZoneIdMapperBorrowed::find_canonical_iana_from_bcp47,
99             FnInStruct
100         )]
101         pub fn find_canonical_iana_from_bcp47(
102             &self,
103             value: &DiplomatStr,
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)?,
110             );
111             if let Some(s) = handle.find_canonical_iana_from_bcp47(bcp47_id) {
112                 Ok(s.write_to(write)?)
113             } else {
114                 Err(ICU4XError::TimeZoneInvalidIdError)
115             }
116         }
117     }
119     /// A mapper between IANA time zone identifiers and BCP-47 time zone identifiers.
120     ///
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.
123     #[diplomat::opaque]
124     #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperWithFastCanonicalization, Struct)]
125     #[diplomat::rust_link(
126         icu::timezone::TimeZoneIdMapperWithFastCanonicalization::as_borrowed,
127         FnInStruct,
128         hidden
129     )]
130     #[diplomat::rust_link(
131         icu::timezone::TimeZoneIdMapperWithFastCanonicalization::inner,
132         FnInStruct,
133         hidden
134     )]
135     #[diplomat::rust_link(
136         icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed,
137         Struct,
138         hidden
139     )]
140     #[diplomat::rust_link(
141         icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed::inner,
142         FnInStruct,
143         hidden
144     )]
145     pub struct ICU4XTimeZoneIdMapperWithFastCanonicalization(
146         pub TimeZoneIdMapperWithFastCanonicalization<TimeZoneIdMapper>,
147     );
149     impl ICU4XTimeZoneIdMapperWithFastCanonicalization {
150         #[diplomat::rust_link(
151             icu::timezone::TimeZoneIdMapperWithFastCanonicalization::new,
152             FnInStruct
153         )]
154         #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
155         pub fn create(
156             provider: &ICU4XDataProvider,
157         ) -> Result<Box<ICU4XTimeZoneIdMapperWithFastCanonicalization>, ICU4XError> {
158             Ok(Box::new(ICU4XTimeZoneIdMapperWithFastCanonicalization(
159                 call_constructor!(
160                     TimeZoneIdMapperWithFastCanonicalization::new [r => Ok(r)],
161                     TimeZoneIdMapperWithFastCanonicalization::try_new_with_any_provider,
162                     TimeZoneIdMapperWithFastCanonicalization::try_new_with_buffer_provider,
163                     provider,
164                 )?,
165             )))
166         }
168         #[diplomat::rust_link(
169             icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed::canonicalize_iana,
170             FnInStruct
171         )]
172         pub fn canonicalize_iana(
173             &self,
174             value: &DiplomatStr,
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)?)
183             } else {
184                 Err(ICU4XError::TimeZoneInvalidIdError)
185             }
186         }
188         #[diplomat::rust_link(
189             icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed::canonical_iana_from_bcp47,
190             FnInStruct
191         )]
192         pub fn canonical_iana_from_bcp47(
193             &self,
194             value: &DiplomatStr,
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)?,
201             );
202             if let Some(s) = handle.canonical_iana_from_bcp47(bcp47_id) {
203                 Ok(s.write_to(write)?)
204             } else {
205                 Err(ICU4XError::TimeZoneInvalidIdError)
206             }
207         }
208     }