1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

// Provider structs must be stable
#![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]
//! Data provider struct definitions for this ICU4X component.
//!
//! Read more about data providers: [`icu_provider`]

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")]
/// Baked data
///
/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only
/// guaranteed to match with this version's `*_unstable` providers. Use with caution.
/// </div>
pub use crate::provider::Baked;

/// This is the equivalent of
/// <https://www.unicode.org/reports/tr35/tr35-personNames.html#personnames-element>
///
/// There are some liberties taken from the DTD definition,
/// as we enforce uniqueness on some tags where the documentation doesn't specify any of these
/// constraints.
///
/// e.g. : initialPattern has no upper bound, DTD allows for the element to be specified any number
/// of times, while in this implementation we are restraining it to the 2 documented types
/// (`initial`, `initialSequence`).
#[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> {
    /// <nameOrderLocales order="surnameFirst">ko vi yue zh</nameOrderLocales>
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub surname_first_locales: VarZeroVec<'data, str>,

    /// <nameOrderLocales order="givenFirst">und en</nameOrderLocales>
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub given_first_locales: VarZeroVec<'data, str>,

    /// foreignSpaceReplacement element.
    #[cfg_attr(feature = "serde", serde(borrow, deserialize_with = "option_of_cow"))]
    pub foreign_space_replacement: Option<Cow<'data, str>>,

    /// Equivalent of initialPattern tag + initial
    /// ```xml
    /// <initialPattern type="initial">{0}.</initialPattern>
    /// ```
    #[cfg_attr(feature = "serde", serde(borrow, deserialize_with = "option_of_cow"))]
    pub initial_pattern: Option<Cow<'data, str>>,

    /// Equivalent of initialPattern tag + initialSequence
    /// ```xml
    /// <initialPattern type="initialSequence">{0} {1}</initialPattern>
    /// ```
    #[cfg_attr(feature = "serde", serde(borrow, deserialize_with = "option_of_cow"))]
    pub initial_pattern_sequence: Option<Cow<'data, str>>,

    /// Equivalent of PersonNames
    /// ```xml
    /// <personName>...</personName>
    /// ```
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub person_names_patterns: VarZeroVec<'data, PersonNamesFormattingDataVarULE>,
}

/// Person Name Attributes.
/// {order=givenFirst, length=long, usage=referring, formality=formal}
pub enum PersonNamesFormattingAttributes {
    GivenFirst,
    SurnameFirst,
    Sorting,
    // Length
    Short,
    Medium,
    Long,
    // Usage
    Addressing,
    Referring,
    Monogram,
    // Formality
    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;

/// PersonName Formatting data.
///
/// <https://www.unicode.org/reports/tr35/tr35-personNames.html#personname-element>
#[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> {
    /// Attributes
    /// <https://www.unicode.org/reports/tr35/tr35-personNames.html#personname-element>
    pub attributes: PersonNamesFormattingAttributesMask,
    /// <https://www.unicode.org/reports/tr35/tr35-personNames.html#namepattern-syntax>
    #[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()
    }
}