sl4f_lib/setui/
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
// 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 anyhow::format_err;
use fidl_fuchsia_settings::IntlSettings;
use serde::{Deserialize, Serialize};

/// Supported setUi commands.
pub enum SetUiMethod {
    IsMicMuted,
    SetNetwork,
    GetNetwork,
    SetIntl,
    GetIntl,
    SetBrightness,
    SetMediaVolume,
    SetMicMute,
}

impl std::str::FromStr for SetUiMethod {
    type Err = anyhow::Error;

    fn from_str(method: &str) -> Result<Self, Self::Err> {
        match method {
            "SetNetwork" => Ok(SetUiMethod::SetNetwork),
            "GetNetwork" => Ok(SetUiMethod::GetNetwork),
            "SetIntl" => Ok(SetUiMethod::SetIntl),
            "GetIntl" => Ok(SetUiMethod::GetIntl),
            "IsMicMuted" => Ok(SetUiMethod::IsMicMuted),
            "SetBrightness" => Ok(SetUiMethod::SetBrightness),
            "SetMediaVolume" => Ok(SetUiMethod::SetMediaVolume),
            "SetMicMute" => Ok(SetUiMethod::SetMicMute),
            _ => return Err(format_err!("invalid SetUi SL4F method: {}", method)),
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub enum SetUiResult {
    Success,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum NetworkType {
    Ethernet,
    Wifi,
    Unknown,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum MicStates {
    Muted,
    Available,
    Unknown,
}

/// --------- Data types for Intl ---------

/// Types used for fuchsia.intl FIDL calls.
/// Implementation is copied from /src/settings/service/src/switchboard/intl_types.rs
/// TODO(https://fxbug.dev/42130922) Remove this once intl_types is defined in a rust_library.
#[derive(PartialEq, Eq, 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>,
}

/// Conversion from IntlSettings to IntlInfo
impl From<fidl_fuchsia_settings::IntlSettings> for IntlInfo {
    fn from(src: IntlSettings) -> Self {
        IntlInfo {
            locales: src.locales.map_or(None, |locales| {
                Some(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_or(None, |tz| Some(tz.id)),
            hour_cycle: src.hour_cycle.map(fidl_fuchsia_settings::HourCycle::into),
        }
    }
}

/// Conversion from IntlInfo to IntlSettings
impl Into<fidl_fuchsia_settings::IntlSettings> for IntlInfo {
    fn into(self) -> IntlSettings {
        let mut intl_settings = IntlSettings::default();

        intl_settings.locales = self
            .locales
            .map_or(None, |locales| Some(locales.into_iter().map(LocaleId::into).collect()));
        intl_settings.temperature_unit = self.temperature_unit.map(TemperatureUnit::into);
        intl_settings.time_zone_id =
            self.time_zone_id.map_or(None, |tz| Some(fidl_fuchsia_intl::TimeZoneId { id: tz }));
        intl_settings.hour_cycle = self.hour_cycle.map(HourCycle::into);

        intl_settings
    }
}

/// Unicode BCP-47 Locale Identifier
/// Reference LocaleId definition in
/// https://fuchsia.googlesource.com/fuchsia/+/HEAD/sdk/fidl/fuchsia.intl/intl.fidl
#[derive(PartialEq, Eq, 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 }
    }
}

/// Selection of [temperature units](https://en.wikipedia.org/wiki/Degree_(temperature)).
/// Reference TemperatureUnit definition in
/// https://fuchsia.googlesource.com/fuchsia/+/HEAD/sdk/fidl/fuchsia.intl/intl.fidl
#[derive(PartialEq, Eq, 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,
        }
    }
}

/// Whether if the time format should be using 12 hour or 24 hour clock. H indicates the
/// maximum number that the hour indicator will ever show.
///
/// Reference HourCycle definition in
/// https://fuchsia.googlesource.com/fuchsia/+/HEAD/sdk/fidl/fuchsia.settings/intl.fidl
#[derive(PartialEq, Eq, 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,
        }
    }
}