exemplar_chars.rs (11572B)
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 11 #[cfg(feature = "buffer_provider")] 12 use crate::unstable::provider::ffi::DataProvider; 13 #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))] 14 use crate::unstable::{errors::ffi::DataError, locale_core::ffi::Locale}; 15 16 #[diplomat::opaque] 17 /// A set of "exemplar characters" for a given locale. 18 #[diplomat::rust_link(icu::locale, Mod)] 19 #[diplomat::rust_link(icu::locale::exemplar_chars::ExemplarCharacters, Struct)] 20 #[diplomat::rust_link(icu::locale::exemplar_chars::ExemplarCharactersBorrowed, Struct)] 21 pub struct ExemplarCharacters(pub icu_locale::exemplar_chars::ExemplarCharacters); 22 23 impl ExemplarCharacters { 24 /// Checks whether the string is in the set. 25 #[diplomat::rust_link( 26 icu::collections::codepointinvliststringlist::CodePointInversionListAndStringList::contains_str, 27 FnInStruct 28 )] 29 #[diplomat::attr(supports = method_overloading, rename = "contains")] 30 pub fn contains_str(&self, s: &DiplomatStr) -> bool { 31 let Ok(s) = core::str::from_utf8(s) else { 32 return false; 33 }; 34 self.0.as_borrowed().contains_str(s) 35 } 36 /// Checks whether the code point is in the set. 37 #[diplomat::rust_link( 38 icu::collections::codepointinvliststringlist::CodePointInversionListAndStringList::contains, 39 FnInStruct 40 )] 41 #[diplomat::rust_link( 42 icu::collections::codepointinvliststringlist::CodePointInversionListAndStringList::contains32, 43 FnInStruct, 44 hidden 45 )] 46 pub fn contains(&self, cp: DiplomatChar) -> bool { 47 self.0.as_borrowed().contains32(cp) 48 } 49 50 /// Create an [`ExemplarCharacters`] for the "main" set of exemplar characters for a given locale, using compiled data. 51 #[diplomat::rust_link( 52 icu::locale::exemplar_chars::ExemplarCharacters::try_new_main, 53 FnInStruct 54 )] 55 #[diplomat::rust_link( 56 icu::locale::exemplar_chars::ExemplarCharactersBorrowed::try_new_main, 57 FnInStruct, 58 hidden 59 )] 60 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "main")] 61 #[cfg(feature = "compiled_data")] 62 pub fn create_main(locale: &Locale) -> Result<Box<ExemplarCharacters>, DataError> { 63 let locale = locale.to_datalocale(); 64 Ok(Box::new(ExemplarCharacters( 65 icu_locale::exemplar_chars::ExemplarCharacters::try_new_main(&locale)? 66 .static_to_owned(), 67 ))) 68 } 69 70 /// Create an [`ExemplarCharacters`] for the "main" set of exemplar characters for a given locale, using a particular data source 71 #[diplomat::rust_link( 72 icu::locale::exemplar_chars::ExemplarCharacters::try_new_main, 73 FnInStruct 74 )] 75 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "main_with_provider")] 76 #[cfg(feature = "buffer_provider")] 77 pub fn create_main_with_provider( 78 provider: &DataProvider, 79 locale: &Locale, 80 ) -> Result<Box<ExemplarCharacters>, DataError> { 81 let locale = locale.to_datalocale(); 82 Ok(Box::new(ExemplarCharacters( 83 icu_locale::exemplar_chars::ExemplarCharacters::try_new_main_unstable( 84 &provider.get_unstable()?, 85 &locale, 86 )?, 87 ))) 88 } 89 90 /// Create an [`ExemplarCharacters`] for the "auxiliary" set of exemplar characters for a given locale, using compiled data. 91 #[diplomat::rust_link( 92 icu::locale::exemplar_chars::ExemplarCharacters::try_new_auxiliary, 93 FnInStruct 94 )] 95 #[diplomat::rust_link( 96 icu::locale::exemplar_chars::ExemplarCharactersBorrowed::try_new_auxiliary, 97 FnInStruct, 98 hidden 99 )] 100 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "auxiliary")] 101 #[cfg(feature = "compiled_data")] 102 pub fn create_auxiliary(locale: &Locale) -> Result<Box<ExemplarCharacters>, DataError> { 103 let locale = locale.to_datalocale(); 104 Ok(Box::new(ExemplarCharacters( 105 icu_locale::exemplar_chars::ExemplarCharacters::try_new_auxiliary(&locale)? 106 .static_to_owned(), 107 ))) 108 } 109 /// Create an [`ExemplarCharacters`] for the "auxiliary" set of exemplar characters for a given locale, using compiled data. 110 #[diplomat::rust_link( 111 icu::locale::exemplar_chars::ExemplarCharacters::try_new_auxiliary, 112 FnInStruct 113 )] 114 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "auxiliary_with_provider")] 115 #[cfg(feature = "buffer_provider")] 116 pub fn create_auxiliary_with_provider( 117 provider: &DataProvider, 118 locale: &Locale, 119 ) -> Result<Box<ExemplarCharacters>, DataError> { 120 let locale = locale.to_datalocale(); 121 Ok(Box::new(ExemplarCharacters( 122 icu_locale::exemplar_chars::ExemplarCharacters::try_new_auxiliary_unstable( 123 &provider.get_unstable()?, 124 &locale, 125 )?, 126 ))) 127 } 128 129 /// Create an [`ExemplarCharacters`] for the "punctuation" set of exemplar characters for a given locale, using compiled data. 130 #[diplomat::rust_link( 131 icu::locale::exemplar_chars::ExemplarCharacters::try_new_punctuation, 132 FnInStruct 133 )] 134 #[diplomat::rust_link( 135 icu::locale::exemplar_chars::ExemplarCharactersBorrowed::try_new_punctuation, 136 FnInStruct, 137 hidden 138 )] 139 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "punctuation")] 140 #[cfg(feature = "compiled_data")] 141 pub fn create_punctuation(locale: &Locale) -> Result<Box<ExemplarCharacters>, DataError> { 142 let locale = locale.to_datalocale(); 143 Ok(Box::new(ExemplarCharacters( 144 icu_locale::exemplar_chars::ExemplarCharacters::try_new_punctuation(&locale)? 145 .static_to_owned(), 146 ))) 147 } 148 /// Create an [`ExemplarCharacters`] for the "punctuation" set of exemplar characters for a given locale, using compiled data. 149 #[diplomat::rust_link( 150 icu::locale::exemplar_chars::ExemplarCharacters::try_new_punctuation, 151 FnInStruct 152 )] 153 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "punctuation_with_provider")] 154 #[cfg(feature = "buffer_provider")] 155 pub fn create_punctuation_with_provider( 156 provider: &DataProvider, 157 locale: &Locale, 158 ) -> Result<Box<ExemplarCharacters>, DataError> { 159 let locale = locale.to_datalocale(); 160 Ok(Box::new(ExemplarCharacters( 161 icu_locale::exemplar_chars::ExemplarCharacters::try_new_punctuation_unstable( 162 &provider.get_unstable()?, 163 &locale, 164 )?, 165 ))) 166 } 167 168 /// Create an [`ExemplarCharacters`] for the "numbers" set of exemplar characters for a given locale, using compiled data. 169 #[diplomat::rust_link( 170 icu::locale::exemplar_chars::ExemplarCharacters::try_new_numbers, 171 FnInStruct 172 )] 173 #[diplomat::rust_link( 174 icu::locale::exemplar_chars::ExemplarCharactersBorrowed::try_new_numbers, 175 FnInStruct, 176 hidden 177 )] 178 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "numbers")] 179 #[cfg(feature = "compiled_data")] 180 pub fn create_numbers(locale: &Locale) -> Result<Box<ExemplarCharacters>, DataError> { 181 let locale = locale.to_datalocale(); 182 Ok(Box::new(ExemplarCharacters( 183 icu_locale::exemplar_chars::ExemplarCharacters::try_new_numbers(&locale)? 184 .static_to_owned(), 185 ))) 186 } 187 188 /// Create an [`ExemplarCharacters`] for the "numbers" set of exemplar characters for a given locale, using compiled data. 189 #[diplomat::rust_link( 190 icu::locale::exemplar_chars::ExemplarCharacters::try_new_numbers, 191 FnInStruct 192 )] 193 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "numbers_with_provider")] 194 #[cfg(feature = "buffer_provider")] 195 pub fn create_numbers_with_provider( 196 provider: &DataProvider, 197 locale: &Locale, 198 ) -> Result<Box<ExemplarCharacters>, DataError> { 199 let locale = locale.to_datalocale(); 200 Ok(Box::new(ExemplarCharacters( 201 icu_locale::exemplar_chars::ExemplarCharacters::try_new_numbers_unstable( 202 &provider.get_unstable()?, 203 &locale, 204 )?, 205 ))) 206 } 207 208 /// Create an [`ExemplarCharacters`] for the "index" set of exemplar characters for a given locale, using compiled data. 209 #[diplomat::rust_link( 210 icu::locale::exemplar_chars::ExemplarCharacters::try_new_index, 211 FnInStruct 212 )] 213 #[diplomat::rust_link( 214 icu::locale::exemplar_chars::ExemplarCharactersBorrowed::try_new_index, 215 FnInStruct, 216 hidden 217 )] 218 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "index")] 219 #[cfg(feature = "compiled_data")] 220 pub fn create_index(locale: &Locale) -> Result<Box<ExemplarCharacters>, DataError> { 221 let locale = locale.to_datalocale(); 222 Ok(Box::new(ExemplarCharacters( 223 icu_locale::exemplar_chars::ExemplarCharacters::try_new_index(&locale)? 224 .static_to_owned(), 225 ))) 226 } 227 228 /// Create an [`ExemplarCharacters`] for the "index" set of exemplar characters for a given locale, using compiled data. 229 #[diplomat::rust_link( 230 icu::locale::exemplar_chars::ExemplarCharacters::try_new_index, 231 FnInStruct 232 )] 233 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "index_with_provider")] 234 #[cfg(feature = "buffer_provider")] 235 pub fn create_index_with_provider( 236 provider: &DataProvider, 237 locale: &Locale, 238 ) -> Result<Box<ExemplarCharacters>, DataError> { 239 let locale = locale.to_datalocale(); 240 Ok(Box::new(ExemplarCharacters( 241 icu_locale::exemplar_chars::ExemplarCharacters::try_new_index_unstable( 242 &provider.get_unstable()?, 243 &locale, 244 )?, 245 ))) 246 } 247 } 248 }