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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// 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 ).

use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
use displaydoc::Display;
use icu_locale_core::Locale;
use icu_provider::DataError;

/// Trait for providing person name data.
pub trait PersonName {
    /// Returns the name locale of person name.
    fn name_locale(&self) -> Option<&Locale>;

    /// Returns the preferred order of person name.
    fn preferred_order(&self) -> Option<&PreferredOrder>;

    /// Returns the value of the given field name, it *must* match the name field requested.
    /// The string should be in NFC.
    fn get(&self, field: &NameField) -> &str;

    /// Returns all available name field.
    fn available_name_fields(&self) -> Vec<&NameField>;

    /// Returns true if the provided field name is available.
    fn has_name_field_kind(&self, lookup_name_field: &NameFieldKind) -> bool;

    /// Returns true if person have the name field matching the type and modifier.
    fn has_name_field(&self, lookup_name_field: &NameField) -> bool;
}

///
/// Error handling for the person name formatter.
#[derive(Clone, Eq, PartialEq, Debug, Display)]
pub enum PersonNamesFormatterError {
    #[displaydoc("{0}")]
    ParseError(String),
    #[displaydoc("Invalid person name")]
    InvalidPersonName,
    #[displaydoc("Invalid person name")]
    InvalidLocale,
    #[displaydoc("Invalid CLDR data")]
    InvalidCldrData,
    #[displaydoc("{0}")]
    Data(DataError),
    #[displaydoc("{0}")]
    Pattern(icu_pattern::Error),
}

impl From<DataError> for PersonNamesFormatterError {
    fn from(e: DataError) -> Self {
        PersonNamesFormatterError::Data(e)
    }
}

impl From<icu_pattern::Error> for PersonNamesFormatterError {
    fn from(e: icu_pattern::Error) -> Self {
        PersonNamesFormatterError::Pattern(e)
    }
}

/// Field Modifiers.
///
/// <https://www.unicode.org/reports/tr35/tr35-personNames.html#modifiers>
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub(crate) enum FieldModifier {
    None,
    Informal,
    Prefix,
    Core,
    AllCaps,
    InitialCap,
    Initial,
    Monogram,
}

