Debian Codegraph @

intl/icu_capi/src/collator.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_collator::{Collator, CollatorOptions};
9
10 use crate::{
11 common::ffi::ICU4XOrdering, errors::ffi::ICU4XError, locale::ffi::ICU4XLocale,
12 provider::ffi::ICU4XDataProvider,
13 };
14
15 #[diplomat::opaque]
16 #[diplomat::rust_link(icu::collator::Collator, Struct)]
17 pub struct ICU4XCollator(pub Collator);
18
19 #[diplomat::rust_link(icu::collator::CollatorOptions, Struct)]
20 #[diplomat::rust_link(icu::collator::CollatorOptions::new, FnInStruct, hidden)]
21 #[diplomat::attr(dart, rename = "CollatorOptions")]
22 pub struct ICU4XCollatorOptionsV1 {
23 pub strength: ICU4XCollatorStrength,
24 pub alternate_handling: ICU4XCollatorAlternateHandling,
25 pub case_first: ICU4XCollatorCaseFirst,
26 pub max_variable: ICU4XCollatorMaxVariable,
27 pub case_level: ICU4XCollatorCaseLevel,
28 pub numeric: ICU4XCollatorNumeric,
29 pub backward_second_level: ICU4XCollatorBackwardSecondLevel,
30 }
31
32 // Note the flipped order of the words `Collator` and `Resolved`, because
33 // in FFI `Collator` is part of the `ICU4XCollator` prefix, but in Rust,
34 // `ResolvedCollatorOptions` makes more sense as English.
35 #[diplomat::rust_link(icu::collator::ResolvedCollatorOptions, Struct)]
36 #[diplomat::out]
37 #[diplomat::attr(dart, rename = "ResolvedCollatorOptions")]
38 pub struct ICU4XCollatorResolvedOptionsV1 {
39 pub strength: ICU4XCollatorStrength,
40 pub alternate_handling: ICU4XCollatorAlternateHandling,
41 pub case_first: ICU4XCollatorCaseFirst,
42 pub max_variable: ICU4XCollatorMaxVariable,
43 pub case_level: ICU4XCollatorCaseLevel,
44 pub numeric: ICU4XCollatorNumeric,
45 pub backward_second_level: ICU4XCollatorBackwardSecondLevel,
46 }
47
48 #[diplomat::rust_link(icu::collator::Strength, Enum)]
49 #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)]
50 pub enum ICU4XCollatorStrength {
51 Auto = 0,
52 Primary = 1,
53 Secondary = 2,
54 Tertiary = 3,
55 Quaternary = 4,
56 Identical = 5,
57 }
58
59 #[diplomat::rust_link(icu::collator::AlternateHandling, Enum)]
60 #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)]
61 pub enum ICU4XCollatorAlternateHandling {
62 Auto = 0,
63 NonIgnorable = 1,
64 Shifted = 2,
65 }
66
67 #[diplomat::rust_link(icu::collator::CaseFirst, Enum)]
68 #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)]
69 pub enum ICU4XCollatorCaseFirst {
70 Auto = 0,
71 Off = 1,
72 LowerFirst = 2,
73 UpperFirst = 3,
74 }
75
76 #[diplomat::rust_link(icu::collator::MaxVariable, Enum)]
77 #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)]
78 pub enum ICU4XCollatorMaxVariable {
79 Auto = 0,
80 Space = 1,
81 Punctuation = 2,
82 Symbol = 3,
83 Currency = 4,
84 }
85
86 #[diplomat::rust_link(icu::collator::CaseLevel, Enum)]
87 #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)]
88 pub enum ICU4XCollatorCaseLevel {
89 Auto = 0,
90 Off = 1,
91 On = 2,
92 }
93
94 #[diplomat::rust_link(icu::collator::Numeric, Enum)]
95 #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)]
96 pub enum ICU4XCollatorNumeric {
97 Auto = 0,
98 Off = 1,
99 On = 2,
100 }
101
102 #[diplomat::rust_link(icu::collator::BackwardSecondLevel, Enum)]
103 #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)]
104 pub enum ICU4XCollatorBackwardSecondLevel {
105 Auto = 0,
106 Off = 1,
107 On = 2,
108 }
109
110 impl ICU4XCollator {
111 /// Construct a new Collator instance.
112 #[diplomat::rust_link(icu::collator::Collator::try_new, FnInStruct)]
113 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
114 pub fn create_v1(
115 provider: &ICU4XDataProvider,
116 locale: &ICU4XLocale,
117 options: ICU4XCollatorOptionsV1,
118 ) -> Result<Box<ICU4XCollator>, ICU4XError> {
119 let locale = locale.to_datalocale();
120 let options = CollatorOptions::from(options);
121
122 Ok(Box::new(ICU4XCollator(call_constructor!(
123 Collator::try_new,
124 Collator::try_new_with_any_provider,
125 Collator::try_new_with_buffer_provider,
126 provider,
127 &locale,
128 options,
129 )?)))
130 }
131
132 /// Compare two strings.
133 ///
134 /// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
135 /// to the WHATWG Encoding Standard.
136 #[diplomat::rust_link(icu::collator::Collator::compare_utf8, FnInStruct)]
137 #[diplomat::attr(dart, disable)]
138 pub fn compare(&self, left: &DiplomatStr, right: &DiplomatStr) -> ICU4XOrdering {
139 self.0.compare_utf8(left, right).into()
140 }
141
142 /// Compare two strings.
143 #[diplomat::rust_link(icu::collator::Collator::compare, FnInStruct)]
144 #[diplomat::attr(*, disable)]
145 pub fn compare_valid_utf8(&self, left: &str, right: &str) -> ICU4XOrdering {
146 self.0.compare(left, right).into()
147 }
148
149 /// Compare two strings.
150 ///
151 /// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
152 /// to the WHATWG Encoding Standard.
153 #[diplomat::rust_link(icu::collator::Collator::compare_utf16, FnInStruct)]
154 #[diplomat::attr(dart, disable)]
155 pub fn compare_utf16(&self, left: &DiplomatStr16, right: &DiplomatStr16) -> ICU4XOrdering {
156 self.0.compare_utf16(left, right).into()
157 }
158
159 /// Compare two strings.
160 ///
161 /// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
162 /// to the WHATWG Encoding Standard.
163 #[diplomat::rust_link(icu::collator::Collator::compare_utf16, FnInStruct)]
164 #[diplomat::skip_if_ast]
165 #[diplomat::attr(dart, rename = "compare")]
166 pub fn compare_utf16_(
167 &self,
168 left: &DiplomatStr16,
169 right: &DiplomatStr16,
170 ) -> core::cmp::Ordering {
171 self.0.compare_utf16(left, right)
172 }
173
174 /// The resolved options showing how the default options, the requested options,
175 /// and the options from locale data were combined. None of the struct fields
176 /// will have `Auto` as the value.
177 #[diplomat::rust_link(icu::collator::Collator::resolved_options, FnInStruct)]
178 #[diplomat::attr(supports = accessors, getter)]
179 pub fn resolved_options(&self) -> ICU4XCollatorResolvedOptionsV1 {
180 self.0.resolved_options().into()
181 }
182 }
183 }
184
185 use icu_collator::{
186 AlternateHandling, BackwardSecondLevel, CaseFirst, CaseLevel, CollatorOptions, MaxVariable,
187 Numeric, ResolvedCollatorOptions, Strength,
188 };
189
190 impl From<ffi::ICU4XCollatorStrength> for Option<Strength> {
191 fn from(strength: ffi::ICU4XCollatorStrength) -> Option<Strength> {
192 match strength {
193 ffi::ICU4XCollatorStrength::Auto => None,
194 ffi::ICU4XCollatorStrength::Primary => Some(Strength::Primary),
195 ffi::ICU4XCollatorStrength::Secondary => Some(Strength::Secondary),
196 ffi::ICU4XCollatorStrength::Tertiary => Some(Strength::Tertiary),
197 ffi::ICU4XCollatorStrength::Quaternary => Some(Strength::Quaternary),
198 ffi::ICU4XCollatorStrength::Identical => Some(Strength::Identical),
199 }
200 }
201 }
202
203 impl From<ffi::ICU4XCollatorAlternateHandling> for Option<AlternateHandling> {
204 fn from(alternate_handling: ffi::ICU4XCollatorAlternateHandling) -> Option<AlternateHandling> {
205 match alternate_handling {
206 ffi::ICU4XCollatorAlternateHandling::Auto => None,
207 ffi::ICU4XCollatorAlternateHandling::NonIgnorable => {
208 Some(AlternateHandling::NonIgnorable)
209 }
210 ffi::ICU4XCollatorAlternateHandling::Shifted => Some(AlternateHandling::Shifted),
211 }
212 }
213 }
214
215 impl From<ffi::ICU4XCollatorCaseFirst> for Option<CaseFirst> {
216 fn from(case_first: ffi::ICU4XCollatorCaseFirst) -> Option<CaseFirst> {
217 match case_first {
218 ffi::ICU4XCollatorCaseFirst::Auto => None,
219 ffi::ICU4XCollatorCaseFirst::Off => Some(CaseFirst::Off),
220 ffi::ICU4XCollatorCaseFirst::LowerFirst => Some(CaseFirst::LowerFirst),
221 ffi::ICU4XCollatorCaseFirst::UpperFirst => Some(CaseFirst::UpperFirst),
222 }
223 }
224 }
225
226 impl From<ffi::ICU4XCollatorMaxVariable> for Option<MaxVariable> {
227 fn from(max_variable: ffi::ICU4XCollatorMaxVariable) -> Option<MaxVariable> {
228 match max_variable {
229 ffi::ICU4XCollatorMaxVariable::Auto => None,
230 ffi::ICU4XCollatorMaxVariable::Space => Some(MaxVariable::Space),
231 ffi::ICU4XCollatorMaxVariable::Punctuation => Some(MaxVariable::Punctuation),
232 ffi::ICU4XCollatorMaxVariable::Symbol => Some(MaxVariable::Symbol),
233 ffi::ICU4XCollatorMaxVariable::Currency => Some(MaxVariable::Currency),
234 }
235 }
236 }
237
238 impl From<ffi::ICU4XCollatorCaseLevel> for Option<CaseLevel> {
239 fn from(case_level: ffi::ICU4XCollatorCaseLevel) -> Option<CaseLevel> {
240 match case_level {
241 ffi::ICU4XCollatorCaseLevel::Auto => None,
242 ffi::ICU4XCollatorCaseLevel::Off => Some(CaseLevel::Off),
243 ffi::ICU4XCollatorCaseLevel::On => Some(CaseLevel::On),
244 }
245 }
246 }
247
248 impl From<ffi::ICU4XCollatorNumeric> for Option<Numeric> {
249 fn from(numeric: ffi::ICU4XCollatorNumeric) -> Option<Numeric> {
250 match numeric {
251 ffi::ICU4XCollatorNumeric::Auto => None,
252 ffi::ICU4XCollatorNumeric::Off => Some(Numeric::Off),
253 ffi::ICU4XCollatorNumeric::On => Some(Numeric::On),
254 }
255 }
256 }
257
258 impl From<ffi::ICU4XCollatorBackwardSecondLevel> for Option<BackwardSecondLevel> {
259 fn from(
260 backward_second_level: ffi::ICU4XCollatorBackwardSecondLevel,
261 ) -> Option<BackwardSecondLevel> {
262 match backward_second_level {
263 ffi::ICU4XCollatorBackwardSecondLevel::Auto => None,
264 ffi::ICU4XCollatorBackwardSecondLevel::Off => Some(BackwardSecondLevel::Off),
265 ffi::ICU4XCollatorBackwardSecondLevel::On => Some(BackwardSecondLevel::On),
266 }
267 }
268 }
269
270 impl From<Strength> for ffi::ICU4XCollatorStrength {
271 fn from(strength: Strength) -> ffi::ICU4XCollatorStrength {
272 match strength {
273 Strength::Primary => ffi::ICU4XCollatorStrength::Primary,
274 Strength::Secondary => ffi::ICU4XCollatorStrength::Secondary,
275 Strength::Tertiary => ffi::ICU4XCollatorStrength::Tertiary,
276 Strength::Quaternary => ffi::ICU4XCollatorStrength::Quaternary,
277 Strength::Identical => ffi::ICU4XCollatorStrength::Identical,
278 _ => {
279 debug_assert!(false, "FFI out of sync");
280 ffi::ICU4XCollatorStrength::Identical // Highest we know of
281 }
282 }
283 }
284 }
285
286 impl From<AlternateHandling> for ffi::ICU4XCollatorAlternateHandling {
287 fn from(alternate_handling: AlternateHandling) -> ffi::ICU4XCollatorAlternateHandling {
288 match alternate_handling {
289 AlternateHandling::NonIgnorable => ffi::ICU4XCollatorAlternateHandling::NonIgnorable,
290 AlternateHandling::Shifted => ffi::ICU4XCollatorAlternateHandling::Shifted,
291 _ => {
292 debug_assert!(false, "FFI out of sync");
293 // Possible future values: ShiftTrimmed, Blanked
294 ffi::ICU4XCollatorAlternateHandling::Shifted // Highest we know of
295 }
296 }
297 }
298 }
299
300 impl From<CaseFirst> for ffi::ICU4XCollatorCaseFirst {
301 fn from(case_first: CaseFirst) -> ffi::ICU4XCollatorCaseFirst {
302 match case_first {
303 CaseFirst::Off => ffi::ICU4XCollatorCaseFirst::Off,
304 CaseFirst::LowerFirst => ffi::ICU4XCollatorCaseFirst::LowerFirst,
305 CaseFirst::UpperFirst => ffi::ICU4XCollatorCaseFirst::UpperFirst,
306 _ => {
307 debug_assert!(false, "FFI out of sync");
308 // Does it even make sense that `CaseFirst` is non-exhaustive?
309 ffi::ICU4XCollatorCaseFirst::Off // The most neutral value
310 }
311 }
312 }
313 }
314
315 impl From<MaxVariable> for ffi::ICU4XCollatorMaxVariable {
316 fn from(max_variable: MaxVariable) -> ffi::ICU4XCollatorMaxVariable {
317 match max_variable {
318 MaxVariable::Space => ffi::ICU4XCollatorMaxVariable::Space,
319 MaxVariable::Punctuation => ffi::ICU4XCollatorMaxVariable::Punctuation,
320 MaxVariable::Symbol => ffi::ICU4XCollatorMaxVariable::Symbol,
321 MaxVariable::Currency => ffi::ICU4XCollatorMaxVariable::Currency,
322 _ => {
323 debug_assert!(false, "FFI out of sync");
324 ffi::ICU4XCollatorMaxVariable::Currency // Highest we know of
325 }
326 }
327 }
328 }
329
330 impl From<CaseLevel> for ffi::ICU4XCollatorCaseLevel {
331 fn from(case_level: CaseLevel) -> ffi::ICU4XCollatorCaseLevel {
332 match case_level {
333 CaseLevel::Off => ffi::ICU4XCollatorCaseLevel::Off,
334 CaseLevel::On => ffi::ICU4XCollatorCaseLevel::On,
335 _ => {
336 debug_assert!(false, "FFI out of sync");
337 ffi::ICU4XCollatorCaseLevel::On // The most enabled that we know of
338 }
339 }
340 }
341 }
342
343 impl From<Numeric> for ffi::ICU4XCollatorNumeric {
344 fn from(numeric: Numeric) -> ffi::ICU4XCollatorNumeric {
345 match numeric {
346 Numeric::Off => ffi::ICU4XCollatorNumeric::Off,
347 Numeric::On => ffi::ICU4XCollatorNumeric::On,
348 _ => {
349 debug_assert!(false, "FFI out of sync");
350 ffi::ICU4XCollatorNumeric::On // The most enabled that we know of
351 }
352 }
353 }
354 }
355
356 impl From<BackwardSecondLevel> for ffi::ICU4XCollatorBackwardSecondLevel {
357 fn from(backward_second_level: BackwardSecondLevel) -> ffi::ICU4XCollatorBackwardSecondLevel {
358 match backward_second_level {
359 BackwardSecondLevel::Off => ffi::ICU4XCollatorBackwardSecondLevel::Off,
360 BackwardSecondLevel::On => ffi::ICU4XCollatorBackwardSecondLevel::On,
361 _ => {
362 debug_assert!(false, "FFI out of sync");
363 ffi::ICU4XCollatorBackwardSecondLevel::On // The most enabled that we know of
364 }
365 }
366 }
367 }
368
369 impl From<ffi::ICU4XCollatorOptionsV1> for CollatorOptions {
370 fn from(options: ffi::ICU4XCollatorOptionsV1) -> CollatorOptions {
371 let mut result = CollatorOptions::new();
372 result.strength = options.strength.into();
373 result.alternate_handling = options.alternate_handling.into();
374 result.case_first = options.case_first.into();
375 result.max_variable = options.max_variable.into();
376 result.case_level = options.case_level.into();
377 result.numeric = options.numeric.into();
378 result.backward_second_level = options.backward_second_level.into();
379
380 result
381 }
382 }
383
384 impl From<ResolvedCollatorOptions> for ffi::ICU4XCollatorResolvedOptionsV1 {
385 fn from(options: ResolvedCollatorOptions) -> ffi::ICU4XCollatorResolvedOptionsV1 {
386 Self {
387 strength: options.strength.into(),
388 alternate_handling: options.alternate_handling.into(),
389 case_first: options.case_first.into(),
390 max_variable: options.max_variable.into(),
391 case_level: options.case_level.into(),
392 numeric: options.numeric.into(),
393 backward_second_level: options.backward_second_level.into(),
394 }
395 }
396 }