#![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]
use alloc::borrow::Cow;
use core::fmt::{Debug, Formatter};
use icu_provider::prelude::*;
#[cfg(feature = "serde")]
use icu_provider::serde_borrow_de_utils::option_of_cow;
use zerovec::VarZeroVec;
use crate::personnames::api::FormattingFormality;
use crate::personnames::api::FormattingLength;
use crate::personnames::api::FormattingOrder;
use crate::personnames::api::FormattingUsage;
#[cfg(feature = "compiled_data")]
pub use crate::provider::Baked;
#[icu_provider::data_struct(PersonNamesFormatV1Marker = "personnames/personnames@1")]
#[derive(PartialEq, Clone)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_experimental::personnames::provider))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct PersonNamesFormatV1<'data> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub surname_first_locales: VarZeroVec<'data, str>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub given_first_locales: VarZeroVec<'data, str>,
#[cfg_attr(feature = "serde", serde(borrow, deserialize_with = "option_of_cow"))]
pub foreign_space_replacement: Option<Cow<'data, str>>,
#[cfg_attr(feature = "serde", serde(borrow, deserialize_with = "option_of_cow"))]
pub initial_pattern: Option<Cow<'data, str>>,
#[cfg_attr(feature = "serde", serde(borrow, deserialize_with = "option_of_cow"))]
pub initial_pattern_sequence: Option<Cow<'data, str>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub person_names_patterns: VarZeroVec<'data, PersonNamesFormattingDataVarULE>,
}
pub enum PersonNamesFormattingAttributes {
GivenFirst,
SurnameFirst,
Sorting,
Short,
Medium,
Long,
Addressing,
Referring,
Monogram,
Formal,
Informal,
}
impl PersonNamesFormattingAttributes {
pub fn bit_value(&self) -> PersonNamesFormattingAttributesMask {
match self {
PersonNamesFormattingAttributes::GivenFirst => 1 << 0,
PersonNamesFormattingAttributes::SurnameFirst => 1 << 1,
PersonNamesFormattingAttributes::Sorting => 1 << 2,
PersonNamesFormattingAttributes::Short => 1 << 3,
PersonNamesFormattingAttributes::Medium => 1 << 4,
PersonNamesFormattingAttributes::Long => 1 << 5,
PersonNamesFormattingAttributes::Addressing => 1 << 6,
PersonNamesFormattingAttributes::Referring => 1 << 7,
PersonNamesFormattingAttributes::Monogram => 1 << 8,
PersonNamesFormattingAttributes::Formal => 1 << 9,
PersonNamesFormattingAttributes::Informal => 1 << 10,
}
}
}
impl From<FormattingOrder> for PersonNamesFormattingAttributes {
fn from(value: FormattingOrder) -> Self {
match value {
FormattingOrder::GivenFirst => PersonNamesFormattingAttributes::GivenFirst,
FormattingOrder::SurnameFirst => PersonNamesFormattingAttributes::SurnameFirst,
FormattingOrder::Sorting => PersonNamesFormattingAttributes::Sorting,
}
}
}
impl From<FormattingFormality> for PersonNamesFormattingAttributes {
fn from(value: FormattingFormality) -> Self {
match value {
FormattingFormality::Formal => PersonNamesFormattingAttributes::Formal,
FormattingFormality::Informal => PersonNamesFormattingAttributes::Informal,
}
}
}
impl From<FormattingLength> for PersonNamesFormattingAttributes {
fn from(value: FormattingLength) -> Self {
match value {
FormattingLength::Short => PersonNamesFormattingAttributes::Short,
FormattingLength::Medium => PersonNamesFormattingAttributes::Medium,
FormattingLength::Long => PersonNamesFormattingAttributes::Long,
}
}
}
impl From<FormattingUsage> for PersonNamesFormattingAttributes {
fn from(value: FormattingUsage) -> Self {
match value {
FormattingUsage::Addressing => PersonNamesFormattingAttributes::Addressing,
FormattingUsage::Referring => PersonNamesFormattingAttributes::Referring,
FormattingUsage::Monogram => PersonNamesFormattingAttributes::Monogram,
}
}
}
pub type PersonNamesFormattingAttributesMask = u32;
#[zerovec::make_varule(PersonNamesFormattingDataVarULE)]
#[zerovec::skip_derive(ZeroMapKV, Ord)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_experimental::personnames::provider))]
#[cfg_attr(feature = "datagen", zerovec::derive(Serialize))]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize),
zerovec::derive(Deserialize)
)]
pub struct PersonNamesFormattingData<'data> {
pub attributes: PersonNamesFormattingAttributesMask,
#[cfg_attr(feature = "serde", serde(borrow))]
pub patterns: VarZeroVec<'data, str>,
}
impl Debug for PersonNamesFormattingData<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let mut debug = f.debug_struct("PersonNamesFormattingData");
debug.field(
"given_first",
&(self.attributes & PersonNamesFormattingAttributes::GivenFirst.bit_value()
== PersonNamesFormattingAttributes::GivenFirst.bit_value()),
);
debug.field(
"surname_first",
&(self.attributes & PersonNamesFormattingAttributes::SurnameFirst.bit_value()
== PersonNamesFormattingAttributes::SurnameFirst.bit_value()),
);
debug.field(
"sorting",
&(self.attributes & PersonNamesFormattingAttributes::Sorting.bit_value()
== PersonNamesFormattingAttributes::Sorting.bit_value()),
);
debug.field(
"short",
&(self.attributes & PersonNamesFormattingAttributes::Short.bit_value()
== PersonNamesFormattingAttributes::Short.bit_value()),
);
debug.field(
"medium",
&(self.attributes & PersonNamesFormattingAttributes::Medium.bit_value()
== PersonNamesFormattingAttributes::Medium.bit_value()),
);
debug.field(
"long",
&(self.attributes & PersonNamesFormattingAttributes::Long.bit_value()
== PersonNamesFormattingAttributes::Long.bit_value()),
);
debug.field(
"addressing",
&(self.attributes & PersonNamesFormattingAttributes::Addressing.bit_value()
== PersonNamesFormattingAttributes::Addressing.bit_value()),
);
debug.field(
"referring",
&(self.attributes & PersonNamesFormattingAttributes::Referring.bit_value()
== PersonNamesFormattingAttributes::Referring.bit_value()),
);
debug.field(
"monogram",
&(self.attributes & PersonNamesFormattingAttributes::Monogram.bit_value()
== PersonNamesFormattingAttributes::Monogram.bit_value()),
);
debug.field(
"formal",
&(self.attributes & PersonNamesFormattingAttributes::Formal.bit_value()
== PersonNamesFormattingAttributes::Formal.bit_value()),
);
debug.field(
"informal",
&(self.attributes & PersonNamesFormattingAttributes::Informal.bit_value()
== PersonNamesFormattingAttributes::Informal.bit_value()),
);
debug.finish()
}
}