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::properties_enums::ffi::GeneralCategoryGroup; 10 use alloc::boxed::Box; 11 12 #[cfg(feature = "buffer_provider")] 13 use crate::{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 )] get_strict(&self, name: &DiplomatStr) -> i1638 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 )] get_loose(&self, name: &DiplomatStr) -> i1657 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")] create_general_category() -> Box<PropertyValueNameToEnumMapper>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")] create_general_category_with_provider( provider: &DataProvider, ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError>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")] create_hangul_syllable_type() -> Box<PropertyValueNameToEnumMapper>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")] create_hangul_syllable_type_with_provider( provider: &DataProvider, ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError>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")] create_east_asian_width() -> Box<PropertyValueNameToEnumMapper>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")] create_east_asian_width_with_provider( provider: &DataProvider, ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError>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")] create_bidi_class() -> Box<PropertyValueNameToEnumMapper>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")] create_bidi_class_with_provider( provider: &DataProvider, ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError>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")] create_indic_syllabic_category() -> Box<PropertyValueNameToEnumMapper>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")] create_indic_syllabic_category_with_provider( provider: &DataProvider, ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError>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")] create_line_break() -> Box<PropertyValueNameToEnumMapper>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")] create_line_break_with_provider( provider: &DataProvider, ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError>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")] create_grapheme_cluster_break() -> Box<PropertyValueNameToEnumMapper>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")] create_grapheme_cluster_break_with_provider( provider: &DataProvider, ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError>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")] create_word_break() -> Box<PropertyValueNameToEnumMapper>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")] create_word_break_with_provider( provider: &DataProvider, ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError>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")] create_sentence_break() -> Box<PropertyValueNameToEnumMapper>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")] create_sentence_break_with_provider( provider: &DataProvider, ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError>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")] create_script() -> Box<PropertyValueNameToEnumMapper>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")] create_script_with_provider( provider: &DataProvider, ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError>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 } 321 322 /// A type capable of looking up General Category Group values from a string name. 323 #[diplomat::opaque] 324 #[diplomat::rust_link(icu::properties::PropertyParser, Struct)] 325 #[diplomat::rust_link(icu::properties::props::GeneralCategory, Struct)] 326 pub struct GeneralCategoryNameToGroupMapper( 327 icu_properties::PropertyParser<icu_properties::props::GeneralCategoryGroup>, 328 ); 329 330 impl GeneralCategoryNameToGroupMapper { 331 /// Get the mask value matching the given name, using strict matching 332 /// 333 /// Returns 0 if the name is unknown for this property 334 #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::get_strict, FnInStruct)] 335 #[diplomat::rust_link( 336 icu::properties::PropertyParserBorrowed::get_strict_u16, 337 FnInStruct, 338 hidden 339 )] get_strict(&self, name: &DiplomatStr) -> GeneralCategoryGroup340 pub fn get_strict(&self, name: &DiplomatStr) -> GeneralCategoryGroup { 341 if let Ok(name) = core::str::from_utf8(name) { 342 self.0.as_borrowed().get_strict(name) 343 } else { 344 None 345 } 346 .map(Into::into) 347 .unwrap_or_default() 348 } 349 350 /// Get the mask value matching the given name, using loose matching 351 /// 352 /// Returns 0 if the name is unknown for this property 353 #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::get_loose, FnInStruct)] 354 #[diplomat::rust_link( 355 icu::properties::PropertyParserBorrowed::get_loose_u16, 356 FnInStruct, 357 hidden 358 )] get_loose(&self, name: &DiplomatStr) -> GeneralCategoryGroup359 pub fn get_loose(&self, name: &DiplomatStr) -> GeneralCategoryGroup { 360 if let Ok(name) = core::str::from_utf8(name) { 361 self.0.as_borrowed().get_loose(name) 362 } else { 363 None 364 } 365 .map(Into::into) 366 .unwrap_or_default() 367 } 368 /// Create a name-to-mask mapper for the `General_Category` property, using compiled data. 369 #[diplomat::rust_link(icu_properties::props::GeneralCategoryGroup, Struct)] 370 #[diplomat::attr(auto, constructor)] 371 #[cfg(feature = "compiled_data")] create() -> Box<GeneralCategoryNameToGroupMapper>372 pub fn create() -> Box<GeneralCategoryNameToGroupMapper> { 373 Box::new(GeneralCategoryNameToGroupMapper( 374 icu_properties::PropertyParser::<icu_properties::props::GeneralCategoryGroup>::new( 375 ) 376 .static_to_owned(), 377 )) 378 } 379 /// Create a name-to-mask mapper for the `General_Category` property, using a particular data source. 380 #[diplomat::rust_link(icu_properties::props::GeneralCategoryGroup, Struct)] 381 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")] 382 #[cfg(feature = "buffer_provider")] create_with_provider( provider: &DataProvider, ) -> Result<Box<GeneralCategoryNameToGroupMapper>, DataError>383 pub fn create_with_provider( 384 provider: &DataProvider, 385 ) -> Result<Box<GeneralCategoryNameToGroupMapper>, DataError> { 386 Ok(Box::new( 387 GeneralCategoryNameToGroupMapper(icu_properties::PropertyParser::< 388 icu_properties::props::GeneralCategoryGroup, 389 >::try_new_unstable( 390 &provider.get_unstable()? 391 )?), 392 )) 393 } 394 } 395 } 396