use crate::provider::bidi_data::{
BidiAuxiliaryPropertiesV1, BidiAuxiliaryPropertiesV1Marker, CheckedBidiPairedBracketType,
};
use crate::PropertiesError;
use icu_provider::prelude::*;
#[derive(Debug)]
pub struct BidiAuxiliaryProperties {
data: DataPayload<BidiAuxiliaryPropertiesV1Marker>,
}
impl BidiAuxiliaryProperties {
#[inline]
pub fn as_borrowed(&self) -> BidiAuxiliaryPropertiesBorrowed<'_> {
BidiAuxiliaryPropertiesBorrowed {
data: self.data.get(),
}
}
pub fn from_data(data: DataPayload<BidiAuxiliaryPropertiesV1Marker>) -> Self {
Self { data }
}
}
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct BidiMirroringProperties {
pub mirroring_glyph: Option<char>,
pub mirrored: bool,
}
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum BidiPairingProperties {
Open(char),
Close(char),
None,
}
#[derive(Debug)]
pub struct BidiAuxiliaryPropertiesBorrowed<'a> {
data: &'a BidiAuxiliaryPropertiesV1<'a>,
}
impl<'a> BidiAuxiliaryPropertiesBorrowed<'a> {
fn convert_mirroring_glyph_data(trie_data_char: char) -> Option<char> {
if trie_data_char as u32 == 0 {
None
} else {
Some(trie_data_char)
}
}
pub fn get32_mirroring_props(&self, code_point: u32) -> BidiMirroringProperties {
let bidi_aux_props = self.data.trie.get32(code_point);
let mirroring_glyph_opt =
Self::convert_mirroring_glyph_data(bidi_aux_props.mirroring_glyph);
BidiMirroringProperties {
mirroring_glyph: mirroring_glyph_opt,
mirrored: bidi_aux_props.mirrored,
}
}
pub fn get32_pairing_props(&self, code_point: u32) -> BidiPairingProperties {
let bidi_aux_props = self.data.trie.get32(code_point);
let mirroring_glyph = bidi_aux_props.mirroring_glyph;
let paired_bracket_type = bidi_aux_props.paired_bracket_type;
match paired_bracket_type {
CheckedBidiPairedBracketType::Open => BidiPairingProperties::Open(mirroring_glyph),
CheckedBidiPairedBracketType::Close => BidiPairingProperties::Close(mirroring_glyph),
_ => BidiPairingProperties::None,
}
}
}
impl BidiAuxiliaryPropertiesBorrowed<'static> {
pub const fn static_to_owned(self) -> BidiAuxiliaryProperties {
BidiAuxiliaryProperties {
data: DataPayload::from_static_ref(self.data),
}
}
}
#[cfg(feature = "compiled_data")]
pub const fn bidi_auxiliary_properties() -> BidiAuxiliaryPropertiesBorrowed<'static> {
BidiAuxiliaryPropertiesBorrowed {
data: crate::provider::Baked::SINGLETON_PROPS_BIDIAUXILIARYPROPS_V1,
}
}
icu_provider::gen_any_buffer_data_constructors!(
locale: skip,
options: skip,
result: Result<BidiAuxiliaryProperties, PropertiesError>,
#[cfg(skip)]
functions: [
bidi_auxiliary_properties,
load_bidi_auxiliary_properties_with_any_provider,
load_bidi_auxiliary_properties_with_buffer_provider,
load_bidi_auxiliary_properties_unstable,
]
);
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, bidi_auxiliary_properties)]
pub fn load_bidi_auxiliary_properties_unstable(
provider: &(impl DataProvider<BidiAuxiliaryPropertiesV1Marker> + ?Sized),
) -> Result<BidiAuxiliaryProperties, PropertiesError> {
Ok(provider
.load(Default::default())
.and_then(DataResponse::take_payload)
.map(BidiAuxiliaryProperties::from_data)?)
}