icu_provider_source/cldr_serde/
week_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
// 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 weekData.json files.
//!
//! Sample file:
//! `<https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-core/supplemental/weekData.json>`

use core::convert::TryFrom;
use icu::locale::{subtags::region, subtags::Region};
use serde::{Deserialize, Deserializer};
use std::collections::BTreeMap;
use std::num::ParseIntError;
use std::str::FromStr;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) enum Weekday {
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat,
    Sun,
}

impl From<&Weekday> for icu::calendar::types::IsoWeekday {
    fn from(day: &Weekday) -> Self {
        use icu::calendar::types::IsoWeekday;
        match day {
            Weekday::Mon => IsoWeekday::Monday,
            Weekday::Tue => IsoWeekday::Tuesday,
            Weekday::Wed => IsoWeekday::Wednesday,
            Weekday::Thu => IsoWeekday::Thursday,
            Weekday::Fri => IsoWeekday::Friday,
            Weekday::Sat => IsoWeekday::Saturday,
            Weekday::Sun => IsoWeekday::Sunday,
        }
    }
}

impl From<Weekday> for icu::calendar::types::IsoWeekday {
    fn from(day: Weekday) -> Self {
        (&day).into()
    }
}

/// The territory that data is keyed by.
///
/// For example the "AD" in "weekData": { "minDays": { "AD": 4, } }
///
/// The contained types are strings rather than [`icu::locale::subtags::Region`]
/// to avoid an extra parsing step of the variant in data providers.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Territory {
    // A territory string, e.g. "AD" for Andorra.
    Region(Region),
    // An alternative variant for a given territory (e.g. the first day of the
    // week can be sunday rather than monday GB). The string is set to the region
    // with the "-alt-variant" suffix present in the json.
    AltVariantRegion(Region),
}

/// The string used to represent the default territory.
pub(crate) const DEFAULT_TERRITORY: Territory = Territory::Region(region!("001"));

/// Suffix used to denote alternative week data variants for a given territory (e.g. English BC/AD v English BCE/CE).
const ALT_VARIANT_SUFFIX: &str = "-alt-variant";

impl<'de> Deserialize<'de> for Territory {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct TerritoryVisitor;

        impl serde::de::Visitor<'_> for TerritoryVisitor {
            type Value = Territory;

            fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(
                    formatter,
                    "a valid Unicode Language Identifier or default territory literal"
                )
            }

            fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                if let Some(prefix) = s.strip_suffix(ALT_VARIANT_SUFFIX) {
                    return Ok(Territory::AltVariantRegion(
                        prefix.parse::<Region>().map_err(serde::de::Error::custom)?,
                    ));
                }

                Ok(Territory::Region(
                    s.parse::<Region>().map_err(serde::de::Error::custom)?,
                ))
            }
        }

        deserializer.deserialize_string(TerritoryVisitor)
    }
}

/// Wrapper used to deserialize json string keys as u8s.
#[derive(Debug, Deserialize)]
#[serde(try_from = "String")]
pub(crate) struct U8(pub(crate) u8);

impl TryFrom<String> for U8 {
    type Error = ParseIntError;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        Ok(Self(u8::from_str(&s)?))
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct WeekData {
    pub(crate) min_days: BTreeMap<Territory, U8>,
    pub(crate) first_day: BTreeMap<Territory, Weekday>,
    pub(crate) weekend_start: BTreeMap<Territory, Weekday>,
    pub(crate) weekend_end: BTreeMap<Territory, Weekday>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Supplemental {
    pub(crate) week_data: WeekData,
}

#[derive(Deserialize)]
pub(crate) struct Resource {
    pub(crate) supplemental: Supplemental,
}