| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 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 |
|
| 146 |
|
| 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 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 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 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 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 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 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 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 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 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 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 |
|
| 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 |
|
| 264 |
|
| 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 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 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 |
|
| 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 |
|
| 314 |
|
| 315 |
|
| 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 |
} |