properties_names.rs (21938B)
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 #[diplomat::abi_rename = "icu4x_{0}_mv1"] 7 #[diplomat::attr(auto, namespace = "icu4x")] 8 pub mod ffi { 9 use crate::unstable::properties_enums::ffi::GeneralCategoryGroup; 10 use alloc::boxed::Box; 11 12 #[cfg(feature = "buffer_provider")] 13 use crate::unstable::{errors::ffi::DataError, provider::ffi::DataProvider}; 14 15 /// A type capable of looking up a property value from a string name. 16 #[diplomat::opaque] 17 #[diplomat::rust_link(icu::properties::PropertyParser, Struct)] 18 #[diplomat::rust_link(icu::properties::PropertyParserBorrowed, Struct)] 19 #[diplomat::rust_link(icu::properties::PropertyParser::new, FnInStruct)] 20 #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::new, FnInStruct, hidden)] 21 #[diplomat::rust_link( 22 icu::properties::props::NamedEnumeratedProperty::try_from_str, 23 FnInTrait, 24 hidden 25 )] 26 pub struct PropertyValueNameToEnumMapper(icu_properties::PropertyParser<u16>); 27 28 impl PropertyValueNameToEnumMapper { 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(icu::properties::PropertyParserBorrowed::get_strict, FnInStruct)] 33 #[diplomat::rust_link( 34 icu::properties::PropertyParserBorrowed::get_strict_u16, 35 FnInStruct, 36 hidden 37 )] 38 pub fn get_strict(&self, name: &DiplomatStr) -> i16 { 39 if let Ok(name) = core::str::from_utf8(name) { 40 self.0.as_borrowed().get_strict(name) 41 } else { 42 None 43 } 44 .map(|u_16| u_16 as i16) 45 .unwrap_or(-1) 46 } 47 48 /// Get the property value matching the given name, using loose matching 49 /// 50 /// Returns -1 if the name is unknown for this property 51 #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::get_loose, FnInStruct)] 52 #[diplomat::rust_link( 53 icu::properties::PropertyParserBorrowed::get_loose_u16, 54 FnInStruct, 55 hidden 56 )] 57 pub fn get_loose(&self, name: &DiplomatStr) -> i16 { 58 if let Ok(name) = core::str::from_utf8(name) { 59 self.0.as_borrowed().get_loose(name) 60 } else { 61 None 62 } 63 .map(|u_16| u_16 as i16) 64 .unwrap_or(-1) 65 } 66 67 /// Create a name-to-enum mapper for the `General_Category` property, using compiled data. 68 #[diplomat::rust_link(icu::properties::props::GeneralCategory, Enum)] 69 #[diplomat::attr(auto, named_constructor = "general_category")] 70 #[cfg(feature = "compiled_data")] 71 pub fn create_general_category() -> Box<PropertyValueNameToEnumMapper> { 72 Box::new(PropertyValueNameToEnumMapper( 73 icu_properties::PropertyParser::<icu_properties::props::GeneralCategory>::new() 74 .static_to_owned() 75 .erase(), 76 )) 77 } 78 79 /// Create a name-to-enum mapper for the `General_Category` property, using a particular data source. 80 #[diplomat::rust_link(icu::properties::props::GeneralCategory, Enum)] 81 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "general_category_with_provider")] 82 #[cfg(feature = "buffer_provider")] 83 pub fn create_general_category_with_provider( 84 provider: &DataProvider, 85 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 86 Ok(Box::new(PropertyValueNameToEnumMapper( 87 icu_properties::PropertyParser::< 88 icu_properties::props::GeneralCategory, 89 >::try_new_unstable(&provider.get_unstable()?)? 90 .erase(), 91 ))) 92 } 93 /// Create a name-to-enum mapper for the `Hangul_Syllable_Type` property, using compiled data. 94 #[diplomat::rust_link(icu::properties::props::HangulSyllableType, Struct)] 95 #[diplomat::attr(auto, named_constructor = "hangul_syllable_type")] 96 #[cfg(feature = "compiled_data")] 97 pub fn create_hangul_syllable_type() -> Box<PropertyValueNameToEnumMapper> { 98 Box::new(PropertyValueNameToEnumMapper( 99 icu_properties::PropertyParser::<icu_properties::props::HangulSyllableType>::new() 100 .static_to_owned() 101 .erase(), 102 )) 103 } 104 /// Create a name-to-enum mapper for the `Hangul_Syllable_Type` property, using a particular data source. 105 #[diplomat::rust_link(icu::properties::props::HangulSyllableType, Struct)] 106 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "hangul_syllable_type_with_provider")] 107 #[cfg(feature = "buffer_provider")] 108 pub fn create_hangul_syllable_type_with_provider( 109 provider: &DataProvider, 110 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 111 Ok(Box::new(PropertyValueNameToEnumMapper( 112 icu_properties::PropertyParser::< 113 icu_properties::props::HangulSyllableType, 114 >::try_new_unstable(&provider.get_unstable()?)? 115 .erase(), 116 ))) 117 } 118 /// Create a name-to-enum mapper for the `East_Asian_Width` property, using compiled data. 119 #[diplomat::rust_link(icu::properties::props::EastAsianWidth, Struct)] 120 #[diplomat::attr(auto, named_constructor = "east_asian_width")] 121 #[cfg(feature = "compiled_data")] 122 pub fn create_east_asian_width() -> Box<PropertyValueNameToEnumMapper> { 123 Box::new(PropertyValueNameToEnumMapper( 124 icu_properties::PropertyParser::<icu_properties::props::EastAsianWidth>::new() 125 .static_to_owned() 126 .erase(), 127 )) 128 } 129 /// Create a name-to-enum mapper for the `East_Asian_Width` property, using a particular data source. 130 #[diplomat::rust_link(icu::properties::props::EastAsianWidth, Struct)] 131 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "east_asian_width_with_provider")] 132 #[cfg(feature = "buffer_provider")] 133 pub fn create_east_asian_width_with_provider( 134 provider: &DataProvider, 135 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 136 Ok(Box::new(PropertyValueNameToEnumMapper( 137 icu_properties::PropertyParser::< 138 icu_properties::props::EastAsianWidth, 139 >::try_new_unstable(&provider.get_unstable()? 140 )? 141 .erase(), 142 ))) 143 } 144 /// Create a name-to-enum mapper for the `Bidi_Class` property, using compiled data. 145 #[diplomat::rust_link(icu::properties::props::BidiClass, Struct)] 146 #[diplomat::attr(auto, named_constructor = "bidi_class")] 147 #[cfg(feature = "compiled_data")] 148 pub fn create_bidi_class() -> Box<PropertyValueNameToEnumMapper> { 149 Box::new(PropertyValueNameToEnumMapper( 150 icu_properties::PropertyParser::<icu_properties::props::BidiClass>::new() 151 .static_to_owned() 152 .erase(), 153 )) 154 } 155 /// Create a name-to-enum mapper for the `Bidi_Class` property, using a particular data source. 156 #[diplomat::rust_link(icu::properties::props::BidiClass, Struct)] 157 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "bidi_class_with_provider")] 158 #[cfg(feature = "buffer_provider")] 159 pub fn create_bidi_class_with_provider( 160 provider: &DataProvider, 161 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 162 Ok(Box::new(PropertyValueNameToEnumMapper( 163 icu_properties::PropertyParser::<icu_properties::props::BidiClass>::try_new_unstable(&provider.get_unstable()?)? 164 .erase(), 165 ))) 166 } 167 /// Create a name-to-enum mapper for the `Indic_Syllabic_Category` property, using compiled data. 168 #[diplomat::rust_link(icu::properties::props::IndicSyllabicCategory, Struct)] 169 #[diplomat::attr(auto, named_constructor = "indic_syllabic_category")] 170 #[cfg(feature = "compiled_data")] 171 pub fn create_indic_syllabic_category() -> Box<PropertyValueNameToEnumMapper> { 172 Box::new(PropertyValueNameToEnumMapper(icu_properties::PropertyParser::<icu_properties::props::IndicSyllabicCategory>::new().static_to_owned().erase())) 173 } 174 /// Create a name-to-enum mapper for the `Indic_Syllabic_Category` property, using a particular data source. 175 #[diplomat::rust_link(icu::properties::props::IndicSyllabicCategory, Struct)] 176 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "indic_syllabic_category_with_provider")] 177 #[cfg(feature = "buffer_provider")] 178 pub fn create_indic_syllabic_category_with_provider( 179 provider: &DataProvider, 180 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 181 Ok( 182 Box::new( 183 PropertyValueNameToEnumMapper( 184 icu_properties::PropertyParser::< 185 icu_properties::props::IndicSyllabicCategory, 186 >::try_new_unstable(&provider.get_unstable()?)? 187 .erase(), 188 ), 189 ), 190 ) 191 } 192 /// Create a name-to-enum mapper for the `Line_Break` property, using compiled data. 193 #[diplomat::rust_link(icu::properties::props::LineBreak, Struct)] 194 #[diplomat::attr(auto, named_constructor = "line_break")] 195 #[cfg(feature = "compiled_data")] 196 pub fn create_line_break() -> Box<PropertyValueNameToEnumMapper> { 197 Box::new(PropertyValueNameToEnumMapper( 198 icu_properties::PropertyParser::<icu_properties::props::LineBreak>::new() 199 .static_to_owned() 200 .erase(), 201 )) 202 } 203 /// Create a name-to-enum mapper for the `Line_Break` property, using a particular data source. 204 #[diplomat::rust_link(icu::properties::props::LineBreak, Struct)] 205 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "line_break_with_provider")] 206 #[cfg(feature = "buffer_provider")] 207 pub fn create_line_break_with_provider( 208 provider: &DataProvider, 209 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 210 Ok(Box::new(PropertyValueNameToEnumMapper( 211 icu_properties::PropertyParser::<icu_properties::props::LineBreak>::try_new_unstable(&provider.get_unstable()? 212 )? 213 .erase(), 214 ))) 215 } 216 /// Create a name-to-enum mapper for the `Grapheme_Cluster_Break` property, using compiled data. 217 #[diplomat::rust_link(icu::properties::props::GraphemeClusterBreak, Struct)] 218 #[diplomat::attr(auto, named_constructor = "grapheme_cluster_break")] 219 #[cfg(feature = "compiled_data")] 220 pub fn create_grapheme_cluster_break() -> Box<PropertyValueNameToEnumMapper> { 221 Box::new(PropertyValueNameToEnumMapper( 222 icu_properties::PropertyParser::<icu_properties::props::GraphemeClusterBreak>::new( 223 ) 224 .static_to_owned() 225 .erase(), 226 )) 227 } 228 /// Create a name-to-enum mapper for the `Grapheme_Cluster_Break` property, using a particular data source. 229 #[diplomat::rust_link(icu::properties::props::GraphemeClusterBreak, Struct)] 230 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "grapheme_cluster_break_with_provider")] 231 #[cfg(feature = "buffer_provider")] 232 pub fn create_grapheme_cluster_break_with_provider( 233 provider: &DataProvider, 234 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 235 Ok( 236 Box::new( 237 PropertyValueNameToEnumMapper( 238 icu_properties::PropertyParser::< 239 icu_properties::props::GraphemeClusterBreak, 240 >::try_new_unstable(&provider.get_unstable()?)? 241 .erase(), 242 ), 243 ), 244 ) 245 } 246 /// Create a name-to-enum mapper for the `Word_Break` property, using compiled data. 247 #[diplomat::rust_link(icu::properties::props::WordBreak, Struct)] 248 #[diplomat::attr(auto, named_constructor = "word_break")] 249 #[cfg(feature = "compiled_data")] 250 pub fn create_word_break() -> Box<PropertyValueNameToEnumMapper> { 251 Box::new(PropertyValueNameToEnumMapper( 252 icu_properties::PropertyParser::<icu_properties::props::WordBreak>::new() 253 .static_to_owned() 254 .erase(), 255 )) 256 } 257 /// Create a name-to-enum mapper for the `Word_Break` property, using a particular data source. 258 #[diplomat::rust_link(icu::properties::props::WordBreak, Struct)] 259 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "word_break_with_provider")] 260 #[cfg(feature = "buffer_provider")] 261 pub fn create_word_break_with_provider( 262 provider: &DataProvider, 263 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 264 Ok(Box::new(PropertyValueNameToEnumMapper( 265 icu_properties::PropertyParser::<icu_properties::props::WordBreak>::try_new_unstable(&provider.get_unstable()?)? 266 .erase(), 267 ))) 268 } 269 /// Create a name-to-enum mapper for the `Sentence_Break` property, using compiled data. 270 #[diplomat::rust_link(icu::properties::props::SentenceBreak, Struct)] 271 #[diplomat::attr(auto, named_constructor = "sentence_break")] 272 #[cfg(feature = "compiled_data")] 273 pub fn create_sentence_break() -> Box<PropertyValueNameToEnumMapper> { 274 Box::new(PropertyValueNameToEnumMapper( 275 icu_properties::PropertyParser::<icu_properties::props::SentenceBreak>::new() 276 .static_to_owned() 277 .erase(), 278 )) 279 } 280 /// Create a name-to-enum mapper for the `Sentence_Break` property, using a particular data source. 281 #[diplomat::rust_link(icu::properties::props::SentenceBreak, Struct)] 282 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "sentence_break_with_provider")] 283 #[cfg(feature = "buffer_provider")] 284 pub fn create_sentence_break_with_provider( 285 provider: &DataProvider, 286 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 287 Ok(Box::new(PropertyValueNameToEnumMapper( 288 icu_properties::PropertyParser::< 289 icu_properties::props::SentenceBreak, 290 >::try_new_unstable(&provider.get_unstable()? 291 )? 292 .erase(), 293 ))) 294 } 295 /// Create a name-to-enum mapper for the `Script` property, using compiled data. 296 #[diplomat::rust_link(icu::properties::props::Script, Struct)] 297 #[diplomat::attr(auto, named_constructor = "script")] 298 #[cfg(feature = "compiled_data")] 299 pub fn create_script() -> Box<PropertyValueNameToEnumMapper> { 300 Box::new(PropertyValueNameToEnumMapper( 301 icu_properties::PropertyParser::<icu_properties::props::Script>::new() 302 .static_to_owned() 303 .erase(), 304 )) 305 } 306 /// Create a name-to-enum mapper for the `Script` property, using a particular data source. 307 #[diplomat::rust_link(icu::properties::props::Script, Struct)] 308 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "script_with_provider")] 309 #[cfg(feature = "buffer_provider")] 310 pub fn create_script_with_provider( 311 provider: &DataProvider, 312 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 313 Ok(Box::new(PropertyValueNameToEnumMapper( 314 icu_properties::PropertyParser::<icu_properties::props::Script>::try_new_unstable( 315 &provider.get_unstable()?, 316 )? 317 .erase(), 318 ))) 319 } 320 /// Create a name-to-enum mapper for the `Vertical_Orientation` property, using compiled data. 321 #[diplomat::rust_link(icu::properties::props::VerticalOrientation, Struct)] 322 #[diplomat::attr(auto, named_constructor = "vertical_orientation")] 323 #[cfg(feature = "compiled_data")] 324 pub fn create_vertical_orientation() -> Box<PropertyValueNameToEnumMapper> { 325 Box::new(PropertyValueNameToEnumMapper( 326 icu_properties::PropertyParser::<icu_properties::props::VerticalOrientation>::new() 327 .static_to_owned() 328 .erase(), 329 )) 330 } 331 /// Create a name-to-enum mapper for the `Vertical_Orientation` property, using a particular data source. 332 #[diplomat::rust_link(icu::properties::props::VerticalOrientation, Struct)] 333 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "vertical_orientation_with_provider")] 334 #[cfg(feature = "buffer_provider")] 335 pub fn create_vertical_orientation_with_provider( 336 provider: &DataProvider, 337 ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> { 338 Ok(Box::new(PropertyValueNameToEnumMapper( 339 icu_properties::PropertyParser::<icu_properties::props::VerticalOrientation>::try_new_unstable( 340 &provider.get_unstable()?, 341 )? 342 .erase(), 343 ))) 344 } 345 } 346 347 /// A type capable of looking up General Category Group values from a string name. 348 #[diplomat::opaque] 349 #[diplomat::rust_link(icu::properties::PropertyParser, Struct)] 350 #[diplomat::rust_link(icu::properties::props::GeneralCategory, Enum)] 351 pub struct GeneralCategoryNameToGroupMapper( 352 icu_properties::PropertyParser<icu_properties::props::GeneralCategoryGroup>, 353 ); 354 355 impl GeneralCategoryNameToGroupMapper { 356 /// Get the mask value matching the given name, using strict matching 357 /// 358 /// Returns 0 if the name is unknown for this property 359 #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::get_strict, FnInStruct)] 360 #[diplomat::rust_link( 361 icu::properties::PropertyParserBorrowed::get_strict_u16, 362 FnInStruct, 363 hidden 364 )] 365 pub fn get_strict(&self, name: &DiplomatStr) -> GeneralCategoryGroup { 366 if let Ok(name) = core::str::from_utf8(name) { 367 self.0.as_borrowed().get_strict(name) 368 } else { 369 None 370 } 371 .map(Into::into) 372 .unwrap_or_default() 373 } 374 375 /// Get the mask value matching the given name, using loose matching 376 /// 377 /// Returns 0 if the name is unknown for this property 378 #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::get_loose, FnInStruct)] 379 #[diplomat::rust_link( 380 icu::properties::PropertyParserBorrowed::get_loose_u16, 381 FnInStruct, 382 hidden 383 )] 384 pub fn get_loose(&self, name: &DiplomatStr) -> GeneralCategoryGroup { 385 if let Ok(name) = core::str::from_utf8(name) { 386 self.0.as_borrowed().get_loose(name) 387 } else { 388 None 389 } 390 .map(Into::into) 391 .unwrap_or_default() 392 } 393 /// Create a name-to-mask mapper for the `General_Category` property, using compiled data. 394 #[diplomat::rust_link(icu::properties::props::GeneralCategoryGroup, Struct)] 395 #[diplomat::attr(auto, constructor)] 396 #[cfg(feature = "compiled_data")] 397 pub fn create() -> Box<GeneralCategoryNameToGroupMapper> { 398 Box::new(GeneralCategoryNameToGroupMapper( 399 icu_properties::PropertyParser::<icu_properties::props::GeneralCategoryGroup>::new( 400 ) 401 .static_to_owned(), 402 )) 403 } 404 /// Create a name-to-mask mapper for the `General_Category` property, using a particular data source. 405 #[diplomat::rust_link(icu::properties::props::GeneralCategoryGroup, Struct)] 406 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")] 407 #[cfg(feature = "buffer_provider")] 408 pub fn create_with_provider( 409 provider: &DataProvider, 410 ) -> Result<Box<GeneralCategoryNameToGroupMapper>, DataError> { 411 Ok(Box::new( 412 GeneralCategoryNameToGroupMapper(icu_properties::PropertyParser::< 413 icu_properties::props::GeneralCategoryGroup, 414 >::try_new_unstable( 415 &provider.get_unstable()? 416 )?), 417 )) 418 } 419 } 420 }