settings/intl/
types.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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::base::Merge;
use fidl_fuchsia_settings::IntlSettings;
use serde::{Deserialize, Serialize};

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct IntlInfo {
    pub locales: Option<Vec<LocaleId>>,
    pub temperature_unit: Option<TemperatureUnit>,
    pub time_zone_id: Option<String>,
    pub hour_cycle: Option<HourCycle>,
}

impl Merge for IntlInfo {
    fn merge(&self, other: Self) -> Self {
        IntlInfo {
            locales: other.locales.or_else(|| self.locales.clone()),
            temperature_unit: other.temperature_unit.or(self.temperature_unit),
            time_zone_id: other.time_zone_id.or_else(|| self.time_zone_id.clone()),
            hour_cycle: other.hour_cycle.or(self.hour_cycle),
        }
    }
}

impl From<fidl_fuchsia_settings::IntlSettings> for IntlInfo {
    fn from(src: IntlSettings) -> Self {
        IntlInfo {
            locales: src.locales.map(|locales| {
                locales.into_iter().map(fidl_fuchsia_intl::LocaleId::into).collect()
            }),
            temperature_unit: src.temperature_unit.map(fidl_fuchsia_intl::TemperatureUnit::into),
            time_zone_id: src.time_zone_id.map(|tz| tz.id),
            hour_cycle: src.hour_cycle.map(fidl_fuchsia_settings::HourCycle::into),
        }
    }
}

impl From<IntlInfo> for fidl_fuchsia_settings::IntlSettings {
    fn from(info: IntlInfo) -> IntlSettings {
        IntlSettings {
            locales: info.locales.map(|locales| locales.into_iter().map(LocaleId::into).collect()),
            temperature_unit: info.temperature_unit.map(TemperatureUnit::into),
            time_zone_id: info.time_zone_id.map(|tz| fidl_fuchsia_intl::TimeZoneId { id: tz }),
            hour_cycle: info.hour_cycle.map(HourCycle::into),
            ..Default::default()
        }
    }
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct LocaleId {
    pub id: String,
}

impl From<fidl_fuchsia_intl::LocaleId> for LocaleId {
    fn from(src: fidl_fuchsia_intl::LocaleId) -> Self {
        LocaleId { id: src.id }
    }
}

impl From<LocaleId> for fidl_fuchsia_intl::LocaleId {
    fn from(src: LocaleId) -> Self {
        fidl_fuchsia_intl::LocaleId { id: src.id }
    }
}

#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
pub enum TemperatureUnit {
    Celsius,
    Fahrenheit,
}

impl From<fidl_fuchsia_intl::TemperatureUnit> for TemperatureUnit {
    fn from(src: fidl_fuchsia_intl::TemperatureUnit) -> Self {
        match src {
            fidl_fuchsia_intl::TemperatureUnit::Celsius => TemperatureUnit::Celsius,
            fidl_fuchsia_intl::TemperatureUnit::Fahrenheit => TemperatureUnit::Fahrenheit,
        }
    }
}

impl From<TemperatureUnit> for fidl_fuchsia_intl::TemperatureUnit {
    fn from(src: TemperatureUnit) -> Self {
        match src {
            TemperatureUnit::Celsius => fidl_fuchsia_intl::TemperatureUnit::Celsius,
            TemperatureUnit::Fahrenheit => fidl_fuchsia_intl::TemperatureUnit::Fahrenheit,
        }
    }
}

#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
pub enum HourCycle {
    Unknown,
    H11,
    H12,
    H23,
    H24,
}

impl From<fidl_fuchsia_settings::HourCycle> for HourCycle {
    fn from(src: fidl_fuchsia_settings::HourCycle) -> Self {
        match src {
            fidl_fuchsia_settings::HourCycle::Unknown => HourCycle::Unknown,
            fidl_fuchsia_settings::HourCycle::H11 => HourCycle::H11,
            fidl_fuchsia_settings::HourCycle::H12 => HourCycle::H12,
            fidl_fuchsia_settings::HourCycle::H23 => HourCycle::H23,
            fidl_fuchsia_settings::HourCycle::H24 => HourCycle::H24,
        }
    }
}

impl From<HourCycle> for fidl_fuchsia_settings::HourCycle {
    fn from(src: HourCycle) -> Self {
        match src {
            HourCycle::Unknown => fidl_fuchsia_settings::HourCycle::Unknown,
            HourCycle::H11 => fidl_fuchsia_settings::HourCycle::H11,
            HourCycle::H12 => fidl_fuchsia_settings::HourCycle::H12,
            HourCycle::H23 => fidl_fuchsia_settings::HourCycle::H23,
            HourCycle::H24 => fidl_fuchsia_settings::HourCycle::H24,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::intl::types::{HourCycle, IntlInfo, LocaleId, TemperatureUnit};
    use fidl_fuchsia_settings::IntlSettings;
    use settings_storage::fidl_storage::FidlStorageConvertible;

    const TIME_ZONE_ID: &str = "PDT";
    const LOCALE_ID: &str = "en_us";

    #[fuchsia::test]
    fn fidl_storage_convertible_from_storable_empty() {
        let info = IntlInfo::from_storable(IntlSettings::default());

        assert_eq!(
            info,
            IntlInfo {
                locales: None,
                temperature_unit: None,
                time_zone_id: None,
                hour_cycle: None,
            }
        );
    }

    #[fuchsia::test]
    fn fidl_storage_convertible_from_storable() {
        let intl_settings = IntlSettings {
            locales: Some(vec![fidl_fuchsia_intl::LocaleId { id: LOCALE_ID.into() }]),
            temperature_unit: Some(fidl_fuchsia_intl::TemperatureUnit::Celsius),
            time_zone_id: Some(fidl_fuchsia_intl::TimeZoneId { id: TIME_ZONE_ID.to_string() }),
            hour_cycle: Some(fidl_fuchsia_settings::HourCycle::H12),
            ..Default::default()
        };

        let info = IntlInfo::from_storable(intl_settings);

        assert_eq!(
            info,
            IntlInfo {
                locales: Some(vec![LocaleId { id: LOCALE_ID.into() }]),
                temperature_unit: Some(TemperatureUnit::Celsius),
                time_zone_id: Some(TIME_ZONE_ID.to_string()),
                hour_cycle: Some(HourCycle::H12),
            }
        );
    }

    #[fuchsia::test]
    fn fidl_storage_convertible_to_storable_empty() {
        let info = IntlInfo {
            locales: None,
            temperature_unit: None,
            time_zone_id: None,
            hour_cycle: None,
        };
        let storable = info.to_storable();

        assert_eq!(storable, IntlSettings::default(),);
    }

    #[fuchsia::test]
    fn fidl_storage_convertible_to_storable() {
        let info = IntlInfo {
            locales: Some(vec![LocaleId { id: LOCALE_ID.into() }]),
            temperature_unit: Some(TemperatureUnit::Celsius),
            time_zone_id: Some(TIME_ZONE_ID.to_string()),
            hour_cycle: Some(HourCycle::H12),
        };

        let storable = info.to_storable();

        assert_eq!(
            storable,
            IntlSettings {
                locales: Some(vec![fidl_fuchsia_intl::LocaleId { id: LOCALE_ID.into() }]),
                temperature_unit: Some(fidl_fuchsia_intl::TemperatureUnit::Celsius),
                time_zone_id: Some(fidl_fuchsia_intl::TimeZoneId { id: TIME_ZONE_ID.to_string() }),
                hour_cycle: Some(fidl_fuchsia_settings::HourCycle::H12),
                ..Default::default()
            }
        );
    }
}