Debian Codegraph @

intl/icu_capi/src/zoned_formatter.rs

raw
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 pub mod ffi {
7 use alloc::boxed::Box;
8 use icu_calendar::{DateTime, Gregorian};
9 use icu_datetime::{options::length, TypedZonedDateTimeFormatter, ZonedDateTimeFormatter};
10 use writeable::Writeable;
11
12 use crate::{
13 datetime::ffi::ICU4XDateTime, datetime::ffi::ICU4XIsoDateTime,
14 datetime_formatter::ffi::ICU4XDateLength, datetime_formatter::ffi::ICU4XTimeLength,
15 errors::ffi::ICU4XError, locale::ffi::ICU4XLocale, provider::ffi::ICU4XDataProvider,
16 timezone::ffi::ICU4XCustomTimeZone, timezone_formatter::ffi::ICU4XIsoTimeZoneOptions,
17 };
18
19 // TODO(https://github.com/rust-diplomat/diplomat/issues/248)
20 #[allow(unused_imports)]
21 use crate::{
22 timezone_formatter::ffi::ICU4XIsoTimeZoneFormat,
23 timezone_formatter::ffi::ICU4XIsoTimeZoneMinuteDisplay,
24 timezone_formatter::ffi::ICU4XIsoTimeZoneSecondDisplay,
25 };
26
27 #[diplomat::opaque]
28 /// An object capable of formatting a date time with time zone to a string.
29 #[diplomat::rust_link(icu::datetime::TypedZonedDateTimeFormatter, Struct)]
30 pub struct ICU4XGregorianZonedDateTimeFormatter(pub TypedZonedDateTimeFormatter<Gregorian>);
31
32 impl ICU4XGregorianZonedDateTimeFormatter {
33 /// Creates a new [`ICU4XGregorianZonedDateTimeFormatter`] from locale data.
34 ///
35 /// This function has `date_length` and `time_length` arguments and uses default options
36 /// for the time zone.
37 #[diplomat::rust_link(icu::datetime::TypedZonedDateTimeFormatter::try_new, FnInStruct)]
38 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_lengths")]
39 pub fn create_with_lengths(
40 provider: &ICU4XDataProvider,
41 locale: &ICU4XLocale,
42 date_length: ICU4XDateLength,
43 time_length: ICU4XTimeLength,
44 ) -> Result<Box<ICU4XGregorianZonedDateTimeFormatter>, ICU4XError> {
45 let locale = locale.to_datalocale();
46
47 Ok(Box::new(ICU4XGregorianZonedDateTimeFormatter(
48 call_constructor!(
49 TypedZonedDateTimeFormatter::<Gregorian>::try_new,
50 TypedZonedDateTimeFormatter::<Gregorian>::try_new_with_any_provider,
51 TypedZonedDateTimeFormatter::<Gregorian>::try_new_with_buffer_provider,
52 provider,
53 &locale,
54 length::Bag::from_date_time_style(date_length.into(), time_length.into())
55 .into(),
56 Default::default(),
57 )?,
58 )))
59 }
60
61 /// Creates a new [`ICU4XGregorianZonedDateTimeFormatter`] from locale data.
62 ///
63 /// This function has `date_length` and `time_length` arguments and uses an ISO-8601 style
64 /// fallback for the time zone with the given configurations.
65 #[diplomat::rust_link(icu::datetime::TypedZonedDateTimeFormatter::try_new, FnInStruct)]
66 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_lengths_and_iso_8601_time_zone_fallback")]
67 pub fn create_with_lengths_and_iso_8601_time_zone_fallback(
68 provider: &ICU4XDataProvider,
69 locale: &ICU4XLocale,
70 date_length: ICU4XDateLength,
71 time_length: ICU4XTimeLength,
72 zone_options: ICU4XIsoTimeZoneOptions,
73 ) -> Result<Box<ICU4XGregorianZonedDateTimeFormatter>, ICU4XError> {
74 let locale = locale.to_datalocale();
75
76 Ok(Box::new(ICU4XGregorianZonedDateTimeFormatter(
77 call_constructor!(
78 TypedZonedDateTimeFormatter::<Gregorian>::try_new,
79 TypedZonedDateTimeFormatter::<Gregorian>::try_new_with_any_provider,
80 TypedZonedDateTimeFormatter::<Gregorian>::try_new_with_buffer_provider,
81 provider,
82 &locale,
83 length::Bag::from_date_time_style(date_length.into(), time_length.into())
84 .into(),
85 zone_options.into(),
86 )?,
87 )))
88 }
89
90 /// Formats a [`ICU4XIsoDateTime`] and [`ICU4XCustomTimeZone`] to a string.
91 #[diplomat::rust_link(icu::datetime::TypedZonedDateTimeFormatter::format, FnInStruct)]
92 #[diplomat::rust_link(
93 icu::datetime::TypedZonedDateTimeFormatter::format_to_string,
94 FnInStruct,
95 hidden
96 )]
97 pub fn format_iso_datetime_with_custom_time_zone(
98 &self,
99 datetime: &ICU4XIsoDateTime,
100 time_zone: &ICU4XCustomTimeZone,
101 write: &mut diplomat_runtime::DiplomatWriteable,
102 ) -> Result<(), ICU4XError> {
103 let greg = DateTime::new_from_iso(datetime.0, Gregorian);
104 self.0.format(&greg, &time_zone.0).write_to(write)?;
105 Ok(())
106 }
107 }
108
109 #[diplomat::opaque]
110 /// An object capable of formatting a date time with time zone to a string.
111 #[diplomat::rust_link(icu::datetime::ZonedDateTimeFormatter, Struct)]
112 #[diplomat::rust_link(icu::datetime::FormattedZonedDateTime, Struct, hidden)]
113 pub struct ICU4XZonedDateTimeFormatter(pub ZonedDateTimeFormatter);
114
115 impl ICU4XZonedDateTimeFormatter {
116 /// Creates a new [`ICU4XZonedDateTimeFormatter`] from locale data.
117 ///
118 /// This function has `date_length` and `time_length` arguments and uses default options
119 /// for the time zone.
120 #[diplomat::rust_link(icu::datetime::ZonedDateTimeFormatter::try_new, FnInStruct)]
121 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_lengths")]
122 pub fn create_with_lengths(
123 provider: &ICU4XDataProvider,
124 locale: &ICU4XLocale,
125 date_length: ICU4XDateLength,
126 time_length: ICU4XTimeLength,
127 ) -> Result<Box<ICU4XZonedDateTimeFormatter>, ICU4XError> {
128 let locale = locale.to_datalocale();
129
130 Ok(Box::new(ICU4XZonedDateTimeFormatter(call_constructor!(
131 ZonedDateTimeFormatter::try_new,
132 ZonedDateTimeFormatter::try_new_with_any_provider,
133 ZonedDateTimeFormatter::try_new_with_buffer_provider,
134 provider,
135 &locale,
136 length::Bag::from_date_time_style(date_length.into(), time_length.into()).into(),
137 Default::default(),
138 )?)))
139 }
140
141 /// Creates a new [`ICU4XZonedDateTimeFormatter`] from locale data.
142 ///
143 /// This function has `date_length` and `time_length` arguments and uses an ISO-8601 style
144 /// fallback for the time zone with the given configurations.
145 #[diplomat::rust_link(icu::datetime::ZonedDateTimeFormatter::try_new, FnInStruct)]
146 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_lengths_and_iso_8601_time_zone_fallback")]
147 pub fn create_with_lengths_and_iso_8601_time_zone_fallback(
148 provider: &ICU4XDataProvider,
149 locale: &ICU4XLocale,
150 date_length: ICU4XDateLength,
151 time_length: ICU4XTimeLength,
152 zone_options: ICU4XIsoTimeZoneOptions,
153 ) -> Result<Box<ICU4XZonedDateTimeFormatter>, ICU4XError> {
154 let locale = locale.to_datalocale();
155
156 Ok(Box::new(ICU4XZonedDateTimeFormatter(call_constructor!(
157 ZonedDateTimeFormatter::try_new,
158 ZonedDateTimeFormatter::try_new_with_any_provider,
159 ZonedDateTimeFormatter::try_new_with_buffer_provider,
160 provider,
161 &locale,
162 length::Bag::from_date_time_style(date_length.into(), time_length.into()).into(),
163 zone_options.into(),
164 )?)))
165 }
166
167 /// Formats a [`ICU4XDateTime`] and [`ICU4XCustomTimeZone`] to a string.
168 #[diplomat::rust_link(icu::datetime::ZonedDateTimeFormatter::format, FnInStruct)]
169 #[diplomat::rust_link(
170 icu::datetime::ZonedDateTimeFormatter::format_to_string,
171 FnInStruct,
172 hidden
173 )]
174 #[diplomat::rust_link(icu::datetime::FormattedZonedDateTime::write_to, FnInStruct, hidden)]
175 pub fn format_datetime_with_custom_time_zone(
176 &self,
177 datetime: &ICU4XDateTime,
178 time_zone: &ICU4XCustomTimeZone,
179 write: &mut diplomat_runtime::DiplomatWriteable,
180 ) -> Result<(), ICU4XError> {
181 self.0.format(&datetime.0, &time_zone.0)?.write_to(write)?;
182 Ok(())
183 }
184
185 /// Formats a [`ICU4XIsoDateTime`] and [`ICU4XCustomTimeZone`] to a string.
186 #[diplomat::rust_link(icu::datetime::ZonedDateTimeFormatter::format, FnInStruct)]
187 #[diplomat::rust_link(
188 icu::datetime::ZonedDateTimeFormatter::format_to_string,
189 FnInStruct,
190 hidden
191 )]
192 pub fn format_iso_datetime_with_custom_time_zone(
193 &self,
194 datetime: &ICU4XIsoDateTime,
195 time_zone: &ICU4XCustomTimeZone,
196 write: &mut diplomat_runtime::DiplomatWriteable,
197 ) -> Result<(), ICU4XError> {
198 self.0
199 .format(&datetime.0.to_any(), &time_zone.0)?
200 .write_to(write)?;
201 Ok(())
202 }
203 }
204 }