summaryrefslogtreecommitdiffstats
path: root/intl/icu_capi/src/timezone_formatter.rs
blob: 0cd4b04bc35e4c44e6dd16f5fd8f816d14ecc86f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use icu_datetime::time_zone::{FallbackFormat, TimeZoneFormatterOptions};

macro_rules! call_method {
    ($self:ident, $compiled:ident, $unstable:ident, $provider:expr) => {
        match &$provider.0 {
            $crate::provider::ICU4XDataProviderInner::Destroyed => Err(
                icu_provider::DataError::custom("This provider has been destroyed"),
            )?,
            $crate::provider::ICU4XDataProviderInner::Empty => $self
                .0
                .$unstable(&icu_provider_adapters::empty::EmptyDataProvider::new()),
            #[cfg(feature = "buffer_provider")]
            $crate::provider::ICU4XDataProviderInner::Buffer(buffer_provider) => $self.0.$unstable(
                &icu_provider::AsDeserializingBufferProvider::as_deserializing(buffer_provider),
            ),
            #[cfg(feature = "compiled_data")]
            $crate::provider::ICU4XDataProviderInner::Compiled => $self.0.$compiled(),
        }
    };
}

#[diplomat::bridge]
pub mod ffi {
    use crate::errors::ffi::ICU4XError;
    use crate::locale::ffi::ICU4XLocale;
    use crate::provider::ffi::ICU4XDataProvider;
    use crate::timezone::ffi::ICU4XCustomTimeZone;
    use alloc::boxed::Box;
    use icu_datetime::time_zone::FallbackFormat;
    use icu_datetime::time_zone::IsoFormat;
    use icu_datetime::time_zone::IsoMinutes;
    use icu_datetime::time_zone::IsoSeconds;
    use icu_datetime::time_zone::TimeZoneFormatter;
    use writeable::Writeable;

    #[diplomat::opaque]
    /// An ICU4X TimeZoneFormatter object capable of formatting an [`ICU4XCustomTimeZone`] type (and others) as a string
    #[diplomat::rust_link(icu::datetime::time_zone::TimeZoneFormatter, Struct)]
    pub struct ICU4XTimeZoneFormatter(pub TimeZoneFormatter);

    #[diplomat::enum_convert(IsoFormat, needs_wildcard)]
    #[diplomat::rust_link(icu::datetime::time_zone::IsoFormat, Enum)]
    pub enum ICU4XIsoTimeZoneFormat {
        Basic,
        Extended,
        UtcBasic,
        UtcExtended,
    }

    #[diplomat::enum_convert(IsoMinutes, needs_wildcard)]
    #[diplomat::rust_link(icu::datetime::time_zone::IsoMinutes, Enum)]
    pub enum ICU4XIsoTimeZoneMinuteDisplay {
        Required,
        Optional,
    }

    #[diplomat::enum_convert(IsoSeconds, needs_wildcard)]
    #[diplomat::rust_link(icu::datetime::time_zone::IsoSeconds, Enum)]
    pub enum ICU4XIsoTimeZoneSecondDisplay {
        Optional,
        Never,
    }

    pub struct ICU4XIsoTimeZoneOptions {
        pub format: ICU4XIsoTimeZoneFormat,
        pub minutes: ICU4XIsoTimeZoneMinuteDisplay,
        pub seconds: ICU4XIsoTimeZoneSecondDisplay,
    }

    impl ICU4XTimeZoneFormatter {
        /// Creates a new [`ICU4XTimeZoneFormatter`] from locale data.
        ///
        /// Uses localized GMT as the fallback format.
        #[diplomat::rust_link(icu::datetime::time_zone::TimeZoneFormatter::try_new, FnInStruct)]
        #[diplomat::rust_link(icu::datetime::time_zone::FallbackFormat, Enum, compact)]
        #[diplomat::rust_link(icu::datetime::time_zone::TimeZoneFormatterOptions, Struct, hidden)]
        pub fn create_with_localized_gmt_fallback(
            provider: &ICU4XDataProvider,
            locale: &ICU4XLocale,
        ) -> Result<Box<ICU4XTimeZoneFormatter>, ICU4XError> {
            let locale = locale.to_datalocale();

            Ok(Box::new(ICU4XTimeZoneFormatter(call_constructor!(
                TimeZoneFormatter::try_new,
                TimeZoneFormatter::try_new_with_any_provider,
                TimeZoneFormatter::try_new_with_buffer_provider,
                provider,
                &locale,
                FallbackFormat::LocalizedGmt.into(),
            )?)))
        }

