| 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 |
#[diplomat::bridge] |
| 6 |
pub mod ffi { |
| 7 |
use crate::provider::ffi::ICU4XDataProvider; |
| 8 |
use alloc::boxed::Box; |
| 9 |
use icu_properties::{ |
| 10 |
names::PropertyValueNameToEnumMapper, BidiClass, EastAsianWidth, GeneralCategory, |
| 11 |
GeneralCategoryGroup, GraphemeClusterBreak, HangulSyllableType, IndicSyllabicCategory, |
| 12 |
LineBreak, Script, SentenceBreak, WordBreak, |
| 13 |
}; |
| 14 |
|
| 15 |
use crate::errors::ffi::ICU4XError; |
| 16 |
|
| 17 |
/// A type capable of looking up a property value from a string name. |
| 18 |
#[diplomat::opaque] |
| 19 |
#[diplomat::rust_link(icu::properties::names::PropertyValueNameToEnumMapper, Struct)] |
| 20 |
#[diplomat::rust_link(icu::properties::names::PropertyValueNameToEnumMapperBorrowed, Struct)] |
| 21 |
#[diplomat::rust_link( |
| 22 |
icu::properties::names::PropertyValueNameToEnumMapper::from_data, |
| 23 |
FnInStruct, |
| 24 |
hidden |
| 25 |
)] |
| 26 |
pub struct ICU4XPropertyValueNameToEnumMapper(PropertyValueNameToEnumMapper<u16>); |
| 27 |
|
| 28 |
impl ICU4XPropertyValueNameToEnumMapper { |
| 29 |
/// Get the property value matching the given name, using strict matching |
| 30 |
/// |
| 31 |
/// Returns -1 if the name is unknown for this property |
| 32 |
#[diplomat::rust_link( |
| 33 |
icu::properties::names::PropertyValueNameToEnumMapperBorrowed::get_strict, |
| 34 |
FnInStruct |
| 35 |
)] |
| 36 |
#[diplomat::rust_link( |
| 37 |
icu::properties::names::PropertyValueNameToEnumMapperBorrowed::get_strict_u16, |
| 38 |
FnInStruct, |
| 39 |
hidden |
| 40 |
)] |
| 41 |
pub fn get_strict(&self, name: &DiplomatStr) -> i16 { |
| 42 |
if let Ok(name) = core::str::from_utf8(name) { |
| 43 |
self.0.as_borrowed().get_strict(name) |
| 44 |
} else { |
| 45 |
None |
| 46 |
} |
| 47 |
.map(|u_16| u_16 as i16) |
| 48 |
.unwrap_or(-1) |
| 49 |
} |
| 50 |
|
| 51 |
/// Get the property value matching the given name, using loose matching |
| 52 |
/// |
| 53 |
/// Returns -1 if the name is unknown for this property |
| 54 |
#[diplomat::rust_link( |
| 55 |
icu::properties::names::PropertyValueNameToEnumMapperBorrowed::get_loose, |
| 56 |
FnInStruct |
| 57 |
)] |
| 58 |
#[diplomat::rust_link( |
| 59 |
icu::properties::names::PropertyValueNameToEnumMapperBorrowed::get_loose_u16, |
| 60 |
FnInStruct, |
| 61 |
hidden |
| 62 |
)] |
| 63 |
pub fn get_loose(&self, name: &DiplomatStr) -> i16 { |
| 64 |
if let Ok(name) = core::str::from_utf8(name) { |
| 65 |
self.0.as_borrowed().get_loose(name) |
| 66 |
} else { |
| 67 |
None |
| 68 |
} |
| 69 |
.map(|u_16| u_16 as i16) |
| 70 |
.unwrap_or(-1) |
| 71 |
} |
| 72 |
|
| 73 |
#[diplomat::rust_link(icu::properties::GeneralCategory::name_to_enum_mapper, FnInStruct)] |
| 74 |
#[diplomat::rust_link( |
| 75 |
icu::properties::GeneralCategory::get_name_to_enum_mapper, |
| 76 |
FnInStruct, |
| 77 |
hidden |
| 78 |
)] |
| 79 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "general_category")] |
| 80 |
pub fn load_general_category( |
| 81 |
provider: &ICU4XDataProvider, |
| 82 |
) -> Result<Box<ICU4XPropertyValueNameToEnumMapper>, ICU4XError> { |
| 83 |
Ok(Box::new(ICU4XPropertyValueNameToEnumMapper( |
| 84 |
call_constructor_unstable!( |
| 85 |
GeneralCategory::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 86 |
GeneralCategory::get_name_to_enum_mapper, |
| 87 |
provider, |
| 88 |
)? |
| 89 |
.erase(), |
| 90 |
))) |
| 91 |
} |
| 92 |
|
| 93 |
#[diplomat::rust_link(icu::properties::HangulSyllableType::name_to_enum_mapper, FnInStruct)] |
| 94 |
#[diplomat::rust_link( |
| 95 |
icu::properties::HangulSyllableType::get_name_to_enum_mapper, |
| 96 |
FnInStruct, |
| 97 |
hidden |
| 98 |
)] |
| 99 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "hangul_syllable_type")] |
| 100 |
pub fn load_hangul_syllable_type( |
| 101 |
provider: &ICU4XDataProvider, |
| 102 |
) -> Result<Box<ICU4XPropertyValueNameToEnumMapper>, ICU4XError> { |
| 103 |
Ok(Box::new(ICU4XPropertyValueNameToEnumMapper( |
| 104 |
call_constructor_unstable!( |
| 105 |
HangulSyllableType::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 106 |
HangulSyllableType::get_name_to_enum_mapper, |
| 107 |
provider, |
| 108 |
)? |
| 109 |
.erase(), |
| 110 |
))) |
| 111 |
} |
| 112 |
|
| 113 |
#[diplomat::rust_link(icu::properties::EastAsianWidth::name_to_enum_mapper, FnInStruct)] |
| 114 |
#[diplomat::rust_link( |
| 115 |
icu::properties::EastAsianWidth::get_name_to_enum_mapper, |
| 116 |
FnInStruct, |
| 117 |
hidden |
| 118 |
)] |
| 119 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "east_asian_width")] |
| 120 |
pub fn load_east_asian_width( |
| 121 |
provider: &ICU4XDataProvider, |
| 122 |
) -> Result<Box<ICU4XPropertyValueNameToEnumMapper>, ICU4XError> { |
| 123 |
Ok(Box::new(ICU4XPropertyValueNameToEnumMapper( |
| 124 |
call_constructor_unstable!( |
| 125 |
EastAsianWidth::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 126 |
EastAsianWidth::get_name_to_enum_mapper, |
| 127 |
provider, |
| 128 |
)? |
| 129 |
.erase(), |
| 130 |
))) |
| 131 |
} |
| 132 |
|
| 133 |
#[diplomat::rust_link(icu::properties::BidiClass::name_to_enum_mapper, FnInStruct)] |
| 134 |
#[diplomat::rust_link( |
| 135 |
icu::properties::BidiClass::get_name_to_enum_mapper, |
| 136 |
FnInStruct, |
| 137 |
hidden |
| 138 |
)] |
| 139 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "bidi_class")] |
| 140 |
pub fn load_bidi_class( |
| 141 |
provider: &ICU4XDataProvider, |
| 142 |
) -> Result<Box<ICU4XPropertyValueNameToEnumMapper>, ICU4XError> { |
| 143 |
Ok(Box::new(ICU4XPropertyValueNameToEnumMapper( |
| 144 |
call_constructor_unstable!( |
| 145 |
BidiClass::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 146 |
BidiClass::get_name_to_enum_mapper, |
| 147 |
provider, |
| 148 |
)? |
| 149 |
.erase(), |
| 150 |
))) |
| 151 |
} |
| 152 |
|
| 153 |
#[diplomat::rust_link( |
| 154 |
icu::properties::IndicSyllabicCategory::name_to_enum_mapper, |
| 155 |
FnInStruct |
| 156 |
)] |
| 157 |
#[diplomat::rust_link( |
| 158 |
icu::properties::IndicSyllabicCategory::get_name_to_enum_mapper, |
| 159 |
FnInStruct, |
| 160 |
hidden |
| 161 |
)] |
| 162 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "indic_syllabic_category")] |
| 163 |
pub fn load_indic_syllabic_category( |
| 164 |
provider: &ICU4XDataProvider, |
| 165 |
) -> Result<Box<ICU4XPropertyValueNameToEnumMapper>, ICU4XError> { |
| 166 |
Ok(Box::new(ICU4XPropertyValueNameToEnumMapper( |
| 167 |
call_constructor_unstable!( |
| 168 |
IndicSyllabicCategory::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 169 |
IndicSyllabicCategory::get_name_to_enum_mapper, |
| 170 |
provider, |
| 171 |
)? |
| 172 |
.erase(), |
| 173 |
))) |
| 174 |
} |
| 175 |
|
| 176 |
#[diplomat::rust_link(icu::properties::LineBreak::name_to_enum_mapper, FnInStruct)] |
| 177 |
#[diplomat::rust_link( |
| 178 |
icu::properties::LineBreak::get_name_to_enum_mapper, |
| 179 |
FnInStruct, |
| 180 |
hidden |
| 181 |
)] |
| 182 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "line_break")] |
| 183 |
pub fn load_line_break( |
| 184 |
provider: &ICU4XDataProvider, |
| 185 |
) -> Result<Box<ICU4XPropertyValueNameToEnumMapper>, ICU4XError> { |
| 186 |
Ok(Box::new(ICU4XPropertyValueNameToEnumMapper( |
| 187 |
call_constructor_unstable!( |
| 188 |
LineBreak::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 189 |
LineBreak::get_name_to_enum_mapper, |
| 190 |
provider, |
| 191 |
)? |
| 192 |
.erase(), |
| 193 |
))) |
| 194 |
} |
| 195 |
|
| 196 |
#[diplomat::rust_link( |
| 197 |
icu::properties::GraphemeClusterBreak::name_to_enum_mapper, |
| 198 |
FnInStruct |
| 199 |
)] |
| 200 |
#[diplomat::rust_link( |
| 201 |
icu::properties::GraphemeClusterBreak::get_name_to_enum_mapper, |
| 202 |
FnInStruct, |
| 203 |
hidden |
| 204 |
)] |
| 205 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "grapheme_cluster_break")] |
| 206 |
pub fn load_grapheme_cluster_break( |
| 207 |
provider: &ICU4XDataProvider, |
| 208 |
) -> Result<Box<ICU4XPropertyValueNameToEnumMapper>, ICU4XError> { |
| 209 |
Ok(Box::new(ICU4XPropertyValueNameToEnumMapper( |
| 210 |
call_constructor_unstable!( |
| 211 |
GraphemeClusterBreak::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 212 |
GraphemeClusterBreak::get_name_to_enum_mapper, |
| 213 |
provider, |
| 214 |
)? |
| 215 |
.erase(), |
| 216 |
))) |
| 217 |
} |
| 218 |
|
| 219 |
#[diplomat::rust_link(icu::properties::WordBreak::name_to_enum_mapper, FnInStruct)] |
| 220 |
#[diplomat::rust_link( |
| 221 |
icu::properties::WordBreak::get_name_to_enum_mapper, |
| 222 |
FnInStruct, |
| 223 |
hidden |
| 224 |
)] |
| 225 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "word_break")] |
| 226 |
pub fn load_word_break( |
| 227 |
provider: &ICU4XDataProvider, |
| 228 |
) -> Result<Box<ICU4XPropertyValueNameToEnumMapper>, ICU4XError> { |
| 229 |
Ok(Box::new(ICU4XPropertyValueNameToEnumMapper( |
| 230 |
call_constructor_unstable!( |
| 231 |
WordBreak::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 232 |
WordBreak::get_name_to_enum_mapper, |
| 233 |
provider, |
| 234 |
)? |
| 235 |
.erase(), |
| 236 |
))) |
| 237 |
} |
| 238 |
|
| 239 |
#[diplomat::rust_link(icu::properties::SentenceBreak::name_to_enum_mapper, FnInStruct)] |
| 240 |
#[diplomat::rust_link( |
| 241 |
icu::properties::SentenceBreak::get_name_to_enum_mapper, |
| 242 |
FnInStruct, |
| 243 |
hidden |
| 244 |
)] |
| 245 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "sentence_break")] |
| 246 |
pub fn load_sentence_break( |
| 247 |
provider: &ICU4XDataProvider, |
| 248 |
) -> Result<Box<ICU4XPropertyValueNameToEnumMapper>, ICU4XError> { |
| 249 |
Ok(Box::new(ICU4XPropertyValueNameToEnumMapper( |
| 250 |
call_constructor_unstable!( |
| 251 |
SentenceBreak::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 252 |
SentenceBreak::get_name_to_enum_mapper, |
| 253 |
provider, |
| 254 |
)? |
| 255 |
.erase(), |
| 256 |
))) |
| 257 |
} |
| 258 |
|
| 259 |
#[diplomat::rust_link(icu::properties::Script::name_to_enum_mapper, FnInStruct)] |
| 260 |
#[diplomat::rust_link(icu::properties::Script::get_name_to_enum_mapper, FnInStruct, hidden)] |
| 261 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "script")] |
| 262 |
pub fn load_script( |
| 263 |
provider: &ICU4XDataProvider, |
| 264 |
) -> Result<Box<ICU4XPropertyValueNameToEnumMapper>, ICU4XError> { |
| 265 |
Ok(Box::new(ICU4XPropertyValueNameToEnumMapper( |
| 266 |
call_constructor_unstable!( |
| 267 |
Script::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 268 |
Script::get_name_to_enum_mapper, |
| 269 |
provider, |
| 270 |
)? |
| 271 |
.erase(), |
| 272 |
))) |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/// A type capable of looking up General Category mask values from a string name. |
| 277 |
#[diplomat::opaque] |
| 278 |
#[diplomat::rust_link(icu::properties::GeneralCategoryGroup::name_to_enum_mapper, FnInStruct)] |
| 279 |
#[diplomat::rust_link( |
| 280 |
icu::properties::GeneralCategoryGroup::get_name_to_enum_mapper, |
| 281 |
FnInStruct, |
| 282 |
hidden |
| 283 |
)] |
| 284 |
#[diplomat::rust_link(icu::properties::names::PropertyValueNameToEnumMapper, Struct)] |
| 285 |
pub struct ICU4XGeneralCategoryNameToMaskMapper( |
| 286 |
PropertyValueNameToEnumMapper<GeneralCategoryGroup>, |
| 287 |
); |
| 288 |
|
| 289 |
impl ICU4XGeneralCategoryNameToMaskMapper { |
| 290 |
/// Get the mask value matching the given name, using strict matching |
| 291 |
/// |
| 292 |
/// Returns 0 if the name is unknown for this property |
| 293 |
// #[diplomat::rust_link(icu::properties::maps::PropertyValueNameToEnumMapperBorrowed::get_strict, FnInStruct)] |
| 294 |
// #[diplomat::rust_link(icu::properties::maps::PropertyValueNameToEnumMapperBorrowed::get_strict_u16, FnInStruct, hidden)] |
| 295 |
pub fn get_strict(&self, name: &DiplomatStr) -> u32 { |
| 296 |
if let Ok(name) = core::str::from_utf8(name) { |
| 297 |
self.0.as_borrowed().get_strict(name) |
| 298 |
} else { |
| 299 |
None |
| 300 |
} |
| 301 |
.map(Into::into) |
| 302 |
.unwrap_or_default() |
| 303 |
} |
| 304 |
|
| 305 |
/// Get the mask value matching the given name, using loose matching |
| 306 |
/// |
| 307 |
/// Returns 0 if the name is unknown for this property |
| 308 |
// #[diplomat::rust_link(icu::properties::maps::PropertyValueNameToEnumMapperBorrowed::get_loose, FnInStruct)] |
| 309 |
// #[diplomat::rust_link(icu::properties::maps::PropertyValueNameToEnumMapperBorrowed::get_loose_u16, FnInStruct, hidden)] |
| 310 |
pub fn get_loose(&self, name: &DiplomatStr) -> u32 { |
| 311 |
if let Ok(name) = core::str::from_utf8(name) { |
| 312 |
self.0.as_borrowed().get_loose(name) |
| 313 |
} else { |
| 314 |
None |
| 315 |
} |
| 316 |
.map(Into::into) |
| 317 |
.unwrap_or_default() |
| 318 |
} |
| 319 |
|
| 320 |
#[diplomat::rust_link( |
| 321 |
icu::properties::GeneralCategoryGroup::name_to_enum_mapper, |
| 322 |
FnInStruct |
| 323 |
)] |
| 324 |
#[diplomat::rust_link( |
| 325 |
icu::properties::GeneralCategoryGroup::get_name_to_enum_mapper, |
| 326 |
FnInStruct, |
| 327 |
hidden |
| 328 |
)] |
| 329 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
| 330 |
pub fn load( |
| 331 |
provider: &ICU4XDataProvider, |
| 332 |
) -> Result<Box<ICU4XGeneralCategoryNameToMaskMapper>, ICU4XError> { |
| 333 |
Ok(Box::new(ICU4XGeneralCategoryNameToMaskMapper( |
| 334 |
call_constructor_unstable!( |
| 335 |
GeneralCategoryGroup::name_to_enum_mapper [r => Ok(r.static_to_owned())], |
| 336 |
GeneralCategoryGroup::get_name_to_enum_mapper, |
| 337 |
provider, |
| 338 |
)?, |
| 339 |
))) |
| 340 |
} |
| 341 |
} |
| 342 |
} |