| 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 |
pub mod ffi { |
| 7 |
use crate::{errors::ffi::ICU4XError, provider::ffi::ICU4XDataProvider}; |
| 8 |
use alloc::boxed::Box; |
| 9 |
use icu_normalizer::{ComposingNormalizer, DecomposingNormalizer}; |
| 10 |
|
| 11 |
#[diplomat::opaque] |
| 12 |
#[diplomat::rust_link(icu::normalizer::ComposingNormalizer, Struct)] |
| 13 |
pub struct ICU4XComposingNormalizer(pub ComposingNormalizer); |
| 14 |
|
| 15 |
impl ICU4XComposingNormalizer { |
| 16 |
/// Construct a new ICU4XComposingNormalizer instance for NFC |
| 17 |
#[diplomat::rust_link(icu::normalizer::ComposingNormalizer::new_nfc, FnInStruct)] |
| 18 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "nfc")] |
| 19 |
pub fn create_nfc( |
| 20 |
provider: &ICU4XDataProvider, |
| 21 |
) -> Result<Box<ICU4XComposingNormalizer>, ICU4XError> { |
| 22 |
Ok(Box::new(ICU4XComposingNormalizer(call_constructor!( |
| 23 |
ComposingNormalizer::new_nfc [r => Ok(r)], |
| 24 |
ComposingNormalizer::try_new_nfc_with_any_provider, |
| 25 |
ComposingNormalizer::try_new_nfc_with_buffer_provider, |
| 26 |
provider, |
| 27 |
)?))) |
| 28 |
} |
| 29 |
|
| 30 |
/// Construct a new ICU4XComposingNormalizer instance for NFKC |
| 31 |
#[diplomat::rust_link(icu::normalizer::ComposingNormalizer::new_nfkc, FnInStruct)] |
| 32 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "nfkc")] |
| 33 |
pub fn create_nfkc( |
| 34 |
provider: &ICU4XDataProvider, |
| 35 |
) -> Result<Box<ICU4XComposingNormalizer>, ICU4XError> { |
| 36 |
Ok(Box::new(ICU4XComposingNormalizer(call_constructor!( |
| 37 |
ComposingNormalizer::new_nfkc [r => Ok(r)], |
| 38 |
ComposingNormalizer::try_new_nfkc_with_any_provider, |
| 39 |
ComposingNormalizer::try_new_nfkc_with_buffer_provider, |
| 40 |
provider, |
| 41 |
)?))) |
| 42 |
} |
| 43 |
|
| 44 |
/// Normalize a string |
| 45 |
/// |
| 46 |
/// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according |
| 47 |
/// to the WHATWG Encoding Standard. |
| 48 |
#[diplomat::rust_link(icu::normalizer::ComposingNormalizer::normalize_utf8, FnInStruct)] |
| 49 |
#[diplomat::rust_link(icu::normalizer::ComposingNormalizer::normalize, FnInStruct, hidden)] |
| 50 |
#[diplomat::rust_link( |
| 51 |
icu::normalizer::ComposingNormalizer::normalize_to, |
| 52 |
FnInStruct, |
| 53 |
hidden |
| 54 |
)] |
| 55 |
#[diplomat::rust_link( |
| 56 |
icu::normalizer::ComposingNormalizer::normalize_utf8_to, |
| 57 |
FnInStruct, |
| 58 |
hidden |
| 59 |
)] |
| 60 |
pub fn normalize( |
| 61 |
&self, |
| 62 |
s: &DiplomatStr, |
| 63 |
write: &mut DiplomatWriteable, |
| 64 |
) -> Result<(), ICU4XError> { |
| 65 |
self.0.normalize_utf8_to(s, write)?; |
| 66 |
Ok(()) |
| 67 |
} |
| 68 |
|
| 69 |
/// Check if a string is normalized |
| 70 |
/// |
| 71 |
/// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according |
| 72 |
/// to the WHATWG Encoding Standard. |
| 73 |
#[diplomat::rust_link(icu::normalizer::ComposingNormalizer::is_normalized_utf8, FnInStruct)] |
| 74 |
#[diplomat::rust_link( |
| 75 |
icu::normalizer::ComposingNormalizer::is_normalized, |
| 76 |
FnInStruct, |
| 77 |
hidden |
| 78 |
)] |
| 79 |
pub fn is_normalized(&self, s: &DiplomatStr) -> bool { |
| 80 |
self.0.is_normalized_utf8(s) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
#[diplomat::opaque] |
| 85 |
#[diplomat::rust_link(icu::normalizer::DecomposingNormalizer, Struct)] |
| 86 |
pub struct ICU4XDecomposingNormalizer(pub DecomposingNormalizer); |
| 87 |
|
| 88 |
impl ICU4XDecomposingNormalizer { |
| 89 |
/// Construct a new ICU4XDecomposingNormalizer instance for NFC |
| 90 |
#[diplomat::rust_link(icu::normalizer::DecomposingNormalizer::new_nfd, FnInStruct)] |
| 91 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "nfd")] |
| 92 |
pub fn create_nfd( |
| 93 |
provider: &ICU4XDataProvider, |
| 94 |
) -> Result<Box<ICU4XDecomposingNormalizer>, ICU4XError> { |
| 95 |
Ok(Box::new(ICU4XDecomposingNormalizer(call_constructor!( |
| 96 |
DecomposingNormalizer::new_nfd [r => Ok(r)], |
| 97 |
DecomposingNormalizer::try_new_nfd_with_any_provider, |
| 98 |
DecomposingNormalizer::try_new_nfd_with_buffer_provider, |
| 99 |
provider, |
| 100 |
)?))) |
| 101 |
} |
| 102 |
|
| 103 |
/// Construct a new ICU4XDecomposingNormalizer instance for NFKC |
| 104 |
#[diplomat::rust_link(icu::normalizer::DecomposingNormalizer::new_nfkd, FnInStruct)] |
| 105 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "nfkd")] |
| 106 |
pub fn create_nfkd( |
| 107 |
provider: &ICU4XDataProvider, |
| 108 |
) -> Result<Box<ICU4XDecomposingNormalizer>, ICU4XError> { |
| 109 |
Ok(Box::new(ICU4XDecomposingNormalizer(call_constructor!( |
| 110 |
DecomposingNormalizer::new_nfkd [r => Ok(r)], |
| 111 |
DecomposingNormalizer::try_new_nfkd_with_any_provider, |
| 112 |
DecomposingNormalizer::try_new_nfkd_with_buffer_provider, |
| 113 |
provider, |
| 114 |
)?))) |
| 115 |
} |
| 116 |
|
| 117 |
/// Normalize a string |
| 118 |
/// |
| 119 |
/// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according |
| 120 |
/// to the WHATWG Encoding Standard. |
| 121 |
#[diplomat::rust_link(icu::normalizer::DecomposingNormalizer::normalize_utf8, FnInStruct)] |
| 122 |
#[diplomat::rust_link( |
| 123 |
icu::normalizer::DecomposingNormalizer::normalize, |
| 124 |
FnInStruct, |
| 125 |
hidden |
| 126 |
)] |
| 127 |
#[diplomat::rust_link( |
| 128 |
icu::normalizer::DecomposingNormalizer::normalize_to, |
| 129 |
FnInStruct, |
| 130 |
hidden |
| 131 |
)] |
| 132 |
#[diplomat::rust_link( |
| 133 |
icu::normalizer::DecomposingNormalizer::normalize_utf8_to, |
| 134 |
FnInStruct, |
| 135 |
hidden |
| 136 |
)] |
| 137 |
pub fn normalize( |
| 138 |
&self, |
| 139 |
s: &DiplomatStr, |
| 140 |
write: &mut DiplomatWriteable, |
| 141 |
) -> Result<(), ICU4XError> { |
| 142 |
self.0.normalize_utf8_to(s, write)?; |
| 143 |
Ok(()) |
| 144 |
} |
| 145 |
|
| 146 |
/// Check if a string is normalized |
| 147 |
/// |
| 148 |
/// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according |
| 149 |
/// to the WHATWG Encoding Standard. |
| 150 |
#[diplomat::rust_link( |
| 151 |
icu::normalizer::DecomposingNormalizer::is_normalized_utf8, |
| 152 |
FnInStruct |
| 153 |
)] |
| 154 |
#[diplomat::rust_link( |
| 155 |
icu::normalizer::DecomposingNormalizer::is_normalized, |
| 156 |
FnInStruct, |
| 157 |
hidden |
| 158 |
)] |
| 159 |
pub fn is_normalized(&self, s: &DiplomatStr) -> bool { |
| 160 |
self.0.is_normalized_utf8(s) |
| 161 |
} |
| 162 |
} |
| 163 |
} |