        /// Creates a new [`ICU4XTimeZoneFormatter`] from locale data.
        ///
        /// Uses ISO-8601 as the fallback format.
        #[diplomat::rust_link(icu::datetime::time_zone::TimeZoneFormatter::try_new, FnInStruct)]
        #[diplomat::rust_link(icu::datetime::time_zone::FallbackFormat, Enum, compact)]
        #[diplomat::rust_link(icu::datetime::time_zone::TimeZoneFormatterOptions, Struct, hidden)]
        pub fn create_with_iso_8601_fallback(
            provider: &ICU4XDataProvider,
            locale: &ICU4XLocale,
            options: ICU4XIsoTimeZoneOptions,
        ) -> Result<Box<ICU4XTimeZoneFormatter>, ICU4XError> {
            let locale = locale.to_datalocale();

            Ok(Box::new(ICU4XTimeZoneFormatter(call_constructor!(
                TimeZoneFormatter::try_new,
                TimeZoneFormatter::try_new_with_any_provider,
                TimeZoneFormatter::try_new_with_buffer_provider,
                provider,
                &locale,
                options.into(),
            )?)))
        }

        /// Loads generic non-location long format. Example: "Pacific Time"
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::include_generic_non_location_long,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::load_generic_non_location_long,
            FnInStruct,
            hidden
        )]
        pub fn load_generic_non_location_long(
            &mut self,
            provider: &ICU4XDataProvider,
        ) -> Result<(), ICU4XError> {
            call_method!(
                self,
                include_generic_non_location_long,
                load_generic_non_location_long,
                provider
            )?;
            Ok(())
        }

        /// Loads generic non-location short format. Example: "PT"
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::include_generic_non_location_short,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::load_generic_non_location_short,
            FnInStruct,
            hidden
        )]
        pub fn load_generic_non_location_short(
            &mut self,
            provider: &ICU4XDataProvider,
        ) -> Result<(), ICU4XError> {
            call_method!(
                self,
                include_generic_non_location_short,
                load_generic_non_location_short,
                provider
            )?;
            Ok(())
        }

        /// Loads specific non-location long format. Example: "Pacific Standard Time"
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::include_specific_non_location_long,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::load_specific_non_location_long,
            FnInStruct,
            hidden
        )]
        pub fn load_specific_non_location_long(
            &mut self,
            provider: &ICU4XDataProvider,
        ) -> Result<(), ICU4XError> {
            call_method!(
                self,
                include_specific_non_location_long,
                load_specific_non_location_long,
                provider
            )?;
            Ok(())
        }

        /// Loads specific non-location short format. Example: "PST"
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::include_specific_non_location_short,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::load_specific_non_location_short,
            FnInStruct,
            hidden
        )]
        pub fn load_specific_non_location_short(
            &mut self,
            provider: &ICU4XDataProvider,
        ) -> Result<(), ICU4XError> {
            call_method!(
                self,
                include_specific_non_location_short,
                load_specific_non_location_short,
                provider
            )?;
            Ok(())
        }

        /// Loads generic location format. Example: "Los Angeles Time"
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::include_generic_location_format,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::load_generic_location_format,
            FnInStruct,
            hidden
        )]
        pub fn load_generic_location_format(
            &mut self,
            provider: &ICU4XDataProvider,
        ) -> Result<(), ICU4XError> {
            call_method!(
                self,
                include_generic_location_format,
                load_generic_location_format,
                provider
            )?;
            Ok(())
        }

        /// Loads localized GMT format. Example: "GMT-07:00"
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::include_localized_gmt_format,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::load_localized_gmt_format,
            FnInStruct,
            hidden
        )]
        pub fn include_localized_gmt_format(&mut self) -> Result<(), ICU4XError> {
            self.0.include_localized_gmt_format()?;
            Ok(())
        }

        /// Loads ISO-8601 format. Example: "-07:00"
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::include_iso_8601_format,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::load_iso_8601_format,
            FnInStruct,
            hidden
        )]
        pub fn load_iso_8601_format(
            &mut self,
            options: ICU4XIsoTimeZoneOptions,
        ) -> Result<(), ICU4XError> {
            self.0.include_iso_8601_format(
                options.format.into(),
                options.minutes.into(),
                options.seconds.into(),
            )?;
            Ok(())
        }

        /// Formats a [`ICU4XCustomTimeZone`] to a string.
        #[diplomat::rust_link(icu::datetime::time_zone::TimeZoneFormatter::format, FnInStruct)]
        #[diplomat::rust_link(
            icu::datetime::time_zone::TimeZoneFormatter::format_to_string,
            FnInStruct
        )]
        pub fn format_custom_time_zone(
            &self,
            value: &ICU4XCustomTimeZone,
            write: &mut diplomat_runtime::DiplomatWriteable,
        ) -> Result<(), ICU4XError> {
            self.0.format(&value.0).write_to(write)?;
            Ok(())
        }
    }
}

impl From<ffi::ICU4XIsoTimeZoneOptions> for TimeZoneFormatterOptions {
    fn from(other: ffi::ICU4XIsoTimeZoneOptions) -> Self {
        FallbackFormat::Iso8601(
            other.format.into(),
            other.minutes.into(),
            other.seconds.into(),
        )
        .into()
    }
}