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::{errors::ffi::DataError, provider::ffi::DataProvider}; 13 14 /// Lookup of the Canonical_Combining_Class Unicode property 15 #[diplomat::opaque] 16 #[diplomat::rust_link(icu::normalizer::properties::CanonicalCombiningClassMap, Struct)] 17 #[diplomat::rust_link( 18 icu::normalizer::properties::CanonicalCombiningClassMapBorrowed, 19 Struct, 20 hidden 21 )] 22 pub struct CanonicalCombiningClassMap( 23 pub icu_normalizer::properties::CanonicalCombiningClassMap, 24 ); 25 26 impl CanonicalCombiningClassMap { 27 /// Construct a new CanonicalCombiningClassMap instance for NFC using compiled data. 28 #[diplomat::rust_link( 29 icu::normalizer::properties::CanonicalCombiningClassMap::new, 30 FnInStruct 31 )] 32 #[diplomat::rust_link( 33 icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::new, 34 FnInStruct, 35 hidden 36 )] 37 #[diplomat::attr(auto, constructor)] 38 #[cfg(feature = "compiled_data")] create() -> Box<CanonicalCombiningClassMap>39 pub fn create() -> Box<CanonicalCombiningClassMap> { 40 Box::new(CanonicalCombiningClassMap( 41 icu_normalizer::properties::CanonicalCombiningClassMap::new().static_to_owned(), 42 )) 43 } 44 45 /// Construct a new CanonicalCombiningClassMap instance for NFC using a particular data source. 46 #[diplomat::rust_link( 47 icu::normalizer::properties::CanonicalCombiningClassMap::new, 48 FnInStruct 49 )] 50 #[diplomat::rust_link( 51 icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::new, 52 FnInStruct, 53 hidden 54 )] 55 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")] 56 #[cfg(feature = "buffer_provider")] create_with_provider( provider: &DataProvider, ) -> Result<Box<CanonicalCombiningClassMap>, DataError>57 pub fn create_with_provider( 58 provider: &DataProvider, 59 ) -> Result<Box<CanonicalCombiningClassMap>, DataError> { 60 Ok(Box::new(CanonicalCombiningClassMap(icu_normalizer::properties::CanonicalCombiningClassMap::try_new_with_buffer_provider(provider.get()?)?))) 61 } 62 63 #[diplomat::rust_link( 64 icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::get, 65 FnInStruct 66 )] 67 #[diplomat::rust_link( 68 icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::get32, 69 FnInStruct, 70 hidden 71 )] 72 #[diplomat::rust_link( 73 icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::get32_u8, 74 FnInStruct, 75 hidden 76 )] 77 #[diplomat::rust_link( 78 icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::get_u8, 79 FnInStruct, 80 hidden 81 )] 82 #[diplomat::rust_link( 83 icu::properties::properties::CanonicalCombiningClassMapBorrowed, 84 Struct, 85 compact 86 )] 87 #[diplomat::attr(auto, indexer)] get(&self, ch: DiplomatChar) -> u888 pub fn get(&self, ch: DiplomatChar) -> u8 { 89 self.0.as_borrowed().get32_u8(ch) 90 } 91 } 92 93 /// The raw canonical composition operation. 94 /// 95 /// Callers should generally use ComposingNormalizer unless they specifically need raw composition operations 96 #[diplomat::opaque] 97 #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition, Struct)] 98 #[diplomat::rust_link( 99 icu::normalizer::properties::CanonicalCompositionBorrowed, 100 Struct, 101 hidden 102 )] 103 pub struct CanonicalComposition(pub icu_normalizer::properties::CanonicalComposition); 104 105 impl CanonicalComposition { 106 /// Construct a new CanonicalComposition instance for NFC using compiled data. 107 #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition::new, FnInStruct)] 108 #[diplomat::rust_link( 109 icu::normalizer::properties::CanonicalCompositionBorrowed::new, 110 FnInStruct, 111 hidden 112 )] 113 #[diplomat::attr(auto, constructor)] 114 #[cfg(feature = "compiled_data")] create() -> Box<CanonicalComposition>115 pub fn create() -> Box<CanonicalComposition> { 116 Box::new(CanonicalComposition( 117 icu_normalizer::properties::CanonicalComposition::new().static_to_owned(), 118 )) 119 } 120 121 /// Construct a new CanonicalComposition instance for NFC using a particular data source. 122 #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition::new, FnInStruct)] 123 #[diplomat::rust_link( 124 icu::normalizer::properties::CanonicalCompositionBorrowed::new, 125 FnInStruct, 126 hidden 127 )] 128 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")] 129 #[cfg(feature = "buffer_provider")] create_with_provider( provider: &DataProvider, ) -> Result<Box<CanonicalComposition>, DataError>130 pub fn create_with_provider( 131 provider: &DataProvider, 132 ) -> Result<Box<CanonicalComposition>, DataError> { 133 Ok(Box::new(CanonicalComposition( 134 icu_normalizer::properties::CanonicalComposition::try_new_with_buffer_provider( 135 provider.get()?, 136 )?, 137 ))) 138 } 139 140 /// Performs canonical composition (including Hangul) on a pair of characters 141 /// or returns NUL if these characters don’t compose. Composition exclusions are taken into account. 142 #[diplomat::rust_link( 143 icu::normalizer::properties::CanonicalCompositionBorrowed::compose, 144 FnInStruct 145 )] compose(&self, starter: DiplomatChar, second: DiplomatChar) -> DiplomatChar146 pub fn compose(&self, starter: DiplomatChar, second: DiplomatChar) -> DiplomatChar { 147 match (char::from_u32(starter), char::from_u32(second)) { 148 (Some(starter), Some(second)) => self.0.as_borrowed().compose(starter, second), 149 _ => None, 150 } 151 .unwrap_or('\0') as DiplomatChar 152 } 153 } 154 155 /// The outcome of non-recursive canonical decomposition of a character. 156 /// `second` will be NUL when the decomposition expands to a single character 157 /// (which may or may not be the original one) 158 #[diplomat::rust_link(icu::normalizer::properties::Decomposed, Enum)] 159 #[diplomat::out] 160 pub struct Decomposed { 161 first: DiplomatChar, 162 second: DiplomatChar, 163 } 164 165 /// The raw (non-recursive) canonical decomposition operation. 166 /// 167 /// Callers should generally use DecomposingNormalizer unless they specifically need raw composition operations 168 #[diplomat::opaque] 169 #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition, Struct)] 170 #[diplomat::rust_link( 171 icu::normalizer::properties::CanonicalDecompositionBorrowed, 172 Struct, 173 hidden 174 )] 175 pub struct CanonicalDecomposition(pub icu_normalizer::properties::CanonicalDecomposition); 176 177 impl CanonicalDecomposition { 178 /// Construct a new CanonicalDecomposition instance for NFC using compiled data. 179 #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition::new, FnInStruct)] 180 #[diplomat::rust_link( 181 icu::normalizer::properties::CanonicalDecompositionBorrowed::new, 182 FnInStruct, 183 hidden 184 )] 185 #[diplomat::attr(auto, constructor)] 186 #[cfg(feature = "compiled_data")] create() -> Box<CanonicalDecomposition>187 pub fn create() -> Box<CanonicalDecomposition> { 188 Box::new(CanonicalDecomposition( 189 icu_normalizer::properties::CanonicalDecomposition::new().static_to_owned(), 190 )) 191 } 192 193 /// Construct a new CanonicalDecomposition instance for NFC using a particular data source. 194 #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition::new, FnInStruct)] 195 #[diplomat::rust_link( 196 icu::normalizer::properties::CanonicalDecompositionBorrowed::new, 197 FnInStruct, 198 hidden 199 )] 200 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")] 201 #[cfg(feature = "buffer_provider")] create_with_provider( provider: &DataProvider, ) -> Result<Box<CanonicalDecomposition>, DataError>202 pub fn create_with_provider( 203 provider: &DataProvider, 204 ) -> Result<Box<CanonicalDecomposition>, DataError> { 205 Ok(Box::new(CanonicalDecomposition( 206 icu_normalizer::properties::CanonicalDecomposition::try_new_with_buffer_provider( 207 provider.get()?, 208 )?, 209 ))) 210 } 211 212 /// Performs non-recursive canonical decomposition (including for Hangul). 213 #[diplomat::rust_link( 214 icu::normalizer::properties::CanonicalDecompositionBorrowed::decompose, 215 FnInStruct 216 )] decompose(&self, c: DiplomatChar) -> Decomposed217 pub fn decompose(&self, c: DiplomatChar) -> Decomposed { 218 match char::from_u32(c) { 219 Some(c) => match self.0.as_borrowed().decompose(c) { 220 icu_normalizer::properties::Decomposed::Default => Decomposed { 221 first: c as DiplomatChar, 222 second: '\0' as DiplomatChar, 223 }, 224 icu_normalizer::properties::Decomposed::Singleton(s) => Decomposed { 225 first: s as DiplomatChar, 226 second: '\0' as DiplomatChar, 227 }, 228 icu_normalizer::properties::Decomposed::Expansion(first, second) => { 229 Decomposed { 230 first: first as DiplomatChar, 231 second: second as DiplomatChar, 232 } 233 } 234 }, 235 _ => Decomposed { 236 first: c, 237 second: '\0' as DiplomatChar, 238 }, 239 } 240 } 241 } 242 } 243