icu_provider_source/cldr_serde/time_zones/
time_zone_names.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
// 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 timeZoneNames.json files.
//!
//! Sample file:
//! <https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-dates-full/main/en/timeZoneNames.json>

use icu_pattern::{DoublePlaceholder, PatternString, SinglePlaceholder};
use serde::{
    de::{IgnoredAny, MapAccess, Visitor},
    Deserialize, Deserializer,
};
use std::collections::BTreeMap;

#[derive(PartialEq, Debug, Clone, Deserialize)]
pub(crate) struct ZoneFormat(pub(crate) BTreeMap<String, String>);

#[derive(PartialEq, Debug, Clone, Deserialize)]
pub(crate) struct Metazone {
    pub(crate) long: Option<ZoneFormat>,
    pub(crate) short: Option<ZoneFormat>,
}

impl Metazone {
    pub(crate) fn long_short(&self, long: bool) -> Option<&ZoneFormat> {
        if long {
            self.long.as_ref()
        } else {
            self.short.as_ref()
        }
    }
}

#[derive(PartialEq, Debug, Clone, Deserialize)]
pub(crate) struct Metazones(pub(crate) BTreeMap<String, Metazone>);

#[derive(PartialEq, Debug, Clone, Deserialize)]
// Since this value can be either a Location or a table of sub-regions, we use
// the presence of fields to distinguish the object types. If CLDR-JSON adds
// more fields to Location in the future, they must also be added here.
//
// Example errors that may be caused by this:
//
// - Cannot find bcp47 for "America/Argentina".
// - Cannot find bcp47 for "Etc/UTC/short".
#[serde(deny_unknown_fields)]
pub(crate) struct Location {
    pub(crate) long: Option<ZoneFormat>,
    pub(crate) short: Option<ZoneFormat>,
    #[serde(rename = "exemplarCity")]
    pub(crate) exemplar_city: Option<String>,
    #[serde(rename = "exemplarCity-alt-secondary")]
    pub(crate) exemplar_city_alt_secondary: Option<String>,
}

impl Location {
    pub(crate) fn long_short(&self, long: bool) -> Option<&ZoneFormat> {
        if long {
            self.long.as_ref()
        } else {
            self.short.as_ref()
        }
    }
}

#[derive(PartialEq, Debug, Clone, Deserialize)]
#[serde(untagged)]
pub(crate) enum LocationOrSubRegion {
    Location(Location),
    SubRegion(BTreeMap<String, Location>),
}

#[derive(PartialEq, Debug, Clone, Default, Deserialize)]
pub(crate) struct Region(pub(crate) BTreeMap<String, LocationOrSubRegion>);

#[derive(PartialEq, Debug, Clone, Default, Deserialize)]
pub(crate) struct Zones(pub(crate) BTreeMap<String, Region>);

#[derive(PartialEq, Debug, Default, Clone)]
pub(crate) struct TimeZoneNames {
    pub(crate) hour_format: String,
    pub(crate) gmt_format: PatternString<SinglePlaceholder>,
    pub(crate) gmt_zero_format: String,
    pub(crate) region_format: PatternString<SinglePlaceholder>,
    pub(crate) region_format_dt: PatternString<SinglePlaceholder>,
    pub(crate) region_format_st: PatternString<SinglePlaceholder>,
    pub(crate) fallback_format: PatternString<DoublePlaceholder>,
    pub(crate) zone: Zones,
    pub(crate) metazone: Option<Metazones>,
}

pub(crate) struct TimeZoneNamesVisitor;

impl<'de> Visitor<'de> for TimeZoneNamesVisitor {
    type Value = TimeZoneNames;

    // Format a message stating what data this Visitor expects to receive.
    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("formatting data by numbering system")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: MapAccess<'de>,
    {
        let mut time_zone_names = TimeZoneNames::default();
        while let Some(key) = map.next_key::<String>()? {
            if key.eq("hourFormat") {
                let value = map.next_value::<String>()?;
                time_zone_names.hour_format = value;
            } else if key.eq("gmtFormat") {
                let value = map.next_value::<PatternString<SinglePlaceholder>>()?;
                time_zone_names.gmt_format = value;
            } else if key.eq("gmtZeroFormat") {
                let value = map.next_value::<String>()?;
                time_zone_names.gmt_zero_format = value;
            } else if key.eq("fallbackFormat") {
                let value = map.next_value::<PatternString<DoublePlaceholder>>()?;
                time_zone_names.fallback_format = value;
            } else if key.starts_with("regionFormat") {
                let value = map.next_value::<PatternString<SinglePlaceholder>>()?;
                // key is of the form: "regionFormat-type-variant"
                if key.ends_with("-standard") {
                    time_zone_names.region_format_st = value;
                } else if key.ends_with("-daylight") {
                    time_zone_names.region_format_dt = value;
                } else {
                    time_zone_names.region_format = value
                }
            } else if key.eq("metazone") {
                let value = map.next_value::<Metazones>()?;
                time_zone_names.metazone = Some(value);
            } else if key.eq("zone") {
                let value = map.next_value::<Zones>()?;
                time_zone_names.zone = value;
            } else {
                map.next_value::<IgnoredAny>()?;
            }
        }

        Ok(time_zone_names)
    }
}

impl<'de> Deserialize<'de> for TimeZoneNames {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(TimeZoneNamesVisitor)
    }
}

#[derive(PartialEq, Debug, Clone, Deserialize)]
pub(crate) struct Dates {
    #[serde(rename = "timeZoneNames")]
    pub(crate) time_zone_names: TimeZoneNames,
}

#[derive(PartialEq, Debug, Deserialize)]
pub(crate) struct LangTimeZones {
    pub(crate) dates: Dates,
}

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