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
// 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 alloc::boxed::Box;
    use icu_locid_transform::fallback::LocaleFallbackConfig;
    use icu_locid_transform::fallback::LocaleFallbackIterator;
    use icu_locid_transform::fallback::LocaleFallbackPriority;
    use icu_locid_transform::fallback::LocaleFallbackerWithConfig;
    use icu_locid_transform::LocaleFallbacker;

    use crate::{
        errors::ffi::ICU4XError, locale::ffi::ICU4XLocale, provider::ffi::ICU4XDataProvider,
    };

    /// An object that runs the ICU4X locale fallback algorithm.
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::locid_transform::fallback::LocaleFallbacker, Struct)]
    #[diplomat::rust_link(
        icu::locid_transform::fallback::LocaleFallbackerBorrowed,
        Struct,
        hidden
    )]
    pub struct ICU4XLocaleFallbacker(pub LocaleFallbacker);

    /// Priority mode for the ICU4X fallback algorithm.
    #[diplomat::enum_convert(LocaleFallbackPriority, needs_wildcard)]
    #[diplomat::rust_link(icu::locid_transform::fallback::LocaleFallbackPriority, Enum)]
    #[diplomat::rust_link(
        icu::locid_transform::fallback::LocaleFallbackPriority::const_default,
        FnInEnum,
        hidden
    )]
    pub enum ICU4XLocaleFallbackPriority {
        Language = 0,
        Region = 1,
        Collation = 2,
    }

    /// What additional data is required to load when performing fallback.
    #[diplomat::rust_link(icu::locid_transform::fallback::LocaleFallbackSupplement, Enum)]
    #[diplomat::rust_link(
        icu::locid_transform::fallback::LocaleFallbackSupplement::const_default,
        FnInEnum,
        hidden
    )]
    pub enum ICU4XLocaleFallbackSupplement {
        None = 0,
        Collation = 1,
    }

    /// Collection of configurations for the ICU4X fallback algorithm.
    #[diplomat::rust_link(icu::locid_transform::fallback::LocaleFallbackConfig, Struct)]
    #[diplomat::rust_link(
        icu::locid_transform::fallback::LocaleFallbackConfig::const_default,
        FnInStruct,
        hidden
    )]
    pub struct ICU4XLocaleFallbackConfig<'a> {
        /// Choice of priority mode.
        pub priority: ICU4XLocaleFallbackPriority,
        /// An empty string is considered `None`.
        pub extension_key: &'a DiplomatStr,
        /// Fallback supplement data key to customize fallback rules.
        pub fallback_supplement: ICU4XLocaleFallbackSupplement,
    }

    /// An object that runs the ICU4X locale fallback algorithm with specific configurations.
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::locid_transform::fallback::LocaleFallbacker, Struct)]
    #[diplomat::rust_link(icu::locid_transform::fallback::LocaleFallbackerWithConfig, Struct)]
    pub struct ICU4XLocaleFallbackerWithConfig<'a>(pub LocaleFallbackerWithConfig<'a>);

    /// An iterator over the locale under fallback.
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::locid_transform::fallback::LocaleFallbackIterator, Struct)]
    pub struct ICU4XLocaleFallbackIterator<'a>(pub LocaleFallbackIterator<'a, 'a>);

    impl ICU4XLocaleFallbacker {
        /// Creates a new `ICU4XLocaleFallbacker` from a data provider.
        #[diplomat::rust_link(icu::locid_transform::fallback::LocaleFallbacker::new, FnInStruct)]
        #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
        pub fn create(
            provider: &ICU4XDataProvider,
        ) -> Result<Box<ICU4XLocaleFallbacker>, ICU4XError> {
            Ok(Box::new(ICU4XLocaleFallbacker(call_constructor!(
                LocaleFallbacker::new [r => Ok(r.static_to_owned())],
                LocaleFallbacker::try_new_with_any_provider,
                LocaleFallbacker::try_new_with_buffer_provider,
                provider,
            )?)))
        }

        /// Creates a new `ICU4XLocaleFallbacker` without data for limited functionality.
        #[diplomat::rust_link(
            icu::locid_transform::fallback::LocaleFallbacker::new_without_data,
            FnInStruct
        )]
        #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "without_data")]
        pub fn create_without_data() -> Box<ICU4XLocaleFallbacker> {
            Box::new(ICU4XLocaleFallbacker(LocaleFallbacker::new_without_data()))
        }

        /// Associates this `ICU4XLocaleFallbacker` with configuration options.
        #[diplomat::rust_link(
            icu::locid_transform::fallback::LocaleFallbacker::for_config,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::locid_transform::fallback::LocaleFallbackerBorrowed::for_config,
            FnInStruct,
            hidden
        )]
        pub fn for_config<'a, 'temp>(
            &'a self,
            config: ICU4XLocaleFallbackConfig<'temp>,
        ) -> Result<Box<ICU4XLocaleFallbackerWithConfig<'a>>, ICU4XError> {
            Ok(Box::new(ICU4XLocaleFallbackerWithConfig(
                self.0.for_config(LocaleFallbackConfig::try_from(config)?),
            )))
        }
    }

    impl<'a> ICU4XLocaleFallbackerWithConfig<'a> {
        /// Creates an iterator from a locale with each step of fallback.
        #[diplomat::rust_link(
            icu::locid_transform::fallback::LocaleFallbacker::fallback_for,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::locid_transform::fallback::LocaleFallbackerBorrowed::fallback_for,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::locid_transform::fallback::LocaleFallbackerWithConfig::fallback_for,
            FnInStruct,
            hidden
        )]
        pub fn fallback_for_locale<'b: 'a, 'temp>(
            &'b self,
            locale: &'temp ICU4XLocale,
        ) -> Box<ICU4XLocaleFallbackIterator<'a>> {
            Box::new(ICU4XLocaleFallbackIterator(
                self.0.fallback_for((&locale.0).into()),
            ))
        }
    }

    impl<'a> ICU4XLocaleFallbackIterator<'a> {
        /// Gets a snapshot of the current state of the locale.
        #[diplomat::rust_link(
            icu::locid_transform::fallback::LocaleFallbackIterator::get,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::locid_transform::fallback::LocaleFallbackIterator::take,
            FnInStruct,
            hidden
        )]
        #[diplomat::attr(*, disable)]
        pub fn get(&self) -> Box<ICU4XLocale> {
            Box::new(ICU4XLocale(self.0.get().clone().into_locale()))
        }

        /// Performs one step of the fallback algorithm, mutating the locale.
        #[diplomat::rust_link(
            icu::locid_transform::fallback::LocaleFallbackIterator::step,
            FnInStruct
        )]
        #[diplomat::attr(*, disable)]
        pub fn step(&mut self) {
            self.0.step();
        }

        /// A combination of `get` and `step`. Returns the value that `get` would return
        /// and advances the iterator until hitting `und`.
        #[diplomat::attr(supports = iterators, iterator)]
        #[diplomat::skip_if_ast]
        pub fn next(&mut self) -> Option<Box<ICU4XLocale>> {
            let current = self.get();
            if current.0 == icu_locid::Locale::UND {
                None
            } else {
                self.step();
                Some(current)
            }
        }
    }
}

impl TryFrom<ffi::ICU4XLocaleFallbackConfig<'_>>
    for icu_locid_transform::fallback::LocaleFallbackConfig
{
    type Error = crate::errors::ffi::ICU4XError;
    fn try_from(other: ffi::ICU4XLocaleFallbackConfig) -> Result<Self, Self::Error> {
        let mut result = Self::default();
        result.priority = other.priority.into();
        result.extension_key = match other.extension_key {
            b"" => None,
            s => Some(icu_locid::extensions::unicode::Key::try_from_bytes(s)?),
        };
        result.fallback_supplement = match other.fallback_supplement {
            ffi::ICU4XLocaleFallbackSupplement::None => None,
            ffi::ICU4XLocaleFallbackSupplement::Collation => {
                Some(icu_locid_transform::fallback::LocaleFallbackSupplement::Collation)
            }
        };
        Ok(result)
    }
}