1#[rustfmt::skip]
8#[diplomat::bridge]
9#[diplomat::abi_rename = "icu4x_{0}_mv1"]
10#[diplomat::attr(auto, namespace = "icu4x")]
11pub mod ffi {
12 use alloc::boxed::Box;
13 use writeable::TryWriteable;
14
15 #[allow(unused_imports)]
16 use crate::{
17 date_formatter::ffi::{DateFormatter, DateFormatterGregorian},
18 date_time_formatter::ffi::{DateTimeFormatter, DateTimeFormatterGregorian},
19 date::ffi::IsoDate,
20 datetime_helpers,
21 datetime_helpers::map_or_default,
22 datetime_options::ffi::{DateTimeAlignment, DateTimeLength, TimePrecision},
23 errors::ffi::DateTimeFormatterLoadError,
24 errors::ffi::DateTimeWriteError,
25 locale_core::ffi::Locale,
26 time_formatter::ffi::TimeFormatter,
27 time::ffi::Time,
28 timezone::ffi::TimeZoneInfo,
29 };
30
31 #[cfg(feature = "buffer_provider")]
32 use crate::provider::ffi::DataProvider;
33
34 #[diplomat::opaque]
35 #[diplomat::rust_link(icu::datetime::FixedCalendarDateTimeFormatter, Typedef)]
36 pub struct TimeZoneFormatter(
37 pub icu_datetime::FixedCalendarDateTimeFormatter<
38 (),
39 icu_datetime::fieldsets::enums::ZoneFieldSet,
40 >,
41 );
42
43 impl TimeZoneFormatter {
44 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_long")]
45 #[cfg(feature = "compiled_data")]
46 pub fn create_specific_long(
47 locale: &Locale,
48 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
49 let zone = icu_datetime::fieldsets::zone::SpecificLong;
50 let prefs = (&locale.0).into();
51 let options = zone;
52 Ok(Box::new(Self(
53 icu_datetime
54 ::FixedCalendarDateTimeFormatter
55 ::try_new(
56 prefs,
57 options
58 )?
59 .cast_into_fset(),
60 )))
61 }
62
63 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_long_with_provider")]
64 #[cfg(feature = "buffer_provider")]
65 pub fn create_specific_long_with_provider(
66 provider: &DataProvider,
67 locale: &Locale,
68 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
69 let provider = provider.get()?;
70 let zone = icu_datetime::fieldsets::zone::SpecificLong;
71 let prefs = (&locale.0).into();
72 let options = zone;
73 Ok(Box::new(Self(
74 icu_datetime
75 ::FixedCalendarDateTimeFormatter
76 ::try_new_with_buffer_provider(
77 provider,
78 prefs,
79 options
80 )?
81 .cast_into_fset(),
82 )))
83 }
84
85 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_short")]
86 #[cfg(feature = "compiled_data")]
87 pub fn create_specific_short(
88 locale: &Locale,
89 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
90 let zone = icu_datetime::fieldsets::zone::SpecificShort;
91 let prefs = (&locale.0).into();
92 let options = zone;
93 Ok(Box::new(Self(
94 icu_datetime
95 ::FixedCalendarDateTimeFormatter
96 ::try_new(
97 prefs,
98 options
99 )?
100 .cast_into_fset(),
101 )))
102 }
103
104 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_short_with_provider")]
105 #[cfg(feature = "buffer_provider")]
106 pub fn create_specific_short_with_provider(
107 provider: &DataProvider,
108 locale: &Locale,
109 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
110 let provider = provider.get()?;
111 let zone = icu_datetime::fieldsets::zone::SpecificShort;
112 let prefs = (&locale.0).into();
113 let options = zone;
114 Ok(Box::new(Self(
115 icu_datetime
116 ::FixedCalendarDateTimeFormatter
117 ::try_new_with_buffer_provider(
118 provider,
119 prefs,
120 options
121 )?
122 .cast_into_fset(),
123 )))
124 }
125
126 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_long")]
127 #[cfg(feature = "compiled_data")]
128 pub fn create_localized_offset_long(
129 locale: &Locale,
130 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
131 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetLong;
132 let prefs = (&locale.0).into();
133 let options = zone;
134 Ok(Box::new(Self(
135 icu_datetime
136 ::FixedCalendarDateTimeFormatter
137 ::try_new(
138 prefs,
139 options
140 )?
141 .cast_into_fset(),
142 )))
143 }
144
145 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_long_with_provider")]
146 #[cfg(feature = "buffer_provider")]
147 pub fn create_localized_offset_long_with_provider(
148 provider: &DataProvider,
149 locale: &Locale,
150 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
151 let provider = provider.get()?;
152 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetLong;
153 let prefs = (&locale.0).into();
154 let options = zone;
155 Ok(Box::new(Self(
156 icu_datetime
157 ::FixedCalendarDateTimeFormatter
158 ::try_new_with_buffer_provider(
159 provider,
160 prefs,
161 options
162 )?
163 .cast_into_fset(),
164 )))
165 }
166
167 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_short")]
168 #[cfg(feature = "compiled_data")]
169 pub fn create_localized_offset_short(
170 locale: &Locale,
171 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
172 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetShort;
173 let prefs = (&locale.0).into();
174 let options = zone;
175 Ok(Box::new(Self(
176 icu_datetime
177 ::FixedCalendarDateTimeFormatter
178 ::try_new(
179 prefs,
180 options
181 )?
182 .cast_into_fset(),
183 )))
184 }
185
186 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_short_with_provider")]
187 #[cfg(feature = "buffer_provider")]
188 pub fn create_localized_offset_short_with_provider(
189 provider: &DataProvider,
190 locale: &Locale,
191 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
192 let provider = provider.get()?;
193 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetShort;
194 let prefs = (&locale.0).into();
195 let options = zone;
196 Ok(Box::new(Self(
197 icu_datetime
198 ::FixedCalendarDateTimeFormatter
199 ::try_new_with_buffer_provider(
200 provider,
201 prefs,
202 options
203 )?
204 .cast_into_fset(),
205 )))
206 }
207
208 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_long")]
209 #[cfg(feature = "compiled_data")]
210 pub fn create_generic_long(
211 locale: &Locale,
212 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
213 let zone = icu_datetime::fieldsets::zone::GenericLong;
214 let prefs = (&locale.0).into();
215 let options = zone;
216 Ok(Box::new(Self(
217 icu_datetime
218 ::FixedCalendarDateTimeFormatter
219 ::try_new(
220 prefs,
221 options
222 )?
223 .cast_into_fset(),
224 )))
225 }
226
227 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_long_with_provider")]
228 #[cfg(feature = "buffer_provider")]
229 pub fn create_generic_long_with_provider(
230 provider: &DataProvider,
231 locale: &Locale,
232 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
233 let provider = provider.get()?;
234 let zone = icu_datetime::fieldsets::zone::GenericLong;
235 let prefs = (&locale.0).into();
236 let options = zone;
237 Ok(Box::new(Self(
238 icu_datetime
239 ::FixedCalendarDateTimeFormatter
240 ::try_new_with_buffer_provider(
241 provider,
242 prefs,
243 options
244 )?
245 .cast_into_fset(),
246 )))
247 }
248
249 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_short")]
250 #[diplomat::demo(default_constructor)]
251 #[cfg(feature = "compiled_data")]
252 pub fn create_generic_short(
253 locale: &Locale,
254 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
255 let zone = icu_datetime::fieldsets::zone::GenericShort;
256 let prefs = (&locale.0).into();
257 let options = zone;
258 Ok(Box::new(Self(
259 icu_datetime
260 ::FixedCalendarDateTimeFormatter
261 ::try_new(
262 prefs,
263 options
264 )?
265 .cast_into_fset(),
266 )))
267 }
268
269 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_short_with_provider")]
270 #[cfg(feature = "buffer_provider")]
271 pub fn create_generic_short_with_provider(
272 provider: &DataProvider,
273 locale: &Locale,
274 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
275 let provider = provider.get()?;
276 let zone = icu_datetime::fieldsets::zone::GenericShort;
277 let prefs = (&locale.0).into();
278 let options = zone;
279 Ok(Box::new(Self(
280 icu_datetime
281 ::FixedCalendarDateTimeFormatter
282 ::try_new_with_buffer_provider(
283 provider,
284 prefs,
285 options
286 )?
287 .cast_into_fset(),
288 )))
289 }
290
291 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "location")]
292 #[cfg(feature = "compiled_data")]
293 pub fn create_location(
294 locale: &Locale,
295 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
296 let zone = icu_datetime::fieldsets::zone::Location;
297 let prefs = (&locale.0).into();
298 let options = zone;
299 Ok(Box::new(Self(
300 icu_datetime
301 ::FixedCalendarDateTimeFormatter
302 ::try_new(
303 prefs,
304 options
305 )?
306 .cast_into_fset(),
307 )))
308 }
309
310 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "location_with_provider")]
311 #[cfg(feature = "buffer_provider")]
312 pub fn create_location_with_provider(
313 provider: &DataProvider,
314 locale: &Locale,
315 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
316 let provider = provider.get()?;
317 let zone = icu_datetime::fieldsets::zone::Location;
318 let prefs = (&locale.0).into();
319 let options = zone;
320 Ok(Box::new(Self(
321 icu_datetime
322 ::FixedCalendarDateTimeFormatter
323 ::try_new_with_buffer_provider(
324 provider,
325 prefs,
326 options
327 )?
328 .cast_into_fset(),
329 )))
330 }
331
332 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "exemplar_city")]
333 #[cfg(feature = "compiled_data")]
334 pub fn create_exemplar_city(
335 locale: &Locale,
336 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
337 let zone = icu_datetime::fieldsets::zone::ExemplarCity;
338 let prefs = (&locale.0).into();
339 let options = zone;
340 Ok(Box::new(Self(
341 icu_datetime
342 ::FixedCalendarDateTimeFormatter
343 ::try_new(
344 prefs,
345 options
346 )?
347 .cast_into_fset(),
348 )))
349 }
350
351 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "exemplar_city_with_provider")]
352 #[cfg(feature = "buffer_provider")]
353 pub fn create_exemplar_city_with_provider(
354 provider: &DataProvider,
355 locale: &Locale,
356 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
357 let provider = provider.get()?;
358 let zone = icu_datetime::fieldsets::zone::ExemplarCity;
359 let prefs = (&locale.0).into();
360 let options = zone;
361 Ok(Box::new(Self(
362 icu_datetime
363 ::FixedCalendarDateTimeFormatter
364 ::try_new_with_buffer_provider(
365 provider,
366 prefs,
367 options
368 )?
369 .cast_into_fset(),
370 )))
371 }
372
373 #[diplomat::rust_link(icu::datetime::FixedCalendarDateTimeFormatter::format, FnInStruct)]
374 #[diplomat::rust_link(icu::datetime::FormattedDateTime, Struct, hidden)]
375 #[diplomat::rust_link(icu::datetime::FormattedDateTime::to_string, FnInStruct, hidden)]
376 pub fn format(
377 &self,
378 zone: &TimeZoneInfo,
379 write: &mut diplomat_runtime::DiplomatWrite,
380 ) -> Result<(), DateTimeWriteError> {
381 let mut input = icu_datetime::DateTimeInputUnchecked::default();
382 input.set_time_zone_id(zone.time_zone_id);
383 if let Some(offset) = zone.offset {
384 input.set_time_zone_utc_offset(offset);
385 }
386 if let Some(local_time) = zone.local_time {
387 input.set_time_zone_local_time(local_time);
388 }
389 if let Some(zone_variant) = zone.zone_variant {
390 input.set_time_zone_variant(zone_variant);
391 }
392 let _infallible = self
393 .0
394 .format_unchecked(input)
395 .try_write_to(write)
396 .ok()
397 .transpose()?;
398 Ok(())
399 }
400 }
401
402}