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 ).
8 use icu_collator::{Collator, CollatorOptions};
11 common::ffi::ICU4XOrdering, errors::ffi::ICU4XError, locale::ffi::ICU4XLocale,
12 provider::ffi::ICU4XDataProvider,
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,
31 #[diplomat::rust_link(icu::collator::Strength, Enum)]
32 pub enum ICU4XCollatorStrength {
41 #[diplomat::rust_link(icu::collator::AlternateHandling, Enum)]
42 pub enum ICU4XCollatorAlternateHandling {
48 #[diplomat::rust_link(icu::collator::CaseFirst, Enum)]
49 pub enum ICU4XCollatorCaseFirst {
56 #[diplomat::rust_link(icu::collator::MaxVariable, Enum)]
57 pub enum ICU4XCollatorMaxVariable {
65 #[diplomat::rust_link(icu::collator::CaseLevel, Enum)]
66 pub enum ICU4XCollatorCaseLevel {
72 #[diplomat::rust_link(icu::collator::Numeric, Enum)]
73 pub enum ICU4XCollatorNumeric {
79 #[diplomat::rust_link(icu::collator::BackwardSecondLevel, Enum)]
80 pub enum ICU4XCollatorBackwardSecondLevel {
87 /// Construct a new Collator instance.
88 #[diplomat::rust_link(icu::collator::Collator::try_new, FnInStruct)]
90 provider: &ICU4XDataProvider,
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!(
99 Collator::try_new_with_any_provider,
100 Collator::try_new_with_buffer_provider,
107 /// Compare potentially ill-formed UTF-8 strings.
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()
120 /// Compare guaranteed well-formed UTF-8 strings.
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()
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()
140 AlternateHandling, BackwardSecondLevel, CaseFirst, CaseLevel, CollatorOptions, MaxVariable,
144 impl From<ffi::ICU4XCollatorStrength> for Option<Strength> {
145 fn from(strength: ffi::ICU4XCollatorStrength) -> Option<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),
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)
164 ffi::ICU4XCollatorAlternateHandling::Shifted => Some(AlternateHandling::Shifted),
169 impl From<ffi::ICU4XCollatorCaseFirst> for Option<CaseFirst> {
170 fn from(case_first: ffi::ICU4XCollatorCaseFirst) -> Option<CaseFirst> {
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),
180 impl From<ffi::ICU4XCollatorMaxVariable> for Option<MaxVariable> {
181 fn from(max_variable: ffi::ICU4XCollatorMaxVariable) -> Option<MaxVariable> {
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),
192 impl From<ffi::ICU4XCollatorCaseLevel> for Option<CaseLevel> {
193 fn from(case_level: ffi::ICU4XCollatorCaseLevel) -> Option<CaseLevel> {
195 ffi::ICU4XCollatorCaseLevel::Auto => None,
196 ffi::ICU4XCollatorCaseLevel::Off => Some(CaseLevel::Off),
197 ffi::ICU4XCollatorCaseLevel::On => Some(CaseLevel::On),
202 impl From<ffi::ICU4XCollatorNumeric> for Option<Numeric> {
203 fn from(numeric: ffi::ICU4XCollatorNumeric) -> Option<Numeric> {
205 ffi::ICU4XCollatorNumeric::Auto => None,
206 ffi::ICU4XCollatorNumeric::Off => Some(Numeric::Off),
207 ffi::ICU4XCollatorNumeric::On => Some(Numeric::On),
212 impl From<ffi::ICU4XCollatorBackwardSecondLevel> for Option<BackwardSecondLevel> {
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),
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();