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
// 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 ).

#[diplomat::bridge]
pub mod ffi {
    use crate::errors::ffi::ICU4XError;
    use crate::provider::ffi::ICU4XDataProvider;
    use alloc::boxed::Box;
    use icu_timezone::{
        TimeZoneBcp47Id, TimeZoneIdMapper, TimeZoneIdMapperWithFastCanonicalization,
    };
    use tinystr::TinyAsciiStr;

    /// A mapper between IANA time zone identifiers and BCP-47 time zone identifiers.
    ///
    /// This mapper supports two-way mapping, but it is optimized for the case of IANA to BCP-47.
    /// It also supports normalizing and canonicalizing the IANA strings.
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::timezone::TimeZoneIdMapper, Struct)]
    #[diplomat::rust_link(icu::timezone::TimeZoneIdMapper::as_borrowed, FnInStruct, hidden)]
    #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperBorrowed, Struct, hidden)]
    #[diplomat::rust_link(icu::timezone::NormalizedIana, Struct, hidden)]
    pub struct ICU4XTimeZoneIdMapper(pub TimeZoneIdMapper);

    impl ICU4XTimeZoneIdMapper {
        #[diplomat::rust_link(icu::timezone::TimeZoneIdMapper::new, FnInStruct)]
        #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
        pub fn create(
            provider: &ICU4XDataProvider,
        ) -> Result<Box<ICU4XTimeZoneIdMapper>, ICU4XError> {
            Ok(Box::new(ICU4XTimeZoneIdMapper(call_constructor!(
                TimeZoneIdMapper::new [r => Ok(r)],
                TimeZoneIdMapper::try_new_with_any_provider,
                TimeZoneIdMapper::try_new_with_buffer_provider,
                provider,
            )?)))
        }

        #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperBorrowed::iana_to_bcp47, FnInStruct)]
        #[diplomat::rust_link(
            icu::timezone::TimeZoneIdMapperBorrowed::iana_bytes_to_bcp47,
            FnInStruct,
            hidden
        )]
        pub fn iana_to_bcp47(
            &self,
            value: &DiplomatStr,
            write: &mut diplomat_runtime::DiplomatWriteable,
        ) -> Result<(), ICU4XError> {
            use writeable::Writeable;
            let handle = self.0.as_borrowed();
            if let Some(s) = handle.iana_bytes_to_bcp47(value) {
                Ok(s.0.write_to(write)?)
            } else {
                Err(ICU4XError::TimeZoneInvalidIdError)
            }
        }

        #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperBorrowed::normalize_iana, FnInStruct)]
        pub fn normalize_iana(
            &self,
            value: &DiplomatStr,
            write: &mut diplomat_runtime::DiplomatWriteable,
        ) -> Result<(), ICU4XError> {
            use writeable::Writeable;
            let handle = self.0.as_borrowed();
            // Validate the UTF-8 here because it gets echoed back to the writeable
            let value = core::str::from_utf8(value)?;
            if let Some(s) = handle.normalize_iana(value) {
                Ok(s.0.write_to(write)?)
            } else {
                Err(ICU4XError::TimeZoneInvalidIdError)
            }
        }

        #[diplomat::rust_link(
            icu::timezone::TimeZoneIdMapperBorrowed::canonicalize_iana,
            FnInStruct
        )]
        pub fn canonicalize_iana(
            &self,
            value: &DiplomatStr,
            write: &mut diplomat_runtime::DiplomatWriteable,
        ) -> Result<(), ICU4XError> {
            use writeable::Writeable;
            let handle = self.0.as_borrowed();
            // Validate the UTF-8 here because it gets echoed back to the writeable
            let value = core::str::from_utf8(value)?;
            if let Some(s) = handle.canonicalize_iana(value) {
                Ok(s.0.write_to(write)?)
            } else {
                Err(ICU4XError::TimeZoneInvalidIdError)
            }
        }

        #[diplomat::rust_link(
            icu::timezone::TimeZoneIdMapperBorrowed::find_canonical_iana_from_bcp47,
            FnInStruct
        )]
        pub fn find_canonical_iana_from_bcp47(
            &self,
            value: &DiplomatStr,
            write: &mut diplomat_runtime::DiplomatWriteable,
        ) -> Result<(), ICU4XError> {
            use writeable::Writeable;
            let handle = self.0.as_borrowed();
            let bcp47_id = TimeZoneBcp47Id(
                TinyAsciiStr::from_bytes(value).map_err(|_| ICU4XError::TimeZoneInvalidIdError)?,
            );
            if let Some(s) = handle.find_canonical_iana_from_bcp47(bcp47_id) {
                Ok(s.write_to(write)?)
            } else {
                Err(ICU4XError::TimeZoneInvalidIdError)
            }
        }
    }

    /// A mapper between IANA time zone identifiers and BCP-47 time zone identifiers.
    ///
    /// This mapper supports two-way mapping, but it is optimized for the case of IANA to BCP-47.
    /// It also supports normalizing and canonicalizing the IANA strings.
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::timezone::TimeZoneIdMapperWithFastCanonicalization, Struct)]
    #[diplomat::rust_link(
        icu::timezone::TimeZoneIdMapperWithFastCanonicalization::as_borrowed,
        FnInStruct,
        hidden
    )]
    #[diplomat::rust_link(
        icu::timezone::TimeZoneIdMapperWithFastCanonicalization::inner,
        FnInStruct,
        hidden
    )]
    #[diplomat::rust_link(
        icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed,
        Struct,
        hidden
    )]
    #[diplomat::rust_link(
        icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed::inner,
        FnInStruct,
        hidden
    )]
    pub struct ICU4XTimeZoneIdMapperWithFastCanonicalization(
        pub TimeZoneIdMapperWithFastCanonicalization<TimeZoneIdMapper>,
    );

    impl ICU4XTimeZoneIdMapperWithFastCanonicalization {
        #[diplomat::rust_link(
            icu::timezone::TimeZoneIdMapperWithFastCanonicalization::new,
            FnInStruct
        )]
        #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
        pub fn create(
            provider: &ICU4XDataProvider,
        ) -> Result<Box<ICU4XTimeZoneIdMapperWithFastCanonicalization>, ICU4XError> {
            Ok(Box::new(ICU4XTimeZoneIdMapperWithFastCanonicalization(
                call_constructor!(
                    TimeZoneIdMapperWithFastCanonicalization::new [r => Ok(r)],
                    TimeZoneIdMapperWithFastCanonicalization::try_new_with_any_provider,
                    TimeZoneIdMapperWithFastCanonicalization::try_new_with_buffer_provider,
                    provider,
                )?,
            )))
        }

        #[diplomat::rust_link(
            icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed::canonicalize_iana,
            FnInStruct
        )]
        pub fn canonicalize_iana(
            &self,
            value: &DiplomatStr,
            write: &mut diplomat_runtime::DiplomatWriteable,
        ) -> Result<(), ICU4XError> {
            use writeable::Writeable;
            let handle = self.0.as_borrowed();
            // Validate the UTF-8 here because it gets echoed back to the writeable
            let value = core::str::from_utf8(value)?;
            if let Some(s) = handle.canonicalize_iana(value) {
                Ok(s.0.write_to(write)?)
            } else {
                Err(ICU4XError::TimeZoneInvalidIdError)
            }
        }

        #[diplomat::rust_link(
            icu::timezone::TimeZoneIdMapperWithFastCanonicalizationBorrowed::canonical_iana_from_bcp47,
            FnInStruct
        )]
        pub fn canonical_iana_from_bcp47(
            &self,
            value: &DiplomatStr,
            write: &mut diplomat_runtime::DiplomatWriteable,
        ) -> Result<(), ICU4XError> {
            use writeable::Writeable;
            let handle = self.0.as_borrowed();
            let bcp47_id = TimeZoneBcp47Id(
                TinyAsciiStr::from_bytes(value).map_err(|_| ICU4XError::TimeZoneInvalidIdError)?,
            );
            if let Some(s) = handle.canonical_iana_from_bcp47(bcp47_id) {
                Ok(s.write_to(write)?)
            } else {
                Err(ICU4XError::TimeZoneInvalidIdError)
            }
        }
    }
}