Debian Codegraph @

intl/icu_capi/src/datetime_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::{Date, DateTime, Gregorian};
9 use icu_datetime::{
10 options::length, DateFormatter, DateTimeFormatter, TimeFormatter, TypedDateFormatter,
11 TypedDateTimeFormatter,
12 };
13
14 use crate::{
15 date::ffi::{ICU4XDate, ICU4XIsoDate},
16 datetime::ffi::ICU4XDateTime,
17 datetime::ffi::ICU4XIsoDateTime,
18 errors::ffi::ICU4XError,
19 locale::ffi::ICU4XLocale,
20 provider::ffi::ICU4XDataProvider,
21 time::ffi::ICU4XTime,
22 };
23 use writeable::Writeable;
24
25 #[diplomat::opaque]
26 /// An ICU4X TimeFormatter object capable of formatting an [`ICU4XTime`] type (and others) as a string
27 #[diplomat::rust_link(icu::datetime::TimeFormatter, Struct)]
28 pub struct ICU4XTimeFormatter(pub TimeFormatter);
29
30 #[diplomat::enum_convert(length::Time, needs_wildcard)]
31 #[diplomat::rust_link(icu::datetime::options::length::Time, Enum)]
32 pub enum ICU4XTimeLength {
33 Full,
34 Long,
35 Medium,
36 Short,
37 }
38
39 impl ICU4XTimeFormatter {
40 /// Creates a new [`ICU4XTimeFormatter`] from locale data.
41 #[diplomat::rust_link(icu::datetime::TimeFormatter::try_new_with_length, FnInStruct)]
42 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_length")]
43 pub fn create_with_length(
44 provider: &ICU4XDataProvider,
45 locale: &ICU4XLocale,
46 length: ICU4XTimeLength,
47 ) -> Result<Box<ICU4XTimeFormatter>, ICU4XError> {
48 let locale = locale.to_datalocale();
49
50 Ok(Box::new(ICU4XTimeFormatter(call_constructor!(
51 TimeFormatter::try_new_with_length,
52 TimeFormatter::try_new_with_length_with_any_provider,
53 TimeFormatter::try_new_with_length_with_buffer_provider,
54 provider,
55 &locale,
56 length.into()
57 )?)))
58 }
59
60 /// Formats a [`ICU4XTime`] to a string.
61 #[diplomat::rust_link(icu::datetime::TimeFormatter::format, FnInStruct)]
62 #[diplomat::rust_link(icu::datetime::TimeFormatter::format_to_string, FnInStruct, hidden)]
63 pub fn format_time(
64 &self,
65 value: &ICU4XTime,
66 write: &mut diplomat_runtime::DiplomatWriteable,
67 ) -> Result<(), ICU4XError> {
68 self.0.format(&value.0).write_to(write)?;
69 Ok(())
70 }
71
72 /// Formats a [`ICU4XDateTime`] to a string.
73 #[diplomat::rust_link(icu::datetime::TimeFormatter::format, FnInStruct)]
74 #[diplomat::rust_link(icu::datetime::TimeFormatter::format_to_string, FnInStruct, hidden)]
75 pub fn format_datetime(
76 &self,
77 value: &ICU4XDateTime,
78 write: &mut diplomat_runtime::DiplomatWriteable,
79 ) -> Result<(), ICU4XError> {
80 self.0.format(&value.0).write_to(write)?;
81 Ok(())
82 }
83
84 /// Formats a [`ICU4XIsoDateTime`] to a string.
85 #[diplomat::rust_link(icu::datetime::TimeFormatter::format, FnInStruct)]
86 #[diplomat::rust_link(icu::datetime::TimeFormatter::format_to_string, FnInStruct, hidden)]
87 pub fn format_iso_datetime(
88 &self,
89 value: &ICU4XIsoDateTime,
90 write: &mut diplomat_runtime::DiplomatWriteable,
91 ) -> Result<(), ICU4XError> {
92 self.0.format(&value.0).write_to(write)?;
93 Ok(())
94 }
95 }
96
97 #[diplomat::opaque]
98 /// An ICU4X TypedDateFormatter object capable of formatting a [`ICU4XIsoDateTime`] as a string,
99 /// using the Gregorian Calendar.
100 #[diplomat::rust_link(icu::datetime::TypedDateFormatter, Struct)]
101 pub struct ICU4XGregorianDateFormatter(pub TypedDateFormatter<Gregorian>);
102
103 #[diplomat::enum_convert(length::Date, needs_wildcard)]
104 #[diplomat::rust_link(icu::datetime::options::length::Date, Enum)]
105 pub enum ICU4XDateLength {
106 Full,
107 Long,
108 Medium,
109 Short,
110 }
111
112 impl ICU4XGregorianDateFormatter {
113 /// Creates a new [`ICU4XGregorianDateFormatter`] from locale data.
114 #[diplomat::rust_link(icu::datetime::TypedDateFormatter::try_new_with_length, FnInStruct)]
115 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_length")]
116 pub fn create_with_length(
117 provider: &ICU4XDataProvider,
118 locale: &ICU4XLocale,
119 length: ICU4XDateLength,
120 ) -> Result<Box<ICU4XGregorianDateFormatter>, ICU4XError> {
121 let locale = locale.to_datalocale();
122
123 Ok(Box::new(ICU4XGregorianDateFormatter(call_constructor!(
124 TypedDateFormatter::try_new_with_length,
125 TypedDateFormatter::try_new_with_length_with_any_provider,
126 TypedDateFormatter::try_new_with_length_with_buffer_provider,
127 provider,
128 &locale,
129 length.into()
130 )?)))
131 }
132
133 /// Formats a [`ICU4XIsoDate`] to a string.
134 #[diplomat::rust_link(icu::datetime::TypedDateFormatter::format, FnInStruct)]
135 #[diplomat::rust_link(
136 icu::datetime::TypedDateFormatter::format_to_string,
137 FnInStruct,
138 hidden
139 )]
140 pub fn format_iso_date(
141 &self,
142 value: &ICU4XIsoDate,
143 write: &mut diplomat_runtime::DiplomatWriteable,
144 ) -> Result<(), ICU4XError> {
145 let greg = Date::new_from_iso(value.0, Gregorian);
146 self.0.format(&greg).write_to(write)?;
147 Ok(())
148 }
149 /// Formats a [`ICU4XIsoDateTime`] to a string.
150 #[diplomat::rust_link(icu::datetime::TypedDateFormatter::format, FnInStruct)]
151 #[diplomat::rust_link(
152 icu::datetime::TypedDateFormatter::format_to_string,
153 FnInStruct,
154 hidden
155 )]
156 pub fn format_iso_datetime(
157 &self,
158 value: &ICU4XIsoDateTime,
159 write: &mut diplomat_runtime::DiplomatWriteable,
160 ) -> Result<(), ICU4XError> {
161 let greg = DateTime::new_from_iso(value.0, Gregorian);
162 self.0.format(&greg).write_to(write)?;
163 Ok(())
164 }
165 }
166
167 #[diplomat::opaque]
168 /// An ICU4X TypedDateTimeFormatter object capable of formatting a [`ICU4XIsoDateTime`] as a string,
169 /// using the Gregorian Calendar.
170 #[diplomat::rust_link(icu::datetime::TypedDateTimeFormatter, Struct)]
171 pub struct ICU4XGregorianDateTimeFormatter(pub TypedDateTimeFormatter<Gregorian>);
172
173 impl ICU4XGregorianDateTimeFormatter {
174 /// Creates a new [`ICU4XGregorianDateFormatter`] from locale data.
175 #[diplomat::rust_link(icu::datetime::TypedDateTimeFormatter::try_new, FnInStruct)]
176 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_lengths")]
177 pub fn create_with_lengths(
178 provider: &ICU4XDataProvider,
179 locale: &ICU4XLocale,
180 date_length: ICU4XDateLength,
181 time_length: ICU4XTimeLength,
182 ) -> Result<Box<ICU4XGregorianDateTimeFormatter>, ICU4XError> {
183 let locale = locale.to_datalocale();
184
185 let options = length::Bag::from_date_time_style(date_length.into(), time_length.into());
186
187 Ok(Box::new(ICU4XGregorianDateTimeFormatter(
188 call_constructor!(
189 TypedDateTimeFormatter::try_new,
190 TypedDateTimeFormatter::try_new_with_any_provider,
191 TypedDateTimeFormatter::try_new_with_buffer_provider,
192 provider,
193 &locale,
194 options.into()
195 )?,
196 )))
197 }
198
199 /// Formats a [`ICU4XIsoDateTime`] to a string.
200 #[diplomat::rust_link(icu::datetime::TypedDateTimeFormatter::format, FnInStruct)]
201 #[diplomat::rust_link(
202 icu::datetime::TypedDateTimeFormatter::format_to_string,
203 FnInStruct,
204 hidden
205 )]
206 pub fn format_iso_datetime(
207 &self,
208 value: &ICU4XIsoDateTime,
209 write: &mut diplomat_runtime::DiplomatWriteable,
210 ) -> Result<(), ICU4XError> {
211 let greg = DateTime::new_from_iso(value.0, Gregorian);
212 self.0.format(&greg).write_to(write)?;
213 Ok(())
214 }
215 }
216
217 #[diplomat::opaque]
218 /// An ICU4X DateFormatter object capable of formatting a [`ICU4XDate`] as a string,
219 /// using some calendar specified at runtime in the locale.
220 #[diplomat::rust_link(icu::datetime::DateFormatter, Struct)]
221 pub struct ICU4XDateFormatter(pub DateFormatter);
222
223 impl ICU4XDateFormatter {
224 /// Creates a new [`ICU4XDateFormatter`] from locale data.
225 #[diplomat::rust_link(icu::datetime::DateFormatter::try_new_with_length, FnInStruct)]
226 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_length")]
227 pub fn create_with_length(
228 provider: &ICU4XDataProvider,
229 locale: &ICU4XLocale,
230 date_length: ICU4XDateLength,
231 ) -> Result<Box<ICU4XDateFormatter>, ICU4XError> {
232 let locale = locale.to_datalocale();
233
234 Ok(Box::new(ICU4XDateFormatter(call_constructor!(
235 DateFormatter::try_new_with_length,
236 DateFormatter::try_new_with_length_with_any_provider,
237 DateFormatter::try_new_with_length_with_buffer_provider,
238 provider,
239 &locale,
240 date_length.into()
241 )?)))
242 }
243
244 /// Formats a [`ICU4XDate`] to a string.
245 #[diplomat::rust_link(icu::datetime::DateFormatter::format, FnInStruct)]
246 #[diplomat::rust_link(icu::datetime::DateFormatter::format_to_string, FnInStruct, hidden)]
247 pub fn format_date(
248 &self,
249 value: &ICU4XDate,
250 write: &mut diplomat_runtime::DiplomatWriteable,
251 ) -> Result<(), ICU4XError> {
252 self.0.format(&value.0)?.write_to(write)?;
253 Ok(())
254 }
255
256 /// Formats a [`ICU4XIsoDate`] to a string.
257 ///
258 /// Will convert to this formatter's calendar first
259 #[diplomat::rust_link(icu::datetime::DateFormatter::format, FnInStruct)]
260 #[diplomat::rust_link(icu::datetime::DateFormatter::format_to_string, FnInStruct, hidden)]
261 pub fn format_iso_date(
262 &self,
263 value: &ICU4XIsoDate,
264 write: &mut diplomat_runtime::DiplomatWriteable,
265 ) -> Result<(), ICU4XError> {
266 let any = value.0.to_any();
267 self.0.format(&any)?.write_to(write)?;
268 Ok(())
269 }
270
271 /// Formats a [`ICU4XDateTime`] to a string.
272 #[diplomat::rust_link(icu::datetime::DateFormatter::format, FnInStruct)]
273 #[diplomat::rust_link(icu::datetime::DateFormatter::format_to_string, FnInStruct, hidden)]
274 pub fn format_datetime(
275 &self,
276 value: &ICU4XDateTime,
277 write: &mut diplomat_runtime::DiplomatWriteable,
278 ) -> Result<(), ICU4XError> {
279 self.0.format(&value.0)?.write_to(write)?;
280 Ok(())
281 }
282
283 /// Formats a [`ICU4XIsoDateTime`] to a string.
284 ///
285 /// Will convert to this formatter's calendar first
286 #[diplomat::rust_link(icu::datetime::DateFormatter::format, FnInStruct)]
287 #[diplomat::rust_link(icu::datetime::DateFormatter::format_to_string, FnInStruct, hidden)]
288 pub fn format_iso_datetime(
289 &self,
290 value: &ICU4XIsoDateTime,
291 write: &mut diplomat_runtime::DiplomatWriteable,
292 ) -> Result<(), ICU4XError> {
293 let any = value.0.to_any();
294 self.0.format(&any)?.write_to(write)?;
295 Ok(())
296 }
297 }
298
299 #[diplomat::opaque]
300 /// An ICU4X DateFormatter object capable of formatting a [`ICU4XDateTime`] as a string,
301 /// using some calendar specified at runtime in the locale.
302 #[diplomat::rust_link(icu::datetime::DateTimeFormatter, Struct)]
303 #[diplomat::rust_link(icu::datetime::FormattedDateTime, Struct, hidden)]
304 pub struct ICU4XDateTimeFormatter(pub DateTimeFormatter);
305
306 impl ICU4XDateTimeFormatter {
307 /// Creates a new [`ICU4XDateTimeFormatter`] from locale data.
308 #[diplomat::rust_link(icu::datetime::DateTimeFormatter::try_new, FnInStruct)]
309 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_lengths")]
310 pub fn create_with_lengths(
311 provider: &ICU4XDataProvider,
312 locale: &ICU4XLocale,
313 date_length: ICU4XDateLength,
314 time_length: ICU4XTimeLength,
315 ) -> Result<Box<ICU4XDateTimeFormatter>, ICU4XError> {
316 let locale = locale.to_datalocale();
317 let options = length::Bag::from_date_time_style(date_length.into(), time_length.into());
318
319 Ok(Box::new(ICU4XDateTimeFormatter(call_constructor!(
320 DateTimeFormatter::try_new,
321 DateTimeFormatter::try_new_with_any_provider,
322 DateTimeFormatter::try_new_with_buffer_provider,
323 provider,
324 &locale,
325 options.into()
326 )?)))
327 }
328
329 /// Formats a [`ICU4XDateTime`] to a string.
330 #[diplomat::rust_link(icu::datetime::DateTimeFormatter::format, FnInStruct)]
331 #[diplomat::rust_link(
332 icu::datetime::DateTimeFormatter::format_to_string,
333 FnInStruct,
334 hidden
335 )]
336 #[diplomat::rust_link(icu::datetime::FormattedDateTime::write_to, FnInStruct, hidden)]
337 pub fn format_datetime(
338 &self,
339 value: &ICU4XDateTime,
340 write: &mut diplomat_runtime::DiplomatWriteable,
341 ) -> Result<(), ICU4XError> {
342 self.0.format(&value.0)?.write_to(write)?;
343 Ok(())
344 }
345
346 /// Formats a [`ICU4XIsoDateTime`] to a string.
347 ///
348 /// Will convert to this formatter's calendar first
349 #[diplomat::rust_link(icu::datetime::DateTimeFormatter::format, FnInStruct)]
350 #[diplomat::rust_link(
351 icu::datetime::DateTimeFormatter::format_to_string,
352 FnInStruct,
353 hidden
354 )]
355 pub fn format_iso_datetime(
356 &self,
357 value: &ICU4XIsoDateTime,
358 write: &mut diplomat_runtime::DiplomatWriteable,
359 ) -> Result<(), ICU4XError> {
360 let any = value.0.to_any();
361 self.0.format(&any)?.write_to(write)?;
362 Ok(())
363 }
364 }
365 }