/rust/registry/src/index.crates.io-6f17d22bba15001f/icu_capi-1.5.1/src/casemap.rs
Line | Count | Source (jump to first uncovered line) |
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 | | use icu_casemap::titlecase::TitlecaseOptions; |
6 | | |
7 | 0 | #[diplomat::bridge] Unexecuted instantiation: ICU4XCaseMapCloser_create Unexecuted instantiation: ICU4XCaseMapCloser_add_case_closure_to Unexecuted instantiation: ICU4XCaseMapCloser_add_string_case_closure_to Unexecuted instantiation: ICU4XCaseMapCloser_destroy Unexecuted instantiation: ICU4XCaseMapper_create Unexecuted instantiation: ICU4XCaseMapper_lowercase Unexecuted instantiation: ICU4XCaseMapper_uppercase Unexecuted instantiation: ICU4XCaseMapper_titlecase_segment_with_only_case_data_v1 Unexecuted instantiation: ICU4XCaseMapper_fold Unexecuted instantiation: ICU4XCaseMapper_fold_turkic Unexecuted instantiation: ICU4XCaseMapper_add_case_closure_to Unexecuted instantiation: ICU4XCaseMapper_simple_lowercase Unexecuted instantiation: ICU4XCaseMapper_simple_uppercase Unexecuted instantiation: ICU4XCaseMapper_simple_titlecase Unexecuted instantiation: ICU4XCaseMapper_simple_fold Unexecuted instantiation: ICU4XCaseMapper_simple_fold_turkic Unexecuted instantiation: ICU4XCaseMapper_destroy Unexecuted instantiation: ICU4XLeadingAdjustment_destroy Unexecuted instantiation: ICU4XTitlecaseMapper_create Unexecuted instantiation: ICU4XTitlecaseMapper_titlecase_segment_v1 Unexecuted instantiation: ICU4XTitlecaseMapper_destroy Unexecuted instantiation: ICU4XTitlecaseOptionsV1_default_options Unexecuted instantiation: ICU4XTitlecaseOptionsV1_destroy Unexecuted instantiation: ICU4XTrailingCase_destroy |
8 | | pub mod ffi { |
9 | | use crate::{ |
10 | | errors::ffi::ICU4XError, locale::ffi::ICU4XLocale, provider::ffi::ICU4XDataProvider, |
11 | | }; |
12 | | use alloc::boxed::Box; |
13 | | use icu_casemap::titlecase::{LeadingAdjustment, TrailingCase}; |
14 | | use icu_casemap::{CaseMapCloser, CaseMapper, TitlecaseMapper}; |
15 | | use writeable::Writeable; |
16 | | |
17 | 0 | #[diplomat::enum_convert(LeadingAdjustment, needs_wildcard)] Unexecuted instantiation: <icu_casemap::titlecase::LeadingAdjustment as core::convert::From<icu_capi::casemap::ffi::ICU4XLeadingAdjustment>>::from Unexecuted instantiation: <icu_capi::casemap::ffi::ICU4XLeadingAdjustment as core::convert::From<icu_casemap::titlecase::LeadingAdjustment>>::from |
18 | | #[diplomat::rust_link(icu::casemap::titlecase::LeadingAdjustment, Enum)] |
19 | | pub enum ICU4XLeadingAdjustment { |
20 | | Auto, |
21 | | None, |
22 | | ToCased, |
23 | | } |
24 | | |
25 | 0 | #[diplomat::enum_convert(TrailingCase, needs_wildcard)] Unexecuted instantiation: <icu_casemap::titlecase::TrailingCase as core::convert::From<icu_capi::casemap::ffi::ICU4XTrailingCase>>::from Unexecuted instantiation: <icu_capi::casemap::ffi::ICU4XTrailingCase as core::convert::From<icu_casemap::titlecase::TrailingCase>>::from |
26 | | #[diplomat::rust_link(icu::casemap::titlecase::TrailingCase, Enum)] |
27 | | pub enum ICU4XTrailingCase { |
28 | | Lower, |
29 | | Unchanged, |
30 | | } |
31 | | |
32 | | #[diplomat::rust_link(icu::casemap::titlecase::TitlecaseOptions, Struct)] |
33 | | #[diplomat::attr(dart, rename = "TitlecaseOptions")] |
34 | | pub struct ICU4XTitlecaseOptionsV1 { |
35 | | pub leading_adjustment: ICU4XLeadingAdjustment, |
36 | | pub trailing_case: ICU4XTrailingCase, |
37 | | } |
38 | | |
39 | | impl ICU4XTitlecaseOptionsV1 { |
40 | | #[diplomat::rust_link(icu::casemap::titlecase::TitlecaseOptions::default, FnInStruct)] |
41 | | #[diplomat::attr(supports = constructors, constructor)] |
42 | 0 | pub fn default_options() -> ICU4XTitlecaseOptionsV1 { |
43 | 0 | // named default_options to avoid keyword clashes |
44 | 0 | Self { |
45 | 0 | leading_adjustment: ICU4XLeadingAdjustment::Auto, |
46 | 0 | trailing_case: ICU4XTrailingCase::Lower, |
47 | 0 | } |
48 | 0 | } |
49 | | } |
50 | | |
51 | | #[diplomat::opaque] |
52 | | #[diplomat::rust_link(icu::casemap::CaseMapper, Struct)] |
53 | | pub struct ICU4XCaseMapper(pub CaseMapper); |
54 | | |
55 | | impl ICU4XCaseMapper { |
56 | | /// Construct a new ICU4XCaseMapper instance |
57 | | #[diplomat::rust_link(icu::casemap::CaseMapper::new, FnInStruct)] |
58 | | #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
59 | 0 | pub fn create(provider: &ICU4XDataProvider) -> Result<Box<ICU4XCaseMapper>, ICU4XError> { |
60 | 0 | Ok(Box::new(ICU4XCaseMapper(call_constructor!( |
61 | 0 | CaseMapper::new [r => Ok(r)], |
62 | | CaseMapper::try_new_with_any_provider, |
63 | | CaseMapper::try_new_with_buffer_provider, |
64 | | provider, |
65 | 0 | )?))) |
66 | 0 | } |
67 | | |
68 | | /// Returns the full lowercase mapping of the given string |
69 | | #[diplomat::rust_link(icu::casemap::CaseMapper::lowercase, FnInStruct)] |
70 | | #[diplomat::rust_link(icu::casemap::CaseMapper::lowercase_to_string, FnInStruct, hidden)] |
71 | 0 | pub fn lowercase( |
72 | 0 | &self, |
73 | 0 | s: &DiplomatStr, |
74 | 0 | locale: &ICU4XLocale, |
75 | 0 | write: &mut DiplomatWriteable, |
76 | 0 | ) -> Result<(), ICU4XError> { |
77 | 0 | self.0 |
78 | 0 | .lowercase(core::str::from_utf8(s)?, &locale.0.id) |
79 | 0 | .write_to(write)?; |
80 | | |
81 | 0 | Ok(()) |
82 | 0 | } |
83 | | |
84 | | /// Returns the full uppercase mapping of the given string |
85 | | #[diplomat::rust_link(icu::casemap::CaseMapper::uppercase, FnInStruct)] |
86 | | #[diplomat::rust_link(icu::casemap::CaseMapper::uppercase_to_string, FnInStruct, hidden)] |
87 | 0 | pub fn uppercase( |
88 | 0 | &self, |
89 | 0 | s: &DiplomatStr, |
90 | 0 | locale: &ICU4XLocale, |
91 | 0 | write: &mut DiplomatWriteable, |
92 | 0 | ) -> Result<(), ICU4XError> { |
93 | 0 | self.0 |
94 | 0 | .uppercase(core::str::from_utf8(s)?, &locale.0.id) |
95 | 0 | .write_to(write)?; |
96 | | |
97 | 0 | Ok(()) |
98 | 0 | } |
99 | | |
100 | | /// Returns the full titlecase mapping of the given string, performing head adjustment without |
101 | | /// loading additional data. |
102 | | /// (if head adjustment is enabled in the options) |
103 | | /// |
104 | | /// The `v1` refers to the version of the options struct, which may change as we add more options |
105 | | #[diplomat::rust_link( |
106 | | icu::casemap::CaseMapper::titlecase_segment_with_only_case_data, |
107 | | FnInStruct |
108 | | )] |
109 | | #[diplomat::rust_link( |
110 | | icu::casemap::CaseMapper::titlecase_segment_with_only_case_data_to_string, |
111 | | FnInStruct, |
112 | | hidden |
113 | | )] |
114 | | #[diplomat::attr(dart, rename = "titlecaseSegmentWithOnlyCaseData")] |
115 | 0 | pub fn titlecase_segment_with_only_case_data_v1( |
116 | 0 | &self, |
117 | 0 | s: &DiplomatStr, |
118 | 0 | locale: &ICU4XLocale, |
119 | 0 | options: ICU4XTitlecaseOptionsV1, |
120 | 0 | write: &mut DiplomatWriteable, |
121 | 0 | ) -> Result<(), ICU4XError> { |
122 | 0 | self.0 |
123 | 0 | .titlecase_segment_with_only_case_data( |
124 | 0 | core::str::from_utf8(s)?, |
125 | 0 | &locale.0.id, |
126 | 0 | options.into(), |
127 | 0 | ) |
128 | 0 | .write_to(write)?; |
129 | | |
130 | 0 | Ok(()) |
131 | 0 | } |
132 | | |
133 | | /// Case-folds the characters in the given string |
134 | | #[diplomat::rust_link(icu::casemap::CaseMapper::fold, FnInStruct)] |
135 | | #[diplomat::rust_link(icu::casemap::CaseMapper::fold_string, FnInStruct, hidden)] |
136 | 0 | pub fn fold( |
137 | 0 | &self, |
138 | 0 | s: &DiplomatStr, |
139 | 0 | write: &mut DiplomatWriteable, |
140 | 0 | ) -> Result<(), ICU4XError> { |
141 | 0 | self.0.fold(core::str::from_utf8(s)?).write_to(write)?; |
142 | | |
143 | 0 | Ok(()) |
144 | 0 | } |
145 | | /// Case-folds the characters in the given string |
146 | | /// using Turkic (T) mappings for dotted/dotless I. |
147 | | #[diplomat::rust_link(icu::casemap::CaseMapper::fold_turkic, FnInStruct)] |
148 | | #[diplomat::rust_link(icu::casemap::CaseMapper::fold_turkic_string, FnInStruct, hidden)] |
149 | 0 | pub fn fold_turkic( |
150 | 0 | &self, |
151 | 0 | s: &DiplomatStr, |
152 | 0 | write: &mut DiplomatWriteable, |
153 | 0 | ) -> Result<(), ICU4XError> { |
154 | 0 | self.0 |
155 | 0 | .fold_turkic(core::str::from_utf8(s)?) |
156 | 0 | .write_to(write)?; |
157 | | |
158 | 0 | Ok(()) |
159 | 0 | } |
160 | | |
161 | | /// Adds all simple case mappings and the full case folding for `c` to `builder`. |
162 | | /// Also adds special case closure mappings. |
163 | | /// |
164 | | /// In other words, this adds all characters that this casemaps to, as |
165 | | /// well as all characters that may casemap to this one. |
166 | | /// |
167 | | /// Note that since ICU4XCodePointSetBuilder does not contain strings, this will |
168 | | /// ignore string mappings. |
169 | | /// |
170 | | /// Identical to the similarly named method on `ICU4XCaseMapCloser`, use that if you |
171 | | /// plan on using string case closure mappings too. |
172 | | #[cfg(feature = "icu_properties")] |
173 | | #[diplomat::rust_link(icu::casemap::CaseMapper::add_case_closure_to, FnInStruct)] |
174 | | #[diplomat::rust_link(icu::casemap::ClosureSink, Trait, hidden)] |
175 | | #[diplomat::rust_link(icu::casemap::ClosureSink::add_char, FnInTrait, hidden)] |
176 | | #[diplomat::rust_link(icu::casemap::ClosureSink::add_string, FnInTrait, hidden)] |
177 | 0 | pub fn add_case_closure_to( |
178 | 0 | &self, |
179 | 0 | c: DiplomatChar, |
180 | 0 | builder: &mut crate::collections_sets::ffi::ICU4XCodePointSetBuilder, |
181 | 0 | ) { |
182 | 0 | if let Some(ch) = char::from_u32(c) { |
183 | 0 | self.0.add_case_closure_to(ch, &mut builder.0) |
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | /// Returns the simple lowercase mapping of the given character. |
188 | | /// |
189 | | /// This function only implements simple and common mappings. |
190 | | /// Full mappings, which can map one char to a string, are not included. |
191 | | /// For full mappings, use `ICU4XCaseMapper::lowercase`. |
192 | | #[diplomat::rust_link(icu::casemap::CaseMapper::simple_lowercase, FnInStruct)] |
193 | 0 | pub fn simple_lowercase(&self, ch: DiplomatChar) -> DiplomatChar { |
194 | 0 | char::from_u32(ch) |
195 | 0 | .map(|ch| self.0.simple_lowercase(ch) as DiplomatChar) |
196 | 0 | .unwrap_or(ch) |
197 | 0 | } |
198 | | |
199 | | /// Returns the simple uppercase mapping of the given character. |
200 | | /// |
201 | | /// This function only implements simple and common mappings. |
202 | | /// Full mappings, which can map one char to a string, are not included. |
203 | | /// For full mappings, use `ICU4XCaseMapper::uppercase`. |
204 | | #[diplomat::rust_link(icu::casemap::CaseMapper::simple_uppercase, FnInStruct)] |
205 | 0 | pub fn simple_uppercase(&self, ch: DiplomatChar) -> DiplomatChar { |
206 | 0 | char::from_u32(ch) |
207 | 0 | .map(|ch| self.0.simple_uppercase(ch) as DiplomatChar) |
208 | 0 | .unwrap_or(ch) |
209 | 0 | } |
210 | | |
211 | | /// Returns the simple titlecase mapping of the given character. |
212 | | /// |
213 | | /// This function only implements simple and common mappings. |
214 | | /// Full mappings, which can map one char to a string, are not included. |
215 | | /// For full mappings, use `ICU4XCaseMapper::titlecase_segment`. |
216 | | #[diplomat::rust_link(icu::casemap::CaseMapper::simple_titlecase, FnInStruct)] |
217 | 0 | pub fn simple_titlecase(&self, ch: DiplomatChar) -> DiplomatChar { |
218 | 0 | char::from_u32(ch) |
219 | 0 | .map(|ch| self.0.simple_titlecase(ch) as DiplomatChar) |
220 | 0 | .unwrap_or(ch) |
221 | 0 | } |
222 | | |
223 | | /// Returns the simple casefolding of the given character. |
224 | | /// |
225 | | /// This function only implements simple folding. |
226 | | /// For full folding, use `ICU4XCaseMapper::fold`. |
227 | | #[diplomat::rust_link(icu::casemap::CaseMapper::simple_fold, FnInStruct)] |
228 | 0 | pub fn simple_fold(&self, ch: DiplomatChar) -> DiplomatChar { |
229 | 0 | char::from_u32(ch) |
230 | 0 | .map(|ch| self.0.simple_fold(ch) as DiplomatChar) |
231 | 0 | .unwrap_or(ch) |
232 | 0 | } |
233 | | /// Returns the simple casefolding of the given character in the Turkic locale |
234 | | /// |
235 | | /// This function only implements simple folding. |
236 | | /// For full folding, use `ICU4XCaseMapper::fold_turkic`. |
237 | | #[diplomat::rust_link(icu::casemap::CaseMapper::simple_fold_turkic, FnInStruct)] |
238 | 0 | pub fn simple_fold_turkic(&self, ch: DiplomatChar) -> DiplomatChar { |
239 | 0 | char::from_u32(ch) |
240 | 0 | .map(|ch| self.0.simple_fold_turkic(ch) as DiplomatChar) |
241 | 0 | .unwrap_or(ch) |
242 | 0 | } |
243 | | } |
244 | | |
245 | | #[diplomat::opaque] |
246 | | #[diplomat::rust_link(icu::casemap::CaseMapCloser, Struct)] |
247 | | pub struct ICU4XCaseMapCloser(pub CaseMapCloser<CaseMapper>); |
248 | | |
249 | | impl ICU4XCaseMapCloser { |
250 | | /// Construct a new ICU4XCaseMapper instance |
251 | | #[diplomat::rust_link(icu::casemap::CaseMapCloser::new, FnInStruct)] |
252 | | #[diplomat::rust_link(icu::casemap::CaseMapCloser::new_with_mapper, FnInStruct, hidden)] |
253 | | #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
254 | 0 | pub fn create(provider: &ICU4XDataProvider) -> Result<Box<ICU4XCaseMapCloser>, ICU4XError> { |
255 | 0 | Ok(Box::new(ICU4XCaseMapCloser(call_constructor!( |
256 | 0 | CaseMapCloser::new [r => Ok(r)], |
257 | | CaseMapCloser::try_new_with_any_provider, |
258 | | CaseMapCloser::try_new_with_buffer_provider, |
259 | | provider, |
260 | 0 | )?))) |
261 | 0 | } |
262 | | |
263 | | /// Adds all simple case mappings and the full case folding for `c` to `builder`. |
264 | | /// Also adds special case closure mappings. |
265 | | #[cfg(feature = "icu_properties")] |
266 | | #[diplomat::rust_link(icu::casemap::CaseMapCloser::add_case_closure_to, FnInStruct)] |
267 | 0 | pub fn add_case_closure_to( |
268 | 0 | &self, |
269 | 0 | c: DiplomatChar, |
270 | 0 | builder: &mut crate::collections_sets::ffi::ICU4XCodePointSetBuilder, |
271 | 0 | ) { |
272 | 0 | if let Some(ch) = char::from_u32(c) { |
273 | 0 | self.0.add_case_closure_to(ch, &mut builder.0) |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | | /// Finds all characters and strings which may casemap to `s` as their full case folding string |
278 | | /// and adds them to the set. |
279 | | /// |
280 | | /// Returns true if the string was found |
281 | | #[cfg(feature = "icu_properties")] |
282 | | #[diplomat::rust_link(icu::casemap::CaseMapCloser::add_string_case_closure_to, FnInStruct)] |
283 | 0 | pub fn add_string_case_closure_to( |
284 | 0 | &self, |
285 | 0 | s: &DiplomatStr, |
286 | 0 | builder: &mut crate::collections_sets::ffi::ICU4XCodePointSetBuilder, |
287 | 0 | ) -> bool { |
288 | 0 | let s = core::str::from_utf8(s).unwrap_or(""); |
289 | 0 | self.0.add_string_case_closure_to(s, &mut builder.0) |
290 | 0 | } |
291 | | } |
292 | | |
293 | | #[diplomat::opaque] |
294 | | #[diplomat::rust_link(icu::casemap::TitlecaseMapper, Struct)] |
295 | | pub struct ICU4XTitlecaseMapper(pub TitlecaseMapper<CaseMapper>); |
296 | | |
297 | | impl ICU4XTitlecaseMapper { |
298 | | /// Construct a new `ICU4XTitlecaseMapper` instance |
299 | | #[diplomat::rust_link(icu::casemap::TitlecaseMapper::new, FnInStruct)] |
300 | | #[diplomat::rust_link(icu::casemap::TitlecaseMapper::new_with_mapper, FnInStruct, hidden)] |
301 | | #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
302 | 0 | pub fn create( |
303 | 0 | provider: &ICU4XDataProvider, |
304 | 0 | ) -> Result<Box<ICU4XTitlecaseMapper>, ICU4XError> { |
305 | 0 | Ok(Box::new(ICU4XTitlecaseMapper(call_constructor!( |
306 | 0 | TitlecaseMapper::new [r => Ok(r)], |
307 | | TitlecaseMapper::try_new_with_any_provider, |
308 | | TitlecaseMapper::try_new_with_buffer_provider, |
309 | | provider, |
310 | 0 | )?))) |
311 | 0 | } |
312 | | |
313 | | /// Returns the full titlecase mapping of the given string |
314 | | /// |
315 | | /// The `v1` refers to the version of the options struct, which may change as we add more options |
316 | | #[diplomat::rust_link(icu::casemap::TitlecaseMapper::titlecase_segment, FnInStruct)] |
317 | | #[diplomat::rust_link( |
318 | | icu::casemap::TitlecaseMapper::titlecase_segment_to_string, |
319 | | FnInStruct, |
320 | | hidden |
321 | | )] |
322 | | #[diplomat::attr(dart, rename = "titlecaseSegment")] |
323 | 0 | pub fn titlecase_segment_v1( |
324 | 0 | &self, |
325 | 0 | s: &DiplomatStr, |
326 | 0 | locale: &ICU4XLocale, |
327 | 0 | options: ICU4XTitlecaseOptionsV1, |
328 | 0 | write: &mut DiplomatWriteable, |
329 | 0 | ) -> Result<(), ICU4XError> { |
330 | 0 | self.0 |
331 | 0 | .titlecase_segment(core::str::from_utf8(s)?, &locale.0.id, options.into()) |
332 | 0 | .write_to(write)?; |
333 | | |
334 | 0 | Ok(()) |
335 | 0 | } |
336 | | } |
337 | | } |
338 | | |
339 | | impl From<ffi::ICU4XTitlecaseOptionsV1> for TitlecaseOptions { |
340 | 0 | fn from(other: ffi::ICU4XTitlecaseOptionsV1) -> Self { |
341 | 0 | let mut ret = Self::default(); |
342 | 0 |
|
343 | 0 | ret.leading_adjustment = other.leading_adjustment.into(); |
344 | 0 | ret.trailing_case = other.trailing_case.into(); |
345 | 0 | ret |
346 | 0 | } |
347 | | } |