Bug 1869647 - Mark hasStorageAccess.sub.https.window.html as intermittent after wpt...
[gecko.git] / intl / icu_capi / src / collator.rs
blobbbf56786b70dee06a1ab5258a2f581ff5125cd32
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 ).
5 #[diplomat::bridge]
6 pub mod ffi {
7     use alloc::boxed::Box;
8     use icu_collator::{Collator, CollatorOptions};
10     use crate::{
11         common::ffi::ICU4XOrdering, errors::ffi::ICU4XError, locale::ffi::ICU4XLocale,
12         provider::ffi::ICU4XDataProvider,
13     };
15     #[diplomat::opaque]
16     #[diplomat::rust_link(icu::collator::Collator, Struct)]
17     pub struct ICU4XCollator(pub Collator);
19     #[diplomat::rust_link(icu::collator::CollatorOptions, Struct)]
20     #[diplomat::rust_link(icu::collator::CollatorOptions::new, FnInStruct, hidden)]
21     pub struct ICU4XCollatorOptionsV1 {
22         pub strength: ICU4XCollatorStrength,
23         pub alternate_handling: ICU4XCollatorAlternateHandling,
24         pub case_first: ICU4XCollatorCaseFirst,
25         pub max_variable: ICU4XCollatorMaxVariable,
26         pub case_level: ICU4XCollatorCaseLevel,
27         pub numeric: ICU4XCollatorNumeric,
28         pub backward_second_level: ICU4XCollatorBackwardSecondLevel,
29     }
31     #[diplomat::rust_link(icu::collator::Strength, Enum)]
32     pub enum ICU4XCollatorStrength {
33         Auto = 0,
34         Primary = 1,
35         Secondary = 2,
36         Tertiary = 3,
37         Quaternary = 4,
38         Identical = 5,
39     }
41     #[diplomat::rust_link(icu::collator::AlternateHandling, Enum)]
42     pub enum ICU4XCollatorAlternateHandling {
43         Auto = 0,
44         NonIgnorable = 1,
45         Shifted = 2,
46     }
48     #[diplomat::rust_link(icu::collator::CaseFirst, Enum)]
49     pub enum ICU4XCollatorCaseFirst {
50         Auto = 0,
51         Off = 1,
52         LowerFirst = 2,
53         UpperFirst = 3,
54     }
56     #[diplomat::rust_link(icu::collator::MaxVariable, Enum)]
57     pub enum ICU4XCollatorMaxVariable {
58         Auto = 0,
59         Space = 1,
60         Punctuation = 2,
61         Symbol = 3,
62         Currency = 4,
63     }
65     #[diplomat::rust_link(icu::collator::CaseLevel, Enum)]
66     pub enum ICU4XCollatorCaseLevel {
67         Auto = 0,
68         Off = 1,
69         On = 2,
70     }
72     #[diplomat::rust_link(icu::collator::Numeric, Enum)]
73     pub enum ICU4XCollatorNumeric {
74         Auto = 0,
75         Off = 1,
76         On = 2,
77     }
79     #[diplomat::rust_link(icu::collator::BackwardSecondLevel, Enum)]
80     pub enum ICU4XCollatorBackwardSecondLevel {
81         Auto = 0,
82         Off = 1,
83         On = 2,
84     }
86     impl ICU4XCollator {
87         /// Construct a new Collator instance.
88         #[diplomat::rust_link(icu::collator::Collator::try_new, FnInStruct)]
89         pub fn create_v1(
90             provider: &ICU4XDataProvider,
91             locale: &ICU4XLocale,
92             options: ICU4XCollatorOptionsV1,
93         ) -> Result<Box<ICU4XCollator>, ICU4XError> {
94             let locale = locale.to_datalocale();
95             let options = CollatorOptions::from(options);
97             Ok(Box::new(ICU4XCollator(call_constructor!(
98                 Collator::try_new,
99                 Collator::try_new_with_any_provider,
100                 Collator::try_new_with_buffer_provider,
101                 provider,
102                 &locale,
103                 options,
104             )?)))
105         }
107         /// Compare potentially ill-formed UTF-8 strings.
108         ///
109         /// Ill-formed input is compared
110         /// as if errors had been replaced with REPLACEMENT CHARACTERs according
111         /// to the WHATWG Encoding Standard.
112         #[diplomat::rust_link(icu::collator::Collator::compare_utf8, FnInStruct)]
113         #[diplomat::attr(dart, disable)]
114         pub fn compare(&self, left: &str, right: &str) -> ICU4XOrdering {
115             let left = left.as_bytes(); // #2520
116             let right = right.as_bytes(); // #2520
117             self.0.compare_utf8(left, right).into()
118         }
120         /// Compare guaranteed well-formed UTF-8 strings.
121         ///
122         /// Note: In C++, passing ill-formed UTF-8 strings is undefined behavior
123         /// (and may be memory-unsafe to do so, too).
124         #[diplomat::rust_link(icu::collator::Collator::compare, FnInStruct)]
125         #[diplomat::attr(dart, rename = "compare")]
126         pub fn compare_valid_utf8(&self, left: &str, right: &str) -> ICU4XOrdering {
127             self.0.compare(left, right).into()
128         }
130         /// Compare potentially ill-formed UTF-16 strings, with unpaired surrogates
131         /// compared as REPLACEMENT CHARACTER.
132         #[diplomat::rust_link(icu::collator::Collator::compare_utf16, FnInStruct)]
133         pub fn compare_utf16(&self, left: &[u16], right: &[u16]) -> ICU4XOrdering {
134             self.0.compare_utf16(left, right).into()
135         }
136     }
139 use icu_collator::{
140     AlternateHandling, BackwardSecondLevel, CaseFirst, CaseLevel, CollatorOptions, MaxVariable,
141     Numeric, Strength,
144 impl From<ffi::ICU4XCollatorStrength> for Option<Strength> {
145     fn from(strength: ffi::ICU4XCollatorStrength) -> Option<Strength> {
146         match strength {
147             ffi::ICU4XCollatorStrength::Auto => None,
148             ffi::ICU4XCollatorStrength::Primary => Some(Strength::Primary),
149             ffi::ICU4XCollatorStrength::Secondary => Some(Strength::Secondary),
150             ffi::ICU4XCollatorStrength::Tertiary => Some(Strength::Tertiary),
151             ffi::ICU4XCollatorStrength::Quaternary => Some(Strength::Quaternary),
152             ffi::ICU4XCollatorStrength::Identical => Some(Strength::Identical),
153         }
154     }
157 impl From<ffi::ICU4XCollatorAlternateHandling> for Option<AlternateHandling> {
158     fn from(alternate_handling: ffi::ICU4XCollatorAlternateHandling) -> Option<AlternateHandling> {
159         match alternate_handling {
160             ffi::ICU4XCollatorAlternateHandling::Auto => None,
161             ffi::ICU4XCollatorAlternateHandling::NonIgnorable => {
162                 Some(AlternateHandling::NonIgnorable)
163             }
164             ffi::ICU4XCollatorAlternateHandling::Shifted => Some(AlternateHandling::Shifted),
165         }
166     }
169 impl From<ffi::ICU4XCollatorCaseFirst> for Option<CaseFirst> {
170     fn from(case_first: ffi::ICU4XCollatorCaseFirst) -> Option<CaseFirst> {
171         match case_first {
172             ffi::ICU4XCollatorCaseFirst::Auto => None,
173             ffi::ICU4XCollatorCaseFirst::Off => Some(CaseFirst::Off),
174             ffi::ICU4XCollatorCaseFirst::LowerFirst => Some(CaseFirst::LowerFirst),
175             ffi::ICU4XCollatorCaseFirst::UpperFirst => Some(CaseFirst::UpperFirst),
176         }
177     }
180 impl From<ffi::ICU4XCollatorMaxVariable> for Option<MaxVariable> {
181     fn from(max_variable: ffi::ICU4XCollatorMaxVariable) -> Option<MaxVariable> {
182         match max_variable {
183             ffi::ICU4XCollatorMaxVariable::Auto => None,
184             ffi::ICU4XCollatorMaxVariable::Space => Some(MaxVariable::Space),
185             ffi::ICU4XCollatorMaxVariable::Punctuation => Some(MaxVariable::Punctuation),
186             ffi::ICU4XCollatorMaxVariable::Symbol => Some(MaxVariable::Symbol),
187             ffi::ICU4XCollatorMaxVariable::Currency => Some(MaxVariable::Currency),
188         }
189     }
192 impl From<ffi::ICU4XCollatorCaseLevel> for Option<CaseLevel> {
193     fn from(case_level: ffi::ICU4XCollatorCaseLevel) -> Option<CaseLevel> {
194         match case_level {
195             ffi::ICU4XCollatorCaseLevel::Auto => None,
196             ffi::ICU4XCollatorCaseLevel::Off => Some(CaseLevel::Off),
197             ffi::ICU4XCollatorCaseLevel::On => Some(CaseLevel::On),
198         }
199     }
202 impl From<ffi::ICU4XCollatorNumeric> for Option<Numeric> {
203     fn from(numeric: ffi::ICU4XCollatorNumeric) -> Option<Numeric> {
204         match numeric {
205             ffi::ICU4XCollatorNumeric::Auto => None,
206             ffi::ICU4XCollatorNumeric::Off => Some(Numeric::Off),
207             ffi::ICU4XCollatorNumeric::On => Some(Numeric::On),
208         }
209     }
212 impl From<ffi::ICU4XCollatorBackwardSecondLevel> for Option<BackwardSecondLevel> {
213     fn from(
214         backward_second_level: ffi::ICU4XCollatorBackwardSecondLevel,
215     ) -> Option<BackwardSecondLevel> {
216         match backward_second_level {
217             ffi::ICU4XCollatorBackwardSecondLevel::Auto => None,
218             ffi::ICU4XCollatorBackwardSecondLevel::Off => Some(BackwardSecondLevel::Off),
219             ffi::ICU4XCollatorBackwardSecondLevel::On => Some(BackwardSecondLevel::On),
220         }
221     }
224 impl From<ffi::ICU4XCollatorOptionsV1> for CollatorOptions {
225     fn from(options: ffi::ICU4XCollatorOptionsV1) -> CollatorOptions {
226         let mut result = CollatorOptions::new();
227         result.strength = options.strength.into();
228         result.alternate_handling = options.alternate_handling.into();
229         result.case_first = options.case_first.into();
230         result.max_variable = options.max_variable.into();
231         result.case_level = options.case_level.into();
232         result.numeric = options.numeric.into();
233         result.backward_second_level = options.backward_second_level.into();
235         result
236     }