properties_maps.rs (23728B)
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 alloc::boxed::Box; 10 #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))] 11 use icu_properties::props::{ 12 BidiClass, CanonicalCombiningClass, EastAsianWidth, GeneralCategory, GraphemeClusterBreak, 13 HangulSyllableType, IndicSyllabicCategory, JoiningType, LineBreak, Script, SentenceBreak, 14 VerticalOrientation, WordBreak, 15 }; 16 17 use crate::unstable::properties_enums::ffi::GeneralCategoryGroup; 18 use crate::unstable::properties_iter::ffi::CodePointRangeIterator; 19 use crate::unstable::properties_sets::ffi::CodePointSetData; 20 #[cfg(feature = "buffer_provider")] 21 use crate::unstable::{errors::ffi::DataError, provider::ffi::DataProvider}; 22 23 #[diplomat::opaque] 24 /// An ICU4X Unicode Map Property object, capable of querying whether a code point (key) to obtain the Unicode property value, for a specific Unicode property. 25 /// 26 /// For properties whose values fit into 8 bits. 27 #[diplomat::rust_link(icu::properties, Mod)] 28 #[diplomat::rust_link(icu::properties::CodePointMapData, Struct)] 29 #[diplomat::rust_link(icu::properties::CodePointMapDataBorrowed, Struct)] 30 #[diplomat::rust_link(icu::properties::CodePointMapData::new, FnInStruct, hidden)] 31 #[diplomat::rust_link(icu::properties::CodePointMapDataBorrowed::new, FnInStruct, hidden)] 32 #[diplomat::rust_link( 33 icu::properties::CodePointMapData::try_into_converted, 34 FnInStruct, 35 hidden 36 )] 37 pub struct CodePointMapData8(icu_properties::CodePointMapData<u8>); 38 39 #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))] 40 fn convert_8<P: icu_collections::codepointtrie::TrieValue>( 41 data: icu_properties::CodePointMapData<P>, 42 ) -> Box<CodePointMapData8> { 43 #[allow(clippy::unwrap_used)] // infallible for the chosen properties 44 Box::new(CodePointMapData8( 45 data.try_into_converted().map_err(|_| ()).unwrap(), 46 )) 47 } 48 49 impl CodePointMapData8 { 50 /// Gets the value for a code point. 51 #[diplomat::rust_link(icu::properties::CodePointMapDataBorrowed::get, FnInStruct)] 52 #[diplomat::rust_link(icu::properties::CodePointMapDataBorrowed::get32, FnInStruct, hidden)] 53 #[diplomat::attr(auto, indexer)] 54 pub fn get(&self, cp: DiplomatChar) -> u8 { 55 self.0.as_borrowed().get32(cp) 56 } 57 58 /// Produces an iterator over ranges of code points that map to `value` 59 #[diplomat::rust_link( 60 icu::properties::CodePointMapDataBorrowed::iter_ranges_for_value, 61 FnInStruct 62 )] 63 pub fn iter_ranges_for_value<'a>(&'a self, value: u8) -> Box<CodePointRangeIterator<'a>> { 64 Box::new(CodePointRangeIterator(Box::new( 65 self.0.as_borrowed().iter_ranges_for_value(value), 66 ))) 67 } 68 69 /// Produces an iterator over ranges of code points that do not map to `value` 70 #[diplomat::rust_link( 71 icu::properties::CodePointMapDataBorrowed::iter_ranges_for_value_complemented, 72 FnInStruct 73 )] 74 pub fn iter_ranges_for_value_complemented<'a>( 75 &'a self, 76 value: u8, 77 ) -> Box<CodePointRangeIterator<'a>> { 78 Box::new(CodePointRangeIterator(Box::new( 79 self.0 80 .as_borrowed() 81 .iter_ranges_for_value_complemented(value), 82 ))) 83 } 84 85 /// Given a mask value (the nth bit marks property value = n), produce an iterator over ranges of code points 86 /// whose property values are contained in the mask. 87 /// 88 /// The main mask property supported is that for General_Category, which can be obtained via `general_category_to_mask()` or 89 /// by using `GeneralCategoryNameToMaskMapper` 90 /// 91 /// Should only be used on maps for properties with values less than 32 (like Generak_Category), 92 /// other maps will have unpredictable results 93 #[diplomat::rust_link( 94 icu::properties::CodePointMapDataBorrowed::iter_ranges_for_group, 95 FnInStruct 96 )] 97 pub fn iter_ranges_for_group<'a>( 98 &'a self, 99 group: GeneralCategoryGroup, 100 ) -> Box<CodePointRangeIterator<'a>> { 101 let ranges = self 102 .0 103 .as_borrowed() 104 .iter_ranges_mapped(move |v| { 105 let val_mask = 1_u32.checked_shl(v.into()).unwrap_or(0); 106 val_mask & group.mask != 0 107 }) 108 .filter(|v| v.value) 109 .map(|v| v.range); 110 Box::new(CodePointRangeIterator(Box::new(ranges))) 111 } 112 113 /// Gets a [`CodePointSetData`] representing all entries in this map that map to the given value 114 #[diplomat::rust_link( 115 icu::properties::CodePointMapDataBorrowed::get_set_for_value, 116 FnInStruct 117 )] 118 pub fn get_set_for_value(&self, value: u8) -> Box<CodePointSetData> { 119 Box::new(CodePointSetData( 120 self.0.as_borrowed().get_set_for_value(value), 121 )) 122 } 123 124 /// Create a map for the `General_Category` property, using compiled data. 125 #[diplomat::rust_link(icu::properties::props::GeneralCategory, Enum)] 126 #[diplomat::attr(auto, named_constructor = "general_category")] 127 #[cfg(feature = "compiled_data")] 128 pub fn create_general_category() -> Box<CodePointMapData8> { 129 convert_8(icu_properties::CodePointMapData::<GeneralCategory>::new().static_to_owned()) 130 } 131 132 /// Create a map for the `General_Category` property, using a particular data source 133 #[diplomat::rust_link(icu::properties::props::GeneralCategory, Enum)] 134 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "general_category_with_provider")] 135 #[cfg(feature = "buffer_provider")] 136 pub fn create_general_category_with_provider( 137 provider: &DataProvider, 138 ) -> Result<Box<CodePointMapData8>, DataError> { 139 Ok(convert_8(icu_properties::CodePointMapData::< 140 GeneralCategory, 141 >::try_new_unstable( 142 &provider.get_unstable()? 143 )?)) 144 } 145 146 /// Create a map for the `Bidi_Class` property, using compiled data. 147 #[diplomat::rust_link(icu::properties::props::BidiClass, Struct)] 148 #[diplomat::attr(auto, named_constructor = "bidi_class")] 149 #[cfg(feature = "compiled_data")] 150 pub fn create_bidi_class() -> Box<CodePointMapData8> { 151 convert_8(icu_properties::CodePointMapData::<BidiClass>::new().static_to_owned()) 152 } 153 154 /// Create a map for the `Bidi_Class` property, using a particular data source. 155 #[diplomat::rust_link(icu::properties::props::BidiClass, Struct)] 156 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "bidi_class_with_provider")] 157 #[cfg(feature = "buffer_provider")] 158 pub fn create_bidi_class_with_provider( 159 provider: &DataProvider, 160 ) -> Result<Box<CodePointMapData8>, DataError> { 161 Ok(convert_8( 162 icu_properties::CodePointMapData::<BidiClass>::try_new_unstable( 163 &provider.get_unstable()?, 164 )?, 165 )) 166 } 167 /// Create a map for the `East_Asian_Width` property, using compiled data. 168 #[diplomat::rust_link(icu::properties::props::EastAsianWidth, Struct)] 169 #[diplomat::attr(auto, named_constructor = "east_asian_width")] 170 #[cfg(feature = "compiled_data")] 171 pub fn create_east_asian_width() -> Box<CodePointMapData8> { 172 convert_8(icu_properties::CodePointMapData::<EastAsianWidth>::new().static_to_owned()) 173 } 174 175 /// Create a map for the `East_Asian_Width` property, using a particular data source. 176 #[diplomat::rust_link(icu::properties::props::EastAsianWidth, Struct)] 177 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "east_asian_width_with_provider")] 178 #[cfg(feature = "buffer_provider")] 179 pub fn create_east_asian_width_with_provider( 180 provider: &DataProvider, 181 ) -> Result<Box<CodePointMapData8>, DataError> { 182 Ok(convert_8( 183 icu_properties::CodePointMapData::<EastAsianWidth>::try_new_unstable( 184 &provider.get_unstable()?, 185 )?, 186 )) 187 } 188 /// Create a map for the `Hangul_Syllable_Type` property, using compiled data. 189 #[diplomat::rust_link(icu::properties::props::HangulSyllableType, Struct)] 190 #[diplomat::attr(auto, named_constructor = "hangul_syllable_type")] 191 #[cfg(feature = "compiled_data")] 192 pub fn create_hangul_syllable_type() -> Box<CodePointMapData8> { 193 convert_8( 194 icu_properties::CodePointMapData::<HangulSyllableType>::new().static_to_owned(), 195 ) 196 } 197 /// Create a map for the `Hangul_Syllable_Type` property, using a particular data source. 198 #[diplomat::rust_link(icu::properties::props::HangulSyllableType, Struct)] 199 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "hangul_syllable_type_with_provider")] 200 #[cfg(feature = "buffer_provider")] 201 pub fn create_hangul_syllable_type_with_provider( 202 provider: &DataProvider, 203 ) -> Result<Box<CodePointMapData8>, DataError> { 204 Ok(convert_8(icu_properties::CodePointMapData::< 205 HangulSyllableType, 206 >::try_new_unstable( 207 &provider.get_unstable()? 208 )?)) 209 } 210 /// Create a map for the `Indic_Syllabic_Property` property, using compiled data. 211 #[diplomat::rust_link(icu::properties::props::IndicSyllabicCategory, Struct)] 212 #[diplomat::attr(auto, named_constructor = "indic_syllabic_category")] 213 #[cfg(feature = "compiled_data")] 214 pub fn create_indic_syllabic_category() -> Box<CodePointMapData8> { 215 convert_8( 216 icu_properties::CodePointMapData::<IndicSyllabicCategory>::new().static_to_owned(), 217 ) 218 } 219 /// Create a map for the `Indic_Syllabic_Property` property, using a particular data source. 220 #[diplomat::rust_link(icu::properties::props::IndicSyllabicCategory, Struct)] 221 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "indic_syllabic_category_with_provider")] 222 #[cfg(feature = "buffer_provider")] 223 pub fn create_indic_syllabic_category_with_provider( 224 provider: &DataProvider, 225 ) -> Result<Box<CodePointMapData8>, DataError> { 226 Ok(convert_8(icu_properties::CodePointMapData::< 227 IndicSyllabicCategory, 228 >::try_new_unstable( 229 &provider.get_unstable()? 230 )?)) 231 } 232 /// Create a map for the `Line_Break` property, using compiled data. 233 #[diplomat::rust_link(icu::properties::props::LineBreak, Struct)] 234 #[diplomat::attr(auto, named_constructor = "line_break")] 235 #[cfg(feature = "compiled_data")] 236 pub fn create_line_break() -> Box<CodePointMapData8> { 237 convert_8(icu_properties::CodePointMapData::<LineBreak>::new().static_to_owned()) 238 } 239 /// Create a map for the `Line_Break` property, using a particular data source. 240 #[diplomat::rust_link(icu::properties::props::LineBreak, Struct)] 241 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "line_break_with_provider")] 242 #[cfg(feature = "buffer_provider")] 243 pub fn create_line_break_with_provider( 244 provider: &DataProvider, 245 ) -> Result<Box<CodePointMapData8>, DataError> { 246 Ok(convert_8( 247 icu_properties::CodePointMapData::<LineBreak>::try_new_unstable( 248 &provider.get_unstable()?, 249 )?, 250 )) 251 } 252 /// Create a map for the `Grapheme_Cluster_Break` property, using compiled data. 253 #[diplomat::rust_link(icu::properties::props::GraphemeClusterBreak, Struct)] 254 #[diplomat::attr(auto, named_constructor = "grapheme_cluster_break")] 255 #[cfg(feature = "compiled_data")] 256 pub fn create_grapheme_cluster_break() -> Box<CodePointMapData8> { 257 convert_8( 258 icu_properties::CodePointMapData::<GraphemeClusterBreak>::new().static_to_owned(), 259 ) 260 } 261 /// Create a map for the `Grapheme_Cluster_Break` property, using a particular data source. 262 #[diplomat::rust_link(icu::properties::props::GraphemeClusterBreak, Struct)] 263 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "grapheme_cluster_break_with_provider")] 264 #[cfg(feature = "buffer_provider")] 265 pub fn create_grapheme_cluster_break_with_provider( 266 provider: &DataProvider, 267 ) -> Result<Box<CodePointMapData8>, DataError> { 268 Ok(convert_8(icu_properties::CodePointMapData::< 269 GraphemeClusterBreak, 270 >::try_new_unstable( 271 &provider.get_unstable()? 272 )?)) 273 } 274 /// Create a map for the `Word_Break` property, using compiled data. 275 #[diplomat::rust_link(icu::properties::props::WordBreak, Struct)] 276 #[diplomat::attr(auto, named_constructor = "word_break")] 277 #[cfg(feature = "compiled_data")] 278 pub fn create_word_break() -> Box<CodePointMapData8> { 279 convert_8(icu_properties::CodePointMapData::<WordBreak>::new().static_to_owned()) 280 } 281 /// Create a map for the `Word_Break` property, using a particular data source. 282 #[diplomat::rust_link(icu::properties::props::WordBreak, Struct)] 283 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "word_break_with_provider")] 284 #[cfg(feature = "buffer_provider")] 285 pub fn create_word_break_with_provider( 286 provider: &DataProvider, 287 ) -> Result<Box<CodePointMapData8>, DataError> { 288 Ok(convert_8( 289 icu_properties::CodePointMapData::<WordBreak>::try_new_unstable( 290 &provider.get_unstable()?, 291 )?, 292 )) 293 } 294 /// Create a map for the `Sentence_Break` property, using compiled data. 295 #[diplomat::rust_link(icu::properties::props::SentenceBreak, Struct)] 296 #[diplomat::attr(auto, named_constructor = "sentence_break")] 297 #[cfg(feature = "compiled_data")] 298 pub fn create_sentence_break() -> Box<CodePointMapData8> { 299 convert_8(icu_properties::CodePointMapData::<SentenceBreak>::new().static_to_owned()) 300 } 301 /// Create a map for the `Sentence_Break` property, using a particular data source. 302 #[diplomat::rust_link(icu::properties::props::SentenceBreak, Struct)] 303 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "sentence_break_with_provider")] 304 #[cfg(feature = "buffer_provider")] 305 pub fn create_sentence_break_with_provider( 306 provider: &DataProvider, 307 ) -> Result<Box<CodePointMapData8>, DataError> { 308 Ok(convert_8( 309 icu_properties::CodePointMapData::<SentenceBreak>::try_new_unstable( 310 &provider.get_unstable()?, 311 )?, 312 )) 313 } 314 /// Create a map for the `Joining_Type` property, using compiled data. 315 #[diplomat::rust_link(icu::properties::props::JoiningType, Struct)] 316 #[diplomat::attr(auto, named_constructor = "joining_type")] 317 #[cfg(feature = "compiled_data")] 318 pub fn create_joining_type() -> Box<CodePointMapData8> { 319 convert_8(icu_properties::CodePointMapData::<JoiningType>::new().static_to_owned()) 320 } 321 322 /// Create a map for the `Joining_Type` property, using a particular data source. 323 #[diplomat::rust_link(icu::properties::props::JoiningType, Struct)] 324 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "joining_type_with_provider")] 325 #[cfg(feature = "buffer_provider")] 326 pub fn create_joining_type_with_provider( 327 provider: &DataProvider, 328 ) -> Result<Box<CodePointMapData8>, DataError> { 329 Ok(convert_8( 330 icu_properties::CodePointMapData::<JoiningType>::try_new_unstable( 331 &provider.get_unstable()?, 332 )?, 333 )) 334 } 335 /// Create a map for the `Canonical_Combining_Class` property, using compiled data. 336 #[diplomat::rust_link(icu::properties::props::CanonicalCombiningClass, Struct)] 337 #[diplomat::attr(auto, named_constructor = "canonical_combining_class")] 338 #[cfg(feature = "compiled_data")] 339 pub fn create_canonical_combining_class() -> Box<CodePointMapData8> { 340 convert_8( 341 icu_properties::CodePointMapData::<CanonicalCombiningClass>::new() 342 .static_to_owned(), 343 ) 344 } 345 /// Create a map for the `Canonical_Combining_Class` property, using a particular data source. 346 #[diplomat::rust_link(icu::properties::props::CanonicalCombiningClass, Struct)] 347 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "canonical_combining_class_with_provider")] 348 #[cfg(feature = "buffer_provider")] 349 pub fn create_canonical_combining_class_with_provider( 350 provider: &DataProvider, 351 ) -> Result<Box<CodePointMapData8>, DataError> { 352 Ok(convert_8(icu_properties::CodePointMapData::< 353 CanonicalCombiningClass, 354 >::try_new_unstable( 355 &provider.get_unstable()? 356 )?)) 357 } 358 /// Create a map for the `Vertical_Orientation` property, using compiled data. 359 #[diplomat::rust_link(icu::properties::props::VerticalOrientation, Struct)] 360 #[diplomat::attr(auto, named_constructor = "vertical_orientation")] 361 #[cfg(feature = "compiled_data")] 362 pub fn create_vertical_orientation() -> Box<CodePointMapData8> { 363 convert_8( 364 icu_properties::CodePointMapData::<VerticalOrientation>::new().static_to_owned(), 365 ) 366 } 367 /// Create a map for the `Vertical_Orientation` property, using a particular data source. 368 #[diplomat::rust_link(icu::properties::props::VerticalOrientation, Struct)] 369 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "vertical_orientation_with_provider")] 370 #[cfg(feature = "buffer_provider")] 371 pub fn create_vertical_orientation_with_provider( 372 provider: &DataProvider, 373 ) -> Result<Box<CodePointMapData8>, DataError> { 374 Ok(convert_8(icu_properties::CodePointMapData::< 375 VerticalOrientation, 376 >::try_new_unstable( 377 &provider.get_unstable()? 378 )?)) 379 } 380 } 381 382 #[diplomat::opaque] 383 /// An ICU4X Unicode Map Property object, capable of querying whether a code point (key) to obtain the Unicode property value, for a specific Unicode property. 384 /// 385 /// For properties whose values fit into 16 bits. 386 #[diplomat::rust_link(icu::properties, Mod)] 387 #[diplomat::rust_link(icu::properties::CodePointMapData, Struct)] 388 #[diplomat::rust_link(icu::properties::CodePointMapDataBorrowed, Struct)] 389 pub struct CodePointMapData16(icu_properties::CodePointMapData<u16>); 390 391 impl CodePointMapData16 { 392 /// Gets the value for a code point. 393 #[diplomat::rust_link(icu::properties::CodePointMapDataBorrowed::get, FnInStruct)] 394 #[diplomat::rust_link(icu::properties::CodePointMapDataBorrowed::get32, FnInStruct, hidden)] 395 #[diplomat::attr(auto, indexer)] 396 pub fn get(&self, cp: DiplomatChar) -> u16 { 397 self.0.as_borrowed().get32(cp) 398 } 399 400 /// Produces an iterator over ranges of code points that map to `value` 401 #[diplomat::rust_link( 402 icu::properties::CodePointMapDataBorrowed::iter_ranges_for_value, 403 FnInStruct 404 )] 405 pub fn iter_ranges_for_value<'a>(&'a self, value: u16) -> Box<CodePointRangeIterator<'a>> { 406 Box::new(CodePointRangeIterator(Box::new( 407 self.0.as_borrowed().iter_ranges_for_value(value), 408 ))) 409 } 410 411 /// Produces an iterator over ranges of code points that do not map to `value` 412 #[diplomat::rust_link( 413 icu::properties::CodePointMapDataBorrowed::iter_ranges_for_value_complemented, 414 FnInStruct 415 )] 416 pub fn iter_ranges_for_value_complemented<'a>( 417 &'a self, 418 value: u16, 419 ) -> Box<CodePointRangeIterator<'a>> { 420 Box::new(CodePointRangeIterator(Box::new( 421 self.0 422 .as_borrowed() 423 .iter_ranges_for_value_complemented(value), 424 ))) 425 } 426 427 /// Gets a [`CodePointSetData`] representing all entries in this map that map to the given value 428 #[diplomat::rust_link( 429 icu::properties::CodePointMapDataBorrowed::get_set_for_value, 430 FnInStruct 431 )] 432 pub fn get_set_for_value(&self, value: u16) -> Box<CodePointSetData> { 433 Box::new(CodePointSetData( 434 self.0.as_borrowed().get_set_for_value(value), 435 )) 436 } 437 438 /// Create a map for the `Script` property, using compiled data. 439 #[diplomat::rust_link(icu::properties::props::Script, Struct)] 440 #[diplomat::attr(auto, named_constructor = "script")] 441 #[cfg(feature = "compiled_data")] 442 pub fn create_script() -> Box<CodePointMapData16> { 443 #[allow(clippy::unwrap_used)] // script is a 16-bit property 444 let data = icu_properties::CodePointMapData::<Script>::new() 445 .static_to_owned() 446 .try_into_converted() 447 .map_err(|_| ()) 448 .unwrap(); 449 Box::new(CodePointMapData16(data)) 450 } 451 452 /// Create a map for the `Script` property, using a particular data source. 453 #[diplomat::rust_link(icu::properties::props::Script, Struct)] 454 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "script_with_provider")] 455 #[cfg(feature = "buffer_provider")] 456 pub fn create_script_with_provider( 457 provider: &DataProvider, 458 ) -> Result<Box<CodePointMapData16>, DataError> { 459 #[allow(clippy::unwrap_used)] // script is a 16-bit property 460 Ok(Box::new(CodePointMapData16( 461 icu_properties::CodePointMapData::<Script>::try_new_unstable( 462 &provider.get_unstable()?, 463 )? 464 .try_into_converted() 465 .map_err(|_| ()) 466 .unwrap(), 467 ))) 468 } 469 } 470 }