#[diplomat::bridge]
pub mod ffi {
use crate::{errors::ffi::ICU4XError, provider::ffi::ICU4XDataProvider};
use alloc::boxed::Box;
use icu_normalizer::properties::{
CanonicalCombiningClassMap, CanonicalComposition, CanonicalDecomposition, Decomposed,
};
#[diplomat::opaque]
#[diplomat::rust_link(icu::normalizer::properties::CanonicalCombiningClassMap, Struct)]
pub struct ICU4XCanonicalCombiningClassMap(pub CanonicalCombiningClassMap);
impl ICU4XCanonicalCombiningClassMap {
#[diplomat::rust_link(
icu::normalizer::properties::CanonicalCombiningClassMap::new,
FnInStruct
)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
pub fn create(
provider: &ICU4XDataProvider,
) -> Result<Box<ICU4XCanonicalCombiningClassMap>, ICU4XError> {
Ok(Box::new(ICU4XCanonicalCombiningClassMap(
call_constructor!(
CanonicalCombiningClassMap::new [r => Ok(r)],
CanonicalCombiningClassMap::try_new_with_any_provider,
CanonicalCombiningClassMap::try_new_with_buffer_provider,
provider
)?,
)))
}
#[diplomat::rust_link(
icu::normalizer::properties::CanonicalCombiningClassMap::get,
FnInStruct
)]
#[diplomat::rust_link(
icu::properties::properties::CanonicalCombiningClass,
Struct,
compact
)]
#[diplomat::attr(supports = indexing, indexer)]
pub fn get(&self, ch: DiplomatChar) -> u8 {
self.0.get32(ch).0
}
#[diplomat::rust_link(
icu::normalizer::properties::CanonicalCombiningClassMap::get32,
FnInStruct
)]
#[diplomat::rust_link(
icu::properties::properties::CanonicalCombiningClass,
Struct,
compact
)]
#[diplomat::attr(dart, disable)]
pub fn get32(&self, ch: u32) -> u8 {
self.0.get32(ch).0
}
}
#[diplomat::opaque]
#[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition, Struct)]
pub struct ICU4XCanonicalComposition(pub CanonicalComposition);
impl ICU4XCanonicalComposition {
#[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition::new, FnInStruct)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
pub fn create(
provider: &ICU4XDataProvider,
) -> Result<Box<ICU4XCanonicalComposition>, ICU4XError> {
Ok(Box::new(ICU4XCanonicalComposition(call_constructor!(
CanonicalComposition::new [r => Ok(r)],
CanonicalComposition::try_new_with_any_provider,
CanonicalComposition::try_new_with_buffer_provider,
provider,
)?)))
}
#[diplomat::rust_link(
icu::normalizer::properties::CanonicalComposition::compose,
FnInStruct
)]
pub fn compose(&self, starter: DiplomatChar, second: DiplomatChar) -> DiplomatChar {
match (char::from_u32(starter), char::from_u32(second)) {
(Some(starter), Some(second)) => self.0.compose(starter, second),
_ => None,
}
.unwrap_or('\0') as DiplomatChar
}
}
#[diplomat::rust_link(icu::normalizer::properties::Decomposed, Enum)]
#[diplomat::out]
pub struct ICU4XDecomposed {
first: DiplomatChar,
second: DiplomatChar,
}
#[diplomat::opaque]
#[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition, Struct)]
pub struct ICU4XCanonicalDecomposition(pub CanonicalDecomposition);
impl ICU4XCanonicalDecomposition {
#[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition::new, FnInStruct)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
pub fn create(
provider: &ICU4XDataProvider,
) -> Result<Box<ICU4XCanonicalDecomposition>, ICU4XError> {
Ok(Box::new(ICU4XCanonicalDecomposition(call_constructor!(
CanonicalDecomposition::new [r => Ok(r)],
CanonicalDecomposition::try_new_with_any_provider,
CanonicalDecomposition::try_new_with_buffer_provider,
provider,
)?)))
}
#[diplomat::rust_link(
icu::normalizer::properties::CanonicalDecomposition::decompose,
FnInStruct
)]
pub fn decompose(&self, c: DiplomatChar) -> ICU4XDecomposed {
match char::from_u32(c) {
Some(c) => match self.0.decompose(c) {
Decomposed::Default => ICU4XDecomposed {
first: c as DiplomatChar,
second: '\0' as DiplomatChar,
},
Decomposed::Singleton(s) => ICU4XDecomposed {
first: s as DiplomatChar,
second: '\0' as DiplomatChar,
},
Decomposed::Expansion(first, second) => ICU4XDecomposed {
first: first as DiplomatChar,
second: second as DiplomatChar,
},
},
_ => ICU4XDecomposed {
first: c,
second: '\0' as DiplomatChar,
},
}
}
}
}