icu_provider_source/personnames/
person_names_format_data_providers.rs

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
// 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 core::convert::TryFrom;
use std::borrow::Cow;
use std::collections::HashSet;

use icu::experimental::personnames::provider::*;
use icu_provider::prelude::*;
use zerovec::VarZeroVec;

use crate::cldr_serde::personnames::person_name_format_json_struct::Resource;
use crate::IterableDataProviderCached;

impl DataProvider<PersonNamesFormatV1Marker> for crate::SourceDataProvider {
    fn load(&self, req: DataRequest) -> Result<DataResponse<PersonNamesFormatV1Marker>, DataError> {
        let data: &Resource = self
            .cldr()?
            .personnames()
            .read_and_parse(req.id.locale, "personNames.json")?;

        Ok(DataResponse {
            metadata: Default::default(),
            payload: DataPayload::from_owned(PersonNamesFormatV1::try_from(data).map_err(|e| {
                DataError::custom("data for PersonNamesFormattingDefinition")
                    .with_display_context(&e)
            })?),
        })
    }
}

impl IterableDataProviderCached<PersonNamesFormatV1Marker> for crate::SourceDataProvider {
    fn iter_ids_cached(&self) -> Result<HashSet<DataIdentifierCow<'static>>, DataError> {
        Ok(self
            .cldr()?
            .personnames()
            .list_locales()?
            .filter(|locale| {
                // The directory might exist without personNames.json
                self.cldr()
                    .unwrap()
                    .personnames()
                    .file_exists(locale, "personNames.json")
                    .unwrap_or_default()
            })
            .map(DataIdentifierCow::from_locale)
            .collect())
    }
}

fn json_field_to_formatting_attribute(value: &str) -> Result<u32, DataError> {
    match value {
        "surnameFirst" => Ok(PersonNamesFormattingAttributes::SurnameFirst.bit_value()),
        "givenFirst" => Ok(PersonNamesFormattingAttributes::GivenFirst.bit_value()),
        "sorting" => Ok(PersonNamesFormattingAttributes::Sorting.bit_value()),

        "long" => Ok(PersonNamesFormattingAttributes::Long.bit_value()),
        "medium" => Ok(PersonNamesFormattingAttributes::Medium.bit_value()),
        "short" => Ok(PersonNamesFormattingAttributes::Short.bit_value()),

        "addressing" => Ok(PersonNamesFormattingAttributes::Addressing.bit_value()),
        "referring" => Ok(PersonNamesFormattingAttributes::Referring.bit_value()),
        "monogram" => Ok(PersonNamesFormattingAttributes::Monogram.bit_value()),

        v if v.starts_with("formal") => Ok(PersonNamesFormattingAttributes::Formal.bit_value()),
        v if v.starts_with("informal") => Ok(PersonNamesFormattingAttributes::Informal.bit_value()),
        _ => Err(DataError::custom("invalid json value")),
    }
}

fn to_mask(ordering: &str, size: &str, referring: &str, formality: &str) -> Result<u32, DataError> {
    let o = json_field_to_formatting_attribute(ordering)?;
    let s = json_field_to_formatting_attribute(size)?;
    let r = json_field_to_formatting_attribute(referring)?;
    let f = json_field_to_formatting_attribute(formality)?;
    Ok(o | s | r | f)
}

///
/// Transform the JSON Resource into a single PersonNamesFormattingDefinitionV1
///
/// The JSON Structure is expected to be perfect and all combination should be provided.
impl TryFrom<&'_ Resource> for PersonNamesFormatV1<'_> {
    type Error = DataError;
    fn try_from(other: &'_ Resource) -> Result<Self, Self::Error> {
        let person_names = &other.main.value.person_names;
        let given_first_locales = VarZeroVec::<str>::from(&person_names.given_first);
        let surname_first_locales = VarZeroVec::<str>::from(&person_names.surname_first);
        let foreign_space_replacement = Some(Cow::Owned(
            person_names.foreign_space_replacement.to_string(),
        ));
        let initial_pattern = Some(Cow::Owned(person_names.initial.to_string()));
        let initial_pattern_sequence = Some(Cow::Owned(person_names.initial_sequence.to_string()));
        let collected_patterns: Result<Vec<_>, _> = person_names
            .formatting_pattern
            .0
            .iter()
            .flat_map(|(ordering, sized_formatting)| {
                sized_formatting
                    .0
                    .iter()
                    .flat_map(|(size, referring_formatting)| {
                        referring_formatting.0.iter().flat_map(
                            |(referring, formality_formatting)| {
                                let mut formal_pattern = vec![];
                                let mut informal_pattern = vec![];
                                for (formality, pattern) in formality_formatting.0.iter() {
                                    if json_field_to_formatting_attribute(formality).unwrap()
                                        == PersonNamesFormattingAttributes::Formal.bit_value()
                                    {
                                        formal_pattern.push(pattern.as_str());
                                    } else {
                                        informal_pattern.push(pattern.as_str());
                                    }
                                }
                                [
                                    to_mask(ordering, size, referring, "formal").map(|mask| {
                                        PersonNamesFormattingData {
                                            attributes: mask,
                                            patterns: VarZeroVec::<str>::from(&formal_pattern),
                                        }
                                    }),
                                    to_mask(ordering, size, referring, "informal").map(|mask| {
                                        PersonNamesFormattingData {
                                            attributes: mask,
                                            patterns: VarZeroVec::<str>::from(&informal_pattern),
                                        }
                                    }),
                                ]
                            },
                        )
                    })
            })
            .collect();

        let person_names_patterns = collected_patterns.map(|value| VarZeroVec::from(&value))?;

        Ok(Self {
            surname_first_locales,
            given_first_locales,
            foreign_space_replacement,
            initial_pattern,
            initial_pattern_sequence,
            person_names_patterns,
        })
    }
}

