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 ZonedTimeFormatter(
37 pub icu_datetime::FixedCalendarDateTimeFormatter<
38 (),
39 icu_datetime::fieldsets::enums::ZonedTimeFieldSet
40 >,
41 );
42
43 impl ZonedTimeFormatter {
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 length: Option<DateTimeLength>,
49 time_precision: Option<TimePrecision>,
50 alignment: Option<DateTimeAlignment>,
51 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
52 let zone = icu_datetime::fieldsets::zone::SpecificLong;
53 let prefs = (&locale.0).into();
54 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
55 options.time_precision = time_precision.map(Into::into);
56 options.alignment = alignment.map(Into::into);
57 let options = options.zone(zone);
58 Ok(Box::new(Self(
59 icu_datetime
60 ::FixedCalendarDateTimeFormatter
61 ::try_new(
62 prefs,
63 options
64 )?
65 .cast_into_fset(),
66 )))
67 }
68
69 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_long_with_provider")]
70 #[cfg(feature = "buffer_provider")]
71 pub fn create_specific_long_with_provider(
72 provider: &DataProvider,
73 locale: &Locale,
74 length: Option<DateTimeLength>,
75 time_precision: Option<TimePrecision>,
76 alignment: Option<DateTimeAlignment>,
77 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
78 let provider = provider.get()?;
79 let zone = icu_datetime::fieldsets::zone::SpecificLong;
80 let prefs = (&locale.0).into();
81 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
82 options.time_precision = time_precision.map(Into::into);
83 options.alignment = alignment.map(Into::into);
84 let options = options.zone(zone);
85 Ok(Box::new(Self(
86 icu_datetime
87 ::FixedCalendarDateTimeFormatter
88 ::try_new_with_buffer_provider(
89 provider,
90 prefs,
91 options
92 )?
93 .cast_into_fset(),
94 )))
95 }
96
97 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_short")]
98 #[cfg(feature = "compiled_data")]
99 pub fn create_specific_short(
100 locale: &Locale,
101 length: Option<DateTimeLength>,
102 time_precision: Option<TimePrecision>,
103 alignment: Option<DateTimeAlignment>,
104 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
105 let zone = icu_datetime::fieldsets::zone::SpecificShort;
106 let prefs = (&locale.0).into();
107 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
108 options.time_precision = time_precision.map(Into::into);
109 options.alignment = alignment.map(Into::into);
110 let options = options.zone(zone);
111 Ok(Box::new(Self(
112 icu_datetime
113 ::FixedCalendarDateTimeFormatter
114 ::try_new(
115 prefs,
116 options
117 )?
118 .cast_into_fset(),
119 )))
120 }
121
122 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_short_with_provider")]
123 #[cfg(feature = "buffer_provider")]
124 pub fn create_specific_short_with_provider(
125 provider: &DataProvider,
126 locale: &Locale,
127 length: Option<DateTimeLength>,
128 time_precision: Option<TimePrecision>,
129 alignment: Option<DateTimeAlignment>,
130 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
131 let provider = provider.get()?;
132 let zone = icu_datetime::fieldsets::zone::SpecificShort;
133 let prefs = (&locale.0).into();
134 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
135 options.time_precision = time_precision.map(Into::into);
136 options.alignment = alignment.map(Into::into);
137 let options = options.zone(zone);
138 Ok(Box::new(Self(
139 icu_datetime
140 ::FixedCalendarDateTimeFormatter
141 ::try_new_with_buffer_provider(
142 provider,
143 prefs,
144 options
145 )?
146 .cast_into_fset(),
147 )))
148 }
149
150 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_long")]
151 #[cfg(feature = "compiled_data")]
152 pub fn create_localized_offset_long(
153 locale: &Locale,
154 length: Option<DateTimeLength>,
155 time_precision: Option<TimePrecision>,
156 alignment: Option<DateTimeAlignment>,
157 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
158 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetLong;
159 let prefs = (&locale.0).into();
160 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
161 options.time_precision = time_precision.map(Into::into);
162 options.alignment = alignment.map(Into::into);
163 let options = options.zone(zone);
164 Ok(Box::new(Self(
165 icu_datetime
166 ::FixedCalendarDateTimeFormatter
167 ::try_new(
168 prefs,
169 options
170 )?
171 .cast_into_fset(),
172 )))
173 }
174
175 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_long_with_provider")]
176 #[cfg(feature = "buffer_provider")]
177 pub fn create_localized_offset_long_with_provider(
178 provider: &DataProvider,
179 locale: &Locale,
180 length: Option<DateTimeLength>,
181 time_precision: Option<TimePrecision>,
182 alignment: Option<DateTimeAlignment>,
183 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
184 let provider = provider.get()?;
185 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetLong;
186 let prefs = (&locale.0).into();
187 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
188 options.time_precision = time_precision.map(Into::into);
189 options.alignment = alignment.map(Into::into);
190 let options = options.zone(zone);
191 Ok(Box::new(Self(
192 icu_datetime
193 ::FixedCalendarDateTimeFormatter
194 ::try_new_with_buffer_provider(
195 provider,
196 prefs,
197 options
198 )?
199 .cast_into_fset(),
200 )))
201 }
202
203 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_short")]
204 #[cfg(feature = "compiled_data")]
205 pub fn create_localized_offset_short(
206 locale: &Locale,
207 length: Option<DateTimeLength>,
208 time_precision: Option<TimePrecision>,
209 alignment: Option<DateTimeAlignment>,
210 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
211 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetShort;
212 let prefs = (&locale.0).into();
213 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
214 options.time_precision = time_precision.map(Into::into);
215 options.alignment = alignment.map(Into::into);
216 let options = options.zone(zone);
217 Ok(Box::new(Self(
218 icu_datetime
219 ::FixedCalendarDateTimeFormatter
220 ::try_new(
221 prefs,
222 options
223 )?
224 .cast_into_fset(),
225 )))
226 }
227
228 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_short_with_provider")]
229 #[cfg(feature = "buffer_provider")]
230 pub fn create_localized_offset_short_with_provider(
231 provider: &DataProvider,
232 locale: &Locale,
233 length: Option<DateTimeLength>,
234 time_precision: Option<TimePrecision>,
235 alignment: Option<DateTimeAlignment>,
236 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
237 let provider = provider.get()?;
238 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetShort;
239 let prefs = (&locale.0).into();
240 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
241 options.time_precision = time_precision.map(Into::into);
242 options.alignment = alignment.map(Into::into);
243 let options = options.zone(zone);
244 Ok(Box::new(Self(
245 icu_datetime
246 ::FixedCalendarDateTimeFormatter
247 ::try_new_with_buffer_provider(
248 provider,
249 prefs,
250 options
251 )?
252 .cast_into_fset(),
253 )))
254 }
255
256 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_long")]
257 #[cfg(feature = "compiled_data")]
258 pub fn create_generic_long(
259 locale: &Locale,
260 length: Option<DateTimeLength>,
261 time_precision: Option<TimePrecision>,
262 alignment: Option<DateTimeAlignment>,
263 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
264 let zone = icu_datetime::fieldsets::zone::GenericLong;
265 let prefs = (&locale.0).into();
266 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
267 options.time_precision = time_precision.map(Into::into);
268 options.alignment = alignment.map(Into::into);
269 let options = options.zone(zone);
270 Ok(Box::new(Self(
271 icu_datetime
272 ::FixedCalendarDateTimeFormatter
273 ::try_new(
274 prefs,
275 options
276 )?
277 .cast_into_fset(),
278 )))
279 }
280
281 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_long_with_provider")]
282 #[cfg(feature = "buffer_provider")]
283 pub fn create_generic_long_with_provider(
284 provider: &DataProvider,
285 locale: &Locale,
286 length: Option<DateTimeLength>,
287 time_precision: Option<TimePrecision>,
288 alignment: Option<DateTimeAlignment>,
289 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
290 let provider = provider.get()?;
291 let zone = icu_datetime::fieldsets::zone::GenericLong;
292 let prefs = (&locale.0).into();
293 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
294 options.time_precision = time_precision.map(Into::into);
295 options.alignment = alignment.map(Into::into);
296 let options = options.zone(zone);
297 Ok(Box::new(Self(
298 icu_datetime
299 ::FixedCalendarDateTimeFormatter
300 ::try_new_with_buffer_provider(
301 provider,
302 prefs,
303 options
304 )?
305 .cast_into_fset(),
306 )))
307 }
308
309 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_short")]
310 #[diplomat::demo(default_constructor)]
311 #[cfg(feature = "compiled_data")]
312 pub fn create_generic_short(
313 locale: &Locale,
314 length: Option<DateTimeLength>,
315 time_precision: Option<TimePrecision>,
316 alignment: Option<DateTimeAlignment>,
317 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
318 let zone = icu_datetime::fieldsets::zone::GenericShort;
319 let prefs = (&locale.0).into();
320 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
321 options.time_precision = time_precision.map(Into::into);
322 options.alignment = alignment.map(Into::into);
323 let options = options.zone(zone);
324 Ok(Box::new(Self(
325 icu_datetime
326 ::FixedCalendarDateTimeFormatter
327 ::try_new(
328 prefs,
329 options
330 )?
331 .cast_into_fset(),
332 )))
333 }
334
335 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_short_with_provider")]
336 #[cfg(feature = "buffer_provider")]
337 pub fn create_generic_short_with_provider(
338 provider: &DataProvider,
339 locale: &Locale,
340 length: Option<DateTimeLength>,
341 time_precision: Option<TimePrecision>,
342 alignment: Option<DateTimeAlignment>,
343 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
344 let provider = provider.get()?;
345 let zone = icu_datetime::fieldsets::zone::GenericShort;
346 let prefs = (&locale.0).into();
347 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
348 options.time_precision = time_precision.map(Into::into);
349 options.alignment = alignment.map(Into::into);
350 let options = options.zone(zone);
351 Ok(Box::new(Self(
352 icu_datetime
353 ::FixedCalendarDateTimeFormatter
354 ::try_new_with_buffer_provider(
355 provider,
356 prefs,
357 options
358 )?
359 .cast_into_fset(),
360 )))
361 }
362
363 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "location")]
364 #[cfg(feature = "compiled_data")]
365 pub fn create_location(
366 locale: &Locale,
367 length: Option<DateTimeLength>,
368 time_precision: Option<TimePrecision>,
369 alignment: Option<DateTimeAlignment>,
370 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
371 let zone = icu_datetime::fieldsets::zone::Location;
372 let prefs = (&locale.0).into();
373 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
374 options.time_precision = time_precision.map(Into::into);
375 options.alignment = alignment.map(Into::into);
376 let options = options.zone(zone);
377 Ok(Box::new(Self(
378 icu_datetime
379 ::FixedCalendarDateTimeFormatter
380 ::try_new(
381 prefs,
382 options
383 )?
384 .cast_into_fset(),
385 )))
386 }
387
388 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "location_with_provider")]
389 #[cfg(feature = "buffer_provider")]
390 pub fn create_location_with_provider(
391 provider: &DataProvider,
392 locale: &Locale,
393 length: Option<DateTimeLength>,
394 time_precision: Option<TimePrecision>,
395 alignment: Option<DateTimeAlignment>,
396 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
397 let provider = provider.get()?;
398 let zone = icu_datetime::fieldsets::zone::Location;
399 let prefs = (&locale.0).into();
400 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
401 options.time_precision = time_precision.map(Into::into);
402 options.alignment = alignment.map(Into::into);
403 let options = options.zone(zone);
404 Ok(Box::new(Self(
405 icu_datetime
406 ::FixedCalendarDateTimeFormatter
407 ::try_new_with_buffer_provider(
408 provider,
409 prefs,
410 options
411 )?
412 .cast_into_fset(),
413 )))
414 }
415
416 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "exemplar_city")]
417 #[cfg(feature = "compiled_data")]
418 pub fn create_exemplar_city(
419 locale: &Locale,
420 length: Option<DateTimeLength>,
421 time_precision: Option<TimePrecision>,
422 alignment: Option<DateTimeAlignment>,
423 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
424 let zone = icu_datetime::fieldsets::zone::ExemplarCity;
425 let prefs = (&locale.0).into();
426 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
427 options.time_precision = time_precision.map(Into::into);
428 options.alignment = alignment.map(Into::into);
429 let options = options.zone(zone);
430 Ok(Box::new(Self(
431 icu_datetime
432 ::FixedCalendarDateTimeFormatter
433 ::try_new(
434 prefs,
435 options
436 )?
437 .cast_into_fset(),
438 )))
439 }
440
441 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "exemplar_city_with_provider")]
442 #[cfg(feature = "buffer_provider")]
443 pub fn create_exemplar_city_with_provider(
444 provider: &DataProvider,
445 locale: &Locale,
446 length: Option<DateTimeLength>,
447 time_precision: Option<TimePrecision>,
448 alignment: Option<DateTimeAlignment>,
449 ) -> Result<Box<Self>, DateTimeFormatterLoadError> {
450 let provider = provider.get()?;
451 let zone = icu_datetime::fieldsets::zone::ExemplarCity;
452 let prefs = (&locale.0).into();
453 let mut options = icu_datetime::fieldsets::T::with_length(map_or_default(length));
454 options.time_precision = time_precision.map(Into::into);
455 options.alignment = alignment.map(Into::into);
456 let options = options.zone(zone);
457 Ok(Box::new(Self(
458 icu_datetime
459 ::FixedCalendarDateTimeFormatter
460 ::try_new_with_buffer_provider(
461 provider,
462 prefs,
463 options
464 )?
465 .cast_into_fset(),
466 )))
467 }
468
469 #[diplomat::rust_link(icu::datetime::FixedCalendarDateTimeFormatter::format, FnInStruct)]
470 #[diplomat::rust_link(icu::datetime::FormattedDateTime, Struct, hidden)]
471 #[diplomat::rust_link(icu::datetime::FormattedDateTime::to_string, FnInStruct, hidden)]
472 pub fn format(
473 &self,
474 time: &Time,
475 zone: &TimeZoneInfo,
476 write: &mut diplomat_runtime::DiplomatWrite,
477 ) -> Result<(), DateTimeWriteError> {
478 let mut input = icu_datetime::DateTimeInputUnchecked::default();
479 input.set_time_fields(time.0);
480 input.set_time_zone_id(zone.time_zone_id);
481 if let Some(offset) = zone.offset {
482 input.set_time_zone_utc_offset(offset);
483 }
484 if let Some(local_time) = zone.local_time {
485 input.set_time_zone_local_time(local_time);
486 }
487 if let Some(zone_variant) = zone.zone_variant {
488 input.set_time_zone_variant(zone_variant);
489 }
490 let _infallible = self
491 .0
492 .format_unchecked(input)
493 .try_write_to(write)
494 .ok()
495 .transpose()?;
496 Ok(())
497 }
498 }
499
500}