1use crate::base::Merge;
6use fidl_fuchsia_settings::IntlSettings;
7use serde::{Deserialize, Serialize};
8
9#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
10pub struct IntlInfo {
11 pub locales: Option<Vec<LocaleId>>,
12 pub temperature_unit: Option<TemperatureUnit>,
13 pub time_zone_id: Option<String>,
14 pub hour_cycle: Option<HourCycle>,
15}
16
17impl Merge for IntlInfo {
18 fn merge(&self, other: Self) -> Self {
19 IntlInfo {
20 locales: other.locales.or_else(|| self.locales.clone()),
21 temperature_unit: other.temperature_unit.or(self.temperature_unit),
22 time_zone_id: other.time_zone_id.or_else(|| self.time_zone_id.clone()),
23 hour_cycle: other.hour_cycle.or(self.hour_cycle),
24 }
25 }
26}
27
28impl From<fidl_fuchsia_settings::IntlSettings> for IntlInfo {
29 fn from(src: IntlSettings) -> Self {
30 IntlInfo {
31 locales: src.locales.map(|locales| {
32 locales.into_iter().map(fidl_fuchsia_intl::LocaleId::into).collect()
33 }),
34 temperature_unit: src.temperature_unit.map(fidl_fuchsia_intl::TemperatureUnit::into),
35 time_zone_id: src.time_zone_id.map(|tz| tz.id),
36 hour_cycle: src.hour_cycle.map(fidl_fuchsia_settings::HourCycle::into),
37 }
38 }
39}
40
41impl From<IntlInfo> for fidl_fuchsia_settings::IntlSettings {
42 fn from(info: IntlInfo) -> IntlSettings {
43 IntlSettings {
44 locales: info.locales.map(|locales| locales.into_iter().map(LocaleId::into).collect()),
45 temperature_unit: info.temperature_unit.map(TemperatureUnit::into),
46 time_zone_id: info.time_zone_id.map(|tz| fidl_fuchsia_intl::TimeZoneId { id: tz }),
47 hour_cycle: info.hour_cycle.map(HourCycle::into),
48 ..Default::default()
49 }
50 }
51}
52
53#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
54pub struct LocaleId {
55 pub id: String,
56}
57
58impl From<fidl_fuchsia_intl::LocaleId> for LocaleId {
59 fn from(src: fidl_fuchsia_intl::LocaleId) -> Self {
60 LocaleId { id: src.id }
61 }
62}
63
64impl From<LocaleId> for fidl_fuchsia_intl::LocaleId {
65 fn from(src: LocaleId) -> Self {
66 fidl_fuchsia_intl::LocaleId { id: src.id }
67 }
68}
69
70#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
71pub enum TemperatureUnit {
72 Celsius,
73 Fahrenheit,
74}
75
76impl From<fidl_fuchsia_intl::TemperatureUnit> for TemperatureUnit {
77 fn from(src: fidl_fuchsia_intl::TemperatureUnit) -> Self {
78 match src {
79 fidl_fuchsia_intl::TemperatureUnit::Celsius => TemperatureUnit::Celsius,
80 fidl_fuchsia_intl::TemperatureUnit::Fahrenheit => TemperatureUnit::Fahrenheit,
81 }
82 }
83}
84
85impl From<TemperatureUnit> for fidl_fuchsia_intl::TemperatureUnit {
86 fn from(src: TemperatureUnit) -> Self {
87 match src {
88 TemperatureUnit::Celsius => fidl_fuchsia_intl::TemperatureUnit::Celsius,
89 TemperatureUnit::Fahrenheit => fidl_fuchsia_intl::TemperatureUnit::Fahrenheit,
90 }
91 }
92}
93
94#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
95pub enum HourCycle {
96 Unknown,
97 H11,
98 H12,
99 H23,
100 H24,
101}
102
103impl From<fidl_fuchsia_settings::HourCycle> for HourCycle {
104 fn from(src: fidl_fuchsia_settings::HourCycle) -> Self {
105 match src {
106 fidl_fuchsia_settings::HourCycle::Unknown => HourCycle::Unknown,
107 fidl_fuchsia_settings::HourCycle::H11 => HourCycle::H11,
108 fidl_fuchsia_settings::HourCycle::H12 => HourCycle::H12,
109 fidl_fuchsia_settings::HourCycle::H23 => HourCycle::H23,
110 fidl_fuchsia_settings::HourCycle::H24 => HourCycle::H24,
111 }
112 }
113}
114
115impl From<HourCycle> for fidl_fuchsia_settings::HourCycle {
116 fn from(src: HourCycle) -> Self {
117 match src {
118 HourCycle::Unknown => fidl_fuchsia_settings::HourCycle::Unknown,
119 HourCycle::H11 => fidl_fuchsia_settings::HourCycle::H11,
120 HourCycle::H12 => fidl_fuchsia_settings::HourCycle::H12,
121 HourCycle::H23 => fidl_fuchsia_settings::HourCycle::H23,
122 HourCycle::H24 => fidl_fuchsia_settings::HourCycle::H24,
123 }
124 }
125}
126
127#[cfg(test)]
128mod tests {
129 use crate::intl::types::{HourCycle, IntlInfo, LocaleId, TemperatureUnit};
130 use fidl_fuchsia_settings::IntlSettings;
131 use settings_storage::fidl_storage::FidlStorageConvertible;
132
133 const TIME_ZONE_ID: &str = "PDT";
134 const LOCALE_ID: &str = "en_us";
135
136 #[fuchsia::test]
137 fn fidl_storage_convertible_from_storable_empty() {
138 let info = IntlInfo::from_storable(IntlSettings::default());
139
140 assert_eq!(
141 info,
142 IntlInfo {
143 locales: None,
144 temperature_unit: None,
145 time_zone_id: None,
146 hour_cycle: None,
147 }
148 );
149 }
150
151 #[fuchsia::test]
152 fn fidl_storage_convertible_from_storable() {
153 let intl_settings = IntlSettings {
154 locales: Some(vec![fidl_fuchsia_intl::LocaleId { id: LOCALE_ID.into() }]),
155 temperature_unit: Some(fidl_fuchsia_intl::TemperatureUnit::Celsius),
156 time_zone_id: Some(fidl_fuchsia_intl::TimeZoneId { id: TIME_ZONE_ID.to_string() }),
157 hour_cycle: Some(fidl_fuchsia_settings::HourCycle::H12),
158 ..Default::default()
159 };
160
161 let info = IntlInfo::from_storable(intl_settings);
162
163 assert_eq!(
164 info,
165 IntlInfo {
166 locales: Some(vec![LocaleId { id: LOCALE_ID.into() }]),
167 temperature_unit: Some(TemperatureUnit::Celsius),
168 time_zone_id: Some(TIME_ZONE_ID.to_string()),
169 hour_cycle: Some(HourCycle::H12),
170 }
171 );
172 }
173
174 #[fuchsia::test]
175 fn fidl_storage_convertible_to_storable_empty() {
176 let info = IntlInfo {
177 locales: None,
178 temperature_unit: None,
179 time_zone_id: None,
180 hour_cycle: None,
181 };
182 let storable = info.to_storable();
183
184 assert_eq!(storable, IntlSettings::default(),);
185 }
186
187 #[fuchsia::test]
188 fn fidl_storage_convertible_to_storable() {
189 let info = IntlInfo {
190 locales: Some(vec![LocaleId { id: LOCALE_ID.into() }]),
191 temperature_unit: Some(TemperatureUnit::Celsius),
192 time_zone_id: Some(TIME_ZONE_ID.to_string()),
193 hour_cycle: Some(HourCycle::H12),
194 };
195
196 let storable = info.to_storable();
197
198 assert_eq!(
199 storable,
200 IntlSettings {
201 locales: Some(vec![fidl_fuchsia_intl::LocaleId { id: LOCALE_ID.into() }]),
202 temperature_unit: Some(fidl_fuchsia_intl::TemperatureUnit::Celsius),
203 time_zone_id: Some(fidl_fuchsia_intl::TimeZoneId { id: TIME_ZONE_ID.to_string() }),
204 hour_cycle: Some(fidl_fuchsia_settings::HourCycle::H12),
205 ..Default::default()
206 }
207 );
208 }
209}