| 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 |
#[diplomat::bridge] |
| 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 |
#[diplomat::enum_convert(LeadingAdjustment, needs_wildcard)] |
| 18 |
#[diplomat::rust_link(icu::casemap::titlecase::LeadingAdjustment, Enum)] |
| 19 |
pub enum ICU4XLeadingAdjustment { |
| 20 |
Auto, |
| 21 |
None, |
| 22 |
ToCased, |
| 23 |
} |
| 24 |
|
| 25 |
#[diplomat::enum_convert(TrailingCase, needs_wildcard)] |
| 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 |
pub fn default_options() -> ICU4XTitlecaseOptionsV1 { |
| 43 |
// named default_options to avoid keyword clashes |
| 44 |
Self { |
| 45 |
leading_adjustment: ICU4XLeadingAdjustment::Auto, |
| 46 |
trailing_case: ICU4XTrailingCase::Lower, |
| 47 |
} |
| 48 |
} |
| 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 |
pub fn create(provider: &ICU4XDataProvider) -> Result<Box<ICU4XCaseMapper>, ICU4XError> { |
| 60 |
Ok(Box::new(ICU4XCaseMapper(call_constructor!( |
| 61 |
CaseMapper::new [r => Ok(r)], |
| 62 |
CaseMapper::try_new_with_any_provider, |
| 63 |
CaseMapper::try_new_with_buffer_provider, |
| 64 |
provider, |
| 65 |
)?))) |
| 66 |
} |
| 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 |
pub fn lowercase( |
| 72 |
&self, |
| 73 |
s: &DiplomatStr, |
| 74 |
locale: &ICU4XLocale, |
| 75 |
write: &mut DiplomatWriteable, |
| 76 |
) -> Result<(), ICU4XError> { |
| 77 |
self.0 |
| 78 |
.lowercase(core::str::from_utf8(s)?, &locale.0.id) |
| 79 |
.write_to(write)?; |
| 80 |
|
| 81 |
Ok(()) |
| 82 |
} |
| 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 |
pub fn uppercase( |
| 88 |
&self, |
| 89 |
s: &DiplomatStr, |
| 90 |
locale: &ICU4XLocale, |
| 91 |
write: &mut DiplomatWriteable, |
| 92 |
) -> Result<(), ICU4XError> { |
| 93 |
self.0 |
| 94 |
.uppercase(core::str::from_utf8(s)?, &locale.0.id) |
| 95 |
.write_to(write)?; |
| 96 |
|
| 97 |
Ok(()) |
| 98 |
} |
| 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 |
pub fn titlecase_segment_with_only_case_data_v1( |
| 116 |
&self, |
| 117 |
s: &DiplomatStr, |
| 118 |
locale: &ICU4XLocale, |
| 119 |
options: ICU4XTitlecaseOptionsV1, |
| 120 |
write: &mut DiplomatWriteable, |
| 121 |
) -> Result<(), ICU4XError> { |
| 122 |
self.0 |
| 123 |
.titlecase_segment_with_only_case_data( |
| 124 |
core::str::from_utf8(s)?, |
| 125 |
&locale.0.id, |
| 126 |
options.into(), |
| 127 |
) |
| 128 |
.write_to(write)?; |
| 129 |
|
| 130 |
Ok(()) |
| 131 |
} |
| 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 |
pub fn fold( |
| 137 |
&self, |
| 138 |
s: &DiplomatStr, |
| 139 |
write: &mut DiplomatWriteable, |
| 140 |
) -> Result<(), ICU4XError> { |
| 141 |
self.0.fold(core::str::from_utf8(s)?).write_to(write)?; |
| 142 |
|
| 143 |
Ok(()) |
| 144 |
} |
| 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 |
pub fn fold_turkic( |
| 150 |
&self, |
| 151 |
s: &DiplomatStr, |
| 152 |
write: &mut DiplomatWriteable, |
| 153 |
) -> Result<(), ICU4XError> { |
| 154 |
self.0 |
| 155 |
.fold_turkic(core::str::from_utf8(s)?) |
| 156 |
.write_to(write)?; |
| 157 |
|
| 158 |
Ok(()) |
| 159 |
} |
| 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 |
pub fn add_case_closure_to( |
| 178 |
&self, |
| 179 |
c: DiplomatChar, |
| 180 |
builder: &mut crate::collections_sets::ffi::ICU4XCodePointSetBuilder, |
| 181 |
) { |
| 182 |
if let Some(ch) = char::from_u32(c) { |
| 183 |
self.0.add_case_closure_to(ch, &mut builder.0) |
| 184 |
} |
| 185 |
} |
| 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 |
pub fn simple_lowercase(&self, ch: DiplomatChar) -> DiplomatChar { |
| 194 |
char::from_u32(ch) |
| 195 |
.map(|ch| self.0.simple_lowercase(ch) as DiplomatChar) |
| 196 |
.unwrap_or(ch) |
| 197 |
} |
| 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 |
pub fn simple_uppercase(&self, ch: DiplomatChar) -> DiplomatChar { |
| 206 |
char::from_u32(ch) |
| 207 |
.map(|ch| self.0.simple_uppercase(ch) as DiplomatChar) |
| 208 |
.unwrap_or(ch) |
| 209 |
} |
| 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 |
pub fn simple_titlecase(&self, ch: DiplomatChar) -> DiplomatChar { |
| 218 |
char::from_u32(ch) |
| 219 |
.map(|ch| self.0.simple_titlecase(ch) as DiplomatChar) |
| 220 |
.unwrap_or(ch) |
| 221 |
} |
| 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 |
pub fn simple_fold(&self, ch: DiplomatChar) -> DiplomatChar { |
| 229 |
char::from_u32(ch) |
| 230 |
.map(|ch| self.0.simple_fold(ch) as DiplomatChar) |
| 231 |
.unwrap_or(ch) |
| 232 |
} |
| 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 |
pub fn simple_fold_turkic(&self, ch: DiplomatChar) -> DiplomatChar { |
| 239 |
char::from_u32(ch) |
| 240 |
.map(|ch| self.0.simple_fold_turkic(ch) as DiplomatChar) |
| 241 |
.unwrap_or(ch) |
| 242 |
} |
| 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 |
pub fn create(provider: &ICU4XDataProvider) -> Result<Box<ICU4XCaseMapCloser>, ICU4XError> { |
| 255 |
Ok(Box::new(ICU4XCaseMapCloser(call_constructor!( |
| 256 |
CaseMapCloser::new [r => Ok(r)], |
| 257 |
CaseMapCloser::try_new_with_any_provider, |
| 258 |
CaseMapCloser::try_new_with_buffer_provider, |
| 259 |
provider, |
| 260 |
)?))) |
| 261 |
} |
| 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 |
pub fn add_case_closure_to( |
| 268 |
&self, |
| 269 |
c: DiplomatChar, |
| 270 |
builder: &mut crate::collections_sets::ffi::ICU4XCodePointSetBuilder, |
| 271 |
) { |
| 272 |
if let Some(ch) = char::from_u32(c) { |
| 273 |
self.0.add_case_closure_to(ch, &mut builder.0) |
| 274 |
} |
| 275 |
} |
| 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 |
pub fn add_string_case_closure_to( |
| 284 |
&self, |
| 285 |
s: &DiplomatStr, |
| 286 |
builder: &mut crate::collections_sets::ffi::ICU4XCodePointSetBuilder, |
| 287 |
) -> bool { |
| 288 |
let s = core::str::from_utf8(s).unwrap_or(""); |
| 289 |
self.0.add_string_case_closure_to(s, &mut builder.0) |
| 290 |
} |
| 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 |
pub fn create( |
| 303 |
provider: &ICU4XDataProvider, |
| 304 |
) -> Result<Box<ICU4XTitlecaseMapper>, ICU4XError> { |
| 305 |
Ok(Box::new(ICU4XTitlecaseMapper(call_constructor!( |
| 306 |
TitlecaseMapper::new [r => Ok(r)], |
| 307 |
TitlecaseMapper::try_new_with_any_provider, |
| 308 |
TitlecaseMapper::try_new_with_buffer_provider, |
| 309 |
provider, |
| 310 |
)?))) |
| 311 |
} |
| 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 |
pub fn titlecase_segment_v1( |
| 324 |
&self, |
| 325 |
s: &DiplomatStr, |
| 326 |
locale: &ICU4XLocale, |
| 327 |
options: ICU4XTitlecaseOptionsV1, |
| 328 |
write: &mut DiplomatWriteable, |
| 329 |
) -> Result<(), ICU4XError> { |
| 330 |
self.0 |
| 331 |
.titlecase_segment(core::str::from_utf8(s)?, &locale.0.id, options.into()) |
| 332 |
.write_to(write)?; |
| 333 |
|
| 334 |
Ok(()) |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
impl From<ffi::ICU4XTitlecaseOptionsV1> for TitlecaseOptions { |
| 340 |
fn from(other: ffi::ICU4XTitlecaseOptionsV1) -> Self { |
| 341 |
let mut ret = Self::default(); |
| 342 |
|
| 343 |
ret.leading_adjustment = other.leading_adjustment.into(); |
| 344 |
ret.trailing_case = other.trailing_case.into(); |
| 345 |
ret |
| 346 |
} |
| 347 |
} |