impl FieldModifier {
    pub(crate) fn bit_value(&self) -> u32 {
        match &self {
            FieldModifier::None => 0,
            FieldModifier::Informal => 1 << 0,
            FieldModifier::Prefix => 1 << 1,
            FieldModifier::Core => 1 << 2,
            FieldModifier::AllCaps => 1 << 3,
            FieldModifier::InitialCap => 1 << 4,
            FieldModifier::Initial => 1 << 5,
            FieldModifier::Monogram => 1 << 6,
        }
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum FieldCapsStyle {
    Auto,
    AllCaps,
    InitialCap,
}

impl From<FieldCapsStyle> for FieldModifier {
    fn from(value: FieldCapsStyle) -> Self {
        match value {
            FieldCapsStyle::Auto => FieldModifier::None,
            FieldCapsStyle::AllCaps => FieldModifier::AllCaps,
            FieldCapsStyle::InitialCap => FieldModifier::InitialCap,
        }
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum FieldPart {
    Auto,
    Core,
    Prefix,
}

impl From<FieldPart> for FieldModifier {
    fn from(value: FieldPart) -> Self {
        match value {
            FieldPart::Auto => FieldModifier::None,
            FieldPart::Core => FieldModifier::Core,
            FieldPart::Prefix => FieldModifier::Prefix,
        }
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum FieldLength {
    Auto,
    Initial,
    Monogram,
}

impl From<FieldLength> for FieldModifier {
    fn from(value: FieldLength) -> Self {
        match value {
            FieldLength::Auto => FieldModifier::None,
            FieldLength::Initial => FieldModifier::Initial,
            FieldLength::Monogram => FieldModifier::Monogram,
        }
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum FieldFormality {
    Auto,
    Informal,
}

impl From<FieldFormality> for FieldModifier {
    fn from(value: FieldFormality) -> Self {
        match value {
            FieldFormality::Auto => FieldModifier::None,
            FieldFormality::Informal => FieldModifier::Informal,
        }
    }
}

/// Field Modifiers Set. (must be the same as FieldModifier repr)
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Copy)]
pub struct FieldModifierSet {
    pub(crate) value: u32,
}

impl FieldModifierSet {
    /// Return true of the field modifier is set. (None will always return true.)
    pub(crate) fn has_field(&self, modifier: FieldModifier) -> bool {
        self.value & modifier.bit_value() == modifier.bit_value()
    }

    /// Returns a copy of the field modifier set with the part changed.
    pub(crate) fn with_part(&self, part: FieldPart) -> FieldModifierSet {
        FieldModifierSet {
            value: (self.value
                & (u32::MAX
                    ^ (FieldModifier::Core.bit_value() | FieldModifier::Prefix.bit_value())))
                | FieldModifier::from(part).bit_value(),
        }
    }
    /// Returns a copy of the field modifier set with the length changed.
    pub(crate) fn with_length(&self, length: FieldLength) -> FieldModifierSet {
        FieldModifierSet {
            value: (self.value
                & (u32::MAX
                    ^ (FieldModifier::Monogram.bit_value() | FieldModifier::Initial.bit_value())))
                | FieldModifier::from(length).bit_value(),
        }
    }

    pub fn formality(formality: FieldFormality) -> Self {
        Self::new(
            FieldCapsStyle::Auto,
            FieldPart::Auto,
            FieldLength::Auto,
            formality,
        )
    }
    pub fn style(style: FieldCapsStyle) -> Self {
        Self::new(
            style,
            FieldPart::Auto,
            FieldLength::Auto,
            FieldFormality::Auto,
        )
    }
    pub fn part(part: FieldPart) -> Self {
        Self::new(
            FieldCapsStyle::Auto,
            part,
            FieldLength::Auto,
            FieldFormality::Auto,
        )
    }
    pub fn length(length: FieldLength) -> Self {
        Self::new(
            FieldCapsStyle::Auto,
            FieldPart::Auto,
            length,
            FieldFormality::Auto,
        )
    }

    pub fn new(
        style: FieldCapsStyle,
        part: FieldPart,
        length: FieldLength,
        formality: FieldFormality,
    ) -> Self {
        FieldModifierSet {
            value: FieldModifier::from(style).bit_value()
                | FieldModifier::from(part).bit_value()
                | FieldModifier::from(length).bit_value()
                | FieldModifier::from(formality).bit_value(),
        }
    }
}

impl Default for FieldModifierSet {
    fn default() -> Self {
        Self::new(
            FieldCapsStyle::Auto,
            FieldPart::Auto,
            FieldLength::Auto,
            FieldFormality::Auto,
        )
    }
}

impl fmt::Debug for FieldModifierSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> core::fmt::Result {
        let mut debug = f.debug_struct("FieldModifierSet");
        debug.field("core", &self.has_field(FieldModifier::Core));
        debug.field("informal", &self.has_field(FieldModifier::Informal));
        debug.field("monogram", &self.has_field(FieldModifier::Monogram));
        debug.field("initial", &self.has_field(FieldModifier::Initial));
        debug.field("prefix", &self.has_field(FieldModifier::Prefix));
        debug.field("all_caps", &self.has_field(FieldModifier::AllCaps));
        debug.field("initial_cap", &self.has_field(FieldModifier::InitialCap));
        debug.finish()
    }
}

/// Name Fields defined by Unicode specifications.
///
/// <https://www.unicode.org/reports/tr35/tr35-personNames.html#fields>
#[derive(Eq, Ord, PartialOrd, PartialEq, Hash, Debug, Clone, Copy)]
pub struct NameField {
    pub kind: NameFieldKind,
    pub modifier: FieldModifierSet,
}

#[derive(Eq, Ord, PartialOrd, PartialEq, Hash, Debug, Clone, Copy)]
pub enum NameFieldKind {
    Title,
    Given,
    Given2,
    Surname,
    Surname2,
    Generation,
    Credentials,
}

/// An enum to specify the preferred field order for the name.
///
/// <https://www.unicode.org/reports/tr35/tr35-personNames.html#person-name-object>
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum PreferredOrder {
    Default,
    GivenFirst,
    SurnameFirst,
}

/// Formatting Order
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum FormattingOrder {
    GivenFirst,
    SurnameFirst,
    Sorting,
}

/// Formatting Length
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum FormattingLength {
    Short,
    Medium,
    Long,
}

/// Formatting Usage
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum FormattingUsage {
    Addressing,
    Referring,
    Monogram,
}

/// Formatting Formality
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum FormattingFormality {
    Formal,
    Informal,
}

/// Person name formatter options.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct PersonNamesFormatterOptions {
    /// TODO: the target locale should be maximized when passed into the formatter.
    pub target_locale: Locale,
    pub order: FormattingOrder,
    pub length: FormattingLength,
    pub usage: FormattingUsage,
    pub formality: FormattingFormality,
}

impl PersonNamesFormatterOptions {
    // TODO: Remove this function. It depends on compiled_data for the LocaleExpander.
    #[cfg(feature = "compiled_data")]
    pub fn new(
        target_locale: Locale,
        order: FormattingOrder,
        length: FormattingLength,
        usage: FormattingUsage,
        formality: FormattingFormality,
    ) -> Self {
        let lc = icu_locale::LocaleExpander::new();
        let mut final_locale = target_locale.clone();
        lc.maximize(&mut final_locale.id);
        Self {
            target_locale: final_locale,
            order,
            length,
            usage,
            formality,
        }
    }
}