sl4f_lib/setui/
types.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::format_err;
6use fidl_fuchsia_settings::IntlSettings;
7use serde::{Deserialize, Serialize};
8
9/// Supported setUi commands.
10pub enum SetUiMethod {
11    IsMicMuted,
12    SetNetwork,
13    GetNetwork,
14    SetIntl,
15    GetIntl,
16    SetBrightness,
17    SetMediaVolume,
18    SetMicMute,
19}
20
21impl std::str::FromStr for SetUiMethod {
22    type Err = anyhow::Error;
23
24    fn from_str(method: &str) -> Result<Self, Self::Err> {
25        match method {
26            "SetNetwork" => Ok(SetUiMethod::SetNetwork),
27            "GetNetwork" => Ok(SetUiMethod::GetNetwork),
28            "SetIntl" => Ok(SetUiMethod::SetIntl),
29            "GetIntl" => Ok(SetUiMethod::GetIntl),
30            "IsMicMuted" => Ok(SetUiMethod::IsMicMuted),
31            "SetBrightness" => Ok(SetUiMethod::SetBrightness),
32            "SetMediaVolume" => Ok(SetUiMethod::SetMediaVolume),
33            "SetMicMute" => Ok(SetUiMethod::SetMicMute),
34            _ => return Err(format_err!("invalid SetUi SL4F method: {}", method)),
35        }
36    }
37}
38
39#[derive(Serialize, Deserialize, Debug)]
40pub enum SetUiResult {
41    Success,
42}
43
44#[derive(Serialize, Deserialize, Debug)]
45#[serde(rename_all = "snake_case")]
46pub enum NetworkType {
47    Ethernet,
48    Wifi,
49    Unknown,
50}
51
52#[derive(Serialize, Deserialize, Debug)]
53#[serde(rename_all = "snake_case")]
54pub enum MicStates {
55    Muted,
56    Available,
57    Unknown,
58}
59
60/// --------- Data types for Intl ---------
61
62/// Types used for fuchsia.intl FIDL calls.
63/// Implementation is copied from /src/settings/service/src/switchboard/intl_types.rs
64/// TODO(https://fxbug.dev/42130922) Remove this once intl_types is defined in a rust_library.
65#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
66pub struct IntlInfo {
67    pub locales: Option<Vec<LocaleId>>,
68    pub temperature_unit: Option<TemperatureUnit>,
69    pub time_zone_id: Option<String>,
70    pub hour_cycle: Option<HourCycle>,
71}
72
73/// Conversion from IntlSettings to IntlInfo
74impl From<fidl_fuchsia_settings::IntlSettings> for IntlInfo {
75    fn from(src: IntlSettings) -> Self {
76        IntlInfo {
77            locales: src.locales.map_or(None, |locales| {
78                Some(locales.into_iter().map(fidl_fuchsia_intl::LocaleId::into).collect())
79            }),
80            temperature_unit: src.temperature_unit.map(fidl_fuchsia_intl::TemperatureUnit::into),
81            time_zone_id: src.time_zone_id.map_or(None, |tz| Some(tz.id)),
82            hour_cycle: src.hour_cycle.map(fidl_fuchsia_settings::HourCycle::into),
83        }
84    }
85}
86
87/// Conversion from IntlInfo to IntlSettings
88impl Into<fidl_fuchsia_settings::IntlSettings> for IntlInfo {
89    fn into(self) -> IntlSettings {
90        let mut intl_settings = IntlSettings::default();
91
92        intl_settings.locales = self
93            .locales
94            .map_or(None, |locales| Some(locales.into_iter().map(LocaleId::into).collect()));
95        intl_settings.temperature_unit = self.temperature_unit.map(TemperatureUnit::into);
96        intl_settings.time_zone_id =
97            self.time_zone_id.map_or(None, |tz| Some(fidl_fuchsia_intl::TimeZoneId { id: tz }));
98        intl_settings.hour_cycle = self.hour_cycle.map(HourCycle::into);
99
100        intl_settings
101    }
102}
103
104/// Unicode BCP-47 Locale Identifier
105/// Reference LocaleId definition in
106/// https://fuchsia.googlesource.com/fuchsia/+/HEAD/sdk/fidl/fuchsia.intl/intl.fidl
107#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
108pub struct LocaleId {
109    pub id: String,
110}
111
112impl From<fidl_fuchsia_intl::LocaleId> for LocaleId {
113    fn from(src: fidl_fuchsia_intl::LocaleId) -> Self {
114        LocaleId { id: src.id }
115    }
116}
117
118impl From<LocaleId> for fidl_fuchsia_intl::LocaleId {
119    fn from(src: LocaleId) -> Self {
120        fidl_fuchsia_intl::LocaleId { id: src.id }
121    }
122}
123
124/// Selection of [temperature units](https://en.wikipedia.org/wiki/Degree_(temperature)).
125/// Reference TemperatureUnit definition in
126/// https://fuchsia.googlesource.com/fuchsia/+/HEAD/sdk/fidl/fuchsia.intl/intl.fidl
127#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize)]
128pub enum TemperatureUnit {
129    Celsius,
130    Fahrenheit,
131}
132
133impl From<fidl_fuchsia_intl::TemperatureUnit> for TemperatureUnit {
134    fn from(src: fidl_fuchsia_intl::TemperatureUnit) -> Self {
135        match src {
136            fidl_fuchsia_intl::TemperatureUnit::Celsius => TemperatureUnit::Celsius,
137            fidl_fuchsia_intl::TemperatureUnit::Fahrenheit => TemperatureUnit::Fahrenheit,
138        }
139    }
140}
141
142impl From<TemperatureUnit> for fidl_fuchsia_intl::TemperatureUnit {
143    fn from(src: TemperatureUnit) -> Self {
144        match src {
145            TemperatureUnit::Celsius => fidl_fuchsia_intl::TemperatureUnit::Celsius,
146            TemperatureUnit::Fahrenheit => fidl_fuchsia_intl::TemperatureUnit::Fahrenheit,
147        }
148    }
149}
150
151/// Whether if the time format should be using 12 hour or 24 hour clock. H indicates the
152/// maximum number that the hour indicator will ever show.
153///
154/// Reference HourCycle definition in
155/// https://fuchsia.googlesource.com/fuchsia/+/HEAD/sdk/fidl/fuchsia.settings/intl.fidl
156#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize)]
157pub enum HourCycle {
158    Unknown,
159    H11,
160    H12,
161    H23,
162    H24,
163}
164
165impl From<fidl_fuchsia_settings::HourCycle> for HourCycle {
166    fn from(src: fidl_fuchsia_settings::HourCycle) -> Self {
167        match src {
168            fidl_fuchsia_settings::HourCycle::Unknown => HourCycle::Unknown,
169            fidl_fuchsia_settings::HourCycle::H11 => HourCycle::H11,
170            fidl_fuchsia_settings::HourCycle::H12 => HourCycle::H12,
171            fidl_fuchsia_settings::HourCycle::H23 => HourCycle::H23,
172            fidl_fuchsia_settings::HourCycle::H24 => HourCycle::H24,
173        }
174    }
175}
176
177impl From<HourCycle> for fidl_fuchsia_settings::HourCycle {
178    fn from(src: HourCycle) -> Self {
179        match src {
180            HourCycle::Unknown => fidl_fuchsia_settings::HourCycle::Unknown,
181            HourCycle::H11 => fidl_fuchsia_settings::HourCycle::H11,
182            HourCycle::H12 => fidl_fuchsia_settings::HourCycle::H12,
183            HourCycle::H23 => fidl_fuchsia_settings::HourCycle::H23,
184            HourCycle::H24 => fidl_fuchsia_settings::HourCycle::H24,
185        }
186    }
187}