icu_provider_source/cldr_serde/time_zones/
time_zone_names.rsuse 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)]
#[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;
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>>()?;
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>;