icu_provider_source/cldr_serde/units/
data.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
// 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 ).

//! Serde structs representing CLDR JSON units.json file.
//!
//! Sample file:
//! <https://github.com/unicode-org/cldr-json/blob/master/cldr-json/cldr-units-full/main/en/units.json>

use icu_pattern::{PatternString, SinglePlaceholder};
use serde::Deserialize;
use std::collections::BTreeMap;

/// Represents various patterns for a unit according to plural rules.
/// The plural rule categories are: zero, one, two, few, many and other.
/// For more details, refer to the technical report:
///     https://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
#[derive(PartialEq, Debug, Deserialize, Clone)]
pub(crate) struct Patterns {
    #[serde(rename = "displayName-count-0")]
    pub(crate) explicit_zero: Option<PatternString<SinglePlaceholder>>,

    #[serde(rename = "displayName-count-1")]
    pub(crate) explicit_one: Option<PatternString<SinglePlaceholder>>,

    #[serde(rename = "unitPattern-count-zero")]
    pub(crate) zero: Option<PatternString<SinglePlaceholder>>,

    #[serde(rename = "unitPattern-count-one")]
    pub(crate) one: Option<PatternString<SinglePlaceholder>>,

    #[serde(rename = "unitPattern-count-two")]
    pub(crate) two: Option<PatternString<SinglePlaceholder>>,

    #[serde(rename = "unitPattern-count-few")]
    pub(crate) few: Option<PatternString<SinglePlaceholder>>,

    #[serde(rename = "unitPattern-count-many")]
    pub(crate) many: Option<PatternString<SinglePlaceholder>>,

    #[serde(rename = "unitPattern-count-other")]
    pub(crate) other: Option<PatternString<SinglePlaceholder>>,

    #[serde(rename = "compoundUnitPattern")]
    pub(crate) compound_unit_pattern: Option<String>,

    #[serde(rename = "unitPrefixPattern")]
    pub(crate) unit_prefix_pattern: Option<String>,

    #[serde(rename = "compoundUnitPattern1")]
    pub(crate) compound_unit_pattern1: Option<String>,

    #[serde(rename = "compountUnitPattern1-count-0")]
    pub(crate) explicit_zero_compound_unit_pattern1: Option<String>,

    #[serde(rename = "compountUnitPattern1-count-1")]
    pub(crate) explicit_one_compound_unit_pattern1: Option<String>,

    #[serde(rename = "compoundUnitPattern1-count-zero")]
    pub(crate) zero_compound_unit_pattern1: Option<String>,

    #[serde(rename = "compoundUnitPattern1-count-one")]
    pub(crate) one_compound_unit_pattern1: Option<String>,

    #[serde(rename = "compoundUnitPattern1-count-two")]
    pub(crate) two_compound_unit_pattern1: Option<String>,

    #[serde(rename = "compoundUnitPattern1-count-few")]
    pub(crate) few_compound_unit_pattern1: Option<String>,

    #[serde(rename = "compoundUnitPattern1-count-many")]
    pub(crate) many_compound_unit_pattern1: Option<String>,

    #[serde(rename = "compoundUnitPattern1-count-other")]
    pub(crate) other_compound_unit_pattern1: Option<String>,
}

#[derive(PartialEq, Debug, Deserialize)]
pub struct DurationUnit {
    #[serde(rename = "durationUnitPattern")]
    pub(crate) pat: String,
    #[serde(rename = "durationUnitPattern-alt-variant")]
    pub(crate) alt_pat: Option<String>,
}

#[derive(PartialEq, Debug, Deserialize)]
pub struct DurationUnits {
    #[serde(rename = "durationUnit-type-hm")]
    pub hm: DurationUnit,
    #[serde(rename = "durationUnit-type-hms")]
    pub hms: DurationUnit,
    #[serde(rename = "durationUnit-type-ms")]
    pub ms: DurationUnit,
}

// TODO: replace Value with specific structs
#[derive(PartialEq, Debug)]
pub(crate) struct UnitsData {
    pub(crate) long: UnitsLengthData,

    pub(crate) short: UnitsLengthData,

    pub(crate) narrow: UnitsLengthData,

    pub(crate) duration: DurationUnits,
}

#[derive(PartialEq, Debug)]
pub(crate) struct UnitsLengthData {
    pub(crate) units: BTreeMap<String, Patterns>,
    pub(crate) per: Patterns,
    pub(crate) times: Patterns,
    pub(crate) powers: BTreeMap<usize, Patterns>,
    pub(crate) binary: BTreeMap<u8, Patterns>,
    pub(crate) decimal: BTreeMap<i8, Patterns>,
}

impl<'de> Deserialize<'de> for UnitsData {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Raw {
            long: BTreeMap<String, Patterns>,
            short: BTreeMap<String, Patterns>,
            narrow: BTreeMap<String, Patterns>,
            #[serde(flatten)]
            duration: DurationUnits,
        }

        let Raw {
            long,
            short,
            narrow,
            duration,
        } = Raw::deserialize(deserializer)?;

        let construct = |mut map: BTreeMap<String, Patterns>| UnitsLengthData {
            per: map.remove("per").unwrap(),
            times: map.remove("times").unwrap(),
            powers: map
                .iter()
                .filter_map(|(k, v)| Some((k.strip_prefix("power")?.parse().ok()?, v.clone())))
                .collect(),
            binary: map
                .iter()
                .filter_map(|(k, v)| Some((k.strip_prefix("1024p")?.parse().ok()?, v.clone())))
                .collect(),
            decimal: map
                .iter()
                .filter_map(|(k, v)| Some((k.strip_prefix("10p")?.parse().ok()?, v.clone())))
                .collect(),
            units: map
                .into_iter()
                .filter_map(|(k, v)| {
                    if k.starts_with("10p")
                        || k.starts_with("1024p")
                        || (k.starts_with("power") && !k.starts_with("power-"))
                    {
                        return None;
                    }
                    k.split_once('-').map(|(_, unit)| (unit.to_string(), v))
                })
                .collect(),
        };

        Ok(Self {
            long: construct(long),
            short: construct(short),
            narrow: construct(narrow),
            duration,
        })
    }
}

#[derive(PartialEq, Debug, Deserialize)]
pub(crate) struct LangUnits {
    pub(crate) units: UnitsData,
}

pub(crate) type Resource = super::super::LocaleResource<LangUnits>;