#[cfg(test)]
mod tests {
    use icu::locale::langid;
    use zerofrom::ZeroFrom;

    use super::*;

    #[test]
    fn test_initial_pattern() -> Result<(), DataError> {
        let provider = crate::SourceDataProvider::new_testing();

        let data_payload: DataPayload<PersonNamesFormatV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(&langid!("en-001").into()),
                ..Default::default()
            })?
            .payload;

        let real_data: &PersonNamesFormatV1 = data_payload.get();

        assert_eq!(
            real_data.initial_pattern.as_ref().unwrap(),
            "{0}.",
            "we are testing with {} and {}",
            real_data.initial_pattern.as_ref().unwrap(),
            "{0}."
        );
        Ok(())
    }

    #[test]
    fn test_have_pattern() -> Result<(), DataError> {
        let provider = crate::SourceDataProvider::new_testing();

        let data_payload: DataPayload<PersonNamesFormatV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(&langid!("en-001").into()),
                ..Default::default()
            })?
            .payload;

        let real_data: &PersonNamesFormatV1 = data_payload.get();
        let test_mask: PersonNamesFormattingAttributesMask =
            PersonNamesFormattingAttributes::GivenFirst.bit_value()
                | PersonNamesFormattingAttributes::Long.bit_value()
                | PersonNamesFormattingAttributes::Referring.bit_value()
                | PersonNamesFormattingAttributes::Formal.bit_value();

        let first_pattern_data = real_data
            .person_names_patterns
            .iter()
            .map(ZeroFrom::zero_from)
            .find(|pattern: &PersonNamesFormattingData| {
                // The data provider should flip all bits to 1 for mutually exclusive
                // attributes not provided by the source.
                // i.e. : you can have 100, 010, 001, 111, but never 000
                // (000 means nothing is provided, and by specification, this means all are valid)
                pattern.attributes & test_mask == test_mask
            })
            .ok_or_else(|| DataError::custom("Cannot find pattern"))?;
        let value = first_pattern_data
            .patterns
            .get(0)
            .ok_or_else(|| DataError::custom("Cannot find pattern"))?;
        assert_eq!(
            value,
            "{title} {given} {given2} {surname} {generation}, {credentials}"
        );
        assert_eq!(
            real_data.initial_pattern.as_ref().unwrap(),
            "{0}.",
            "we are testing with {} and {}",
            real_data.initial_pattern.as_ref().unwrap(),
            "{0}."
        );
        Ok(())
    }

    #[test]
    fn test_have_pattern_multi_formality() -> Result<(), DataError> {
        let provider = crate::SourceDataProvider::new_testing();

        let data_payload: DataPayload<PersonNamesFormatV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(&langid!("es").into()),
                ..Default::default()
            })?
            .payload;

        let real_data: &PersonNamesFormatV1 = data_payload.get();
        let test_mask: PersonNamesFormattingAttributesMask =
            PersonNamesFormattingAttributes::Sorting.bit_value()
                | PersonNamesFormattingAttributes::Short.bit_value()
                | PersonNamesFormattingAttributes::Referring.bit_value()
                | PersonNamesFormattingAttributes::Formal.bit_value();

        let first_pattern_data = real_data
            .person_names_patterns
            .iter()
            .map(ZeroFrom::zero_from)
            .find(|pattern: &PersonNamesFormattingData| {
                // The data provider should flip all bits to 1 for mutually exclusive
                // attributes not provided by the source.
                // i.e. : you can have 100, 010, 001, 111, but never 000
                // (000 means nothing is provided, and by specification, this means all are valid)
                pattern.attributes & test_mask == test_mask
            })
            .ok_or_else(|| DataError::custom("Cannot find pattern"))?;
        let value = first_pattern_data
            .patterns
            .get(0)
            .ok_or_else(|| DataError::custom("Cannot find pattern 0"))?;
        assert_eq!(value, "{surname}, {title} {given} {given2}");
        let value = first_pattern_data
            .patterns
            .get(1)
            .ok_or_else(|| DataError::custom("Cannot find pattern 1"))?;
        assert_eq!(value, "{surname}, {given-initial} {given2-initial}");

        Ok(())
    }
}