1use crate::light::light_hardware_configuration::DisableConditions;
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9#[derive(PartialEq, Default, Debug, Clone, Serialize, Deserialize)]
10pub struct LightInfo {
11 pub light_groups: HashMap<String, LightGroup>,
12}
13
14#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
16pub struct LightGroup {
17 pub name: String,
18 pub enabled: bool,
19 pub light_type: LightType,
20 pub lights: Vec<LightState>,
21
22 pub hardware_index: Vec<u32>,
25
26 pub disable_conditions: Vec<DisableConditions>,
34}
35
36impl From<LightGroup> for fidl_fuchsia_settings::LightGroup {
37 fn from(src: LightGroup) -> Self {
38 fidl_fuchsia_settings::LightGroup {
39 name: Some(src.name),
40 enabled: Some(src.enabled),
41 type_: Some(src.light_type.into()),
42 lights: Some(src.lights.into_iter().map(LightState::into).collect()),
43 ..Default::default()
44 }
45 }
46}
47
48impl From<fidl_fuchsia_settings::LightGroup> for LightGroup {
49 fn from(src: fidl_fuchsia_settings::LightGroup) -> Self {
50 LightGroup {
51 name: src.name.unwrap(),
52 enabled: src.enabled.unwrap(),
53 light_type: src.type_.unwrap().into(),
54 lights: src.lights.unwrap().into_iter().map(LightState::from).collect(),
55 hardware_index: vec![],
57 disable_conditions: vec![],
58 }
59 }
60}
61
62#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize)]
63pub enum LightType {
64 Brightness,
65 Rgb,
66 Simple,
67}
68
69impl From<fidl_fuchsia_settings::LightType> for LightType {
70 fn from(src: fidl_fuchsia_settings::LightType) -> Self {
71 match src {
72 fidl_fuchsia_settings::LightType::Brightness => LightType::Brightness,
73 fidl_fuchsia_settings::LightType::Rgb => LightType::Rgb,
74 fidl_fuchsia_settings::LightType::Simple => LightType::Simple,
75 }
76 }
77}
78
79impl From<LightType> for fidl_fuchsia_settings::LightType {
80 fn from(src: LightType) -> Self {
81 match src {
82 LightType::Brightness => fidl_fuchsia_settings::LightType::Brightness,
83 LightType::Rgb => fidl_fuchsia_settings::LightType::Rgb,
84 LightType::Simple => fidl_fuchsia_settings::LightType::Simple,
85 }
86 }
87}
88
89impl From<fidl_fuchsia_hardware_light::Capability> for LightType {
91 fn from(src: fidl_fuchsia_hardware_light::Capability) -> Self {
92 match src {
93 fidl_fuchsia_hardware_light::Capability::Brightness => LightType::Brightness,
94 fidl_fuchsia_hardware_light::Capability::Rgb => LightType::Rgb,
95 fidl_fuchsia_hardware_light::Capability::Simple => LightType::Simple,
96 }
97 }
98}
99
100impl From<LightType> for fidl_fuchsia_hardware_light::Capability {
102 fn from(src: LightType) -> Self {
103 match src {
104 LightType::Brightness => fidl_fuchsia_hardware_light::Capability::Brightness,
105 LightType::Rgb => fidl_fuchsia_hardware_light::Capability::Rgb,
106 LightType::Simple => fidl_fuchsia_hardware_light::Capability::Simple,
107 }
108 }
109}
110
111#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
112pub struct LightState {
113 pub value: Option<LightValue>,
114}
115
116impl LightState {
117 pub(crate) fn is_finite(&self) -> bool {
118 (self.value).as_ref().map_or(true, |val| val.is_finite())
119 }
120}
121
122impl From<fidl_fuchsia_settings::LightState> for LightState {
123 fn from(src: fidl_fuchsia_settings::LightState) -> Self {
124 LightState { value: src.value.map(LightValue::from) }
125 }
126}
127
128impl From<LightState> for fidl_fuchsia_settings::LightState {
129 fn from(src: LightState) -> Self {
130 fidl_fuchsia_settings::LightState {
131 value: src.value.map(fidl_fuchsia_settings::LightValue::from),
132 ..Default::default()
133 }
134 }
135}
136
137#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
138pub enum LightValue {
139 Brightness(f64),
140 Rgb(ColorRgb),
141 Simple(bool),
142}
143
144impl LightValue {
145 pub(crate) fn is_finite(&self) -> bool {
146 match self {
147 LightValue::Brightness(brightness) => brightness.is_finite(),
148 LightValue::Rgb(color_rgb) => color_rgb.is_finite(),
149 LightValue::Simple(_) => true,
150 }
151 }
152}
153
154impl From<fidl_fuchsia_settings::LightValue> for LightValue {
155 fn from(src: fidl_fuchsia_settings::LightValue) -> Self {
156 match src {
157 fidl_fuchsia_settings::LightValue::On(on) => LightValue::Simple(on),
158 fidl_fuchsia_settings::LightValue::Brightness(brightness) => {
159 LightValue::Brightness(brightness)
160 }
161 fidl_fuchsia_settings::LightValue::Color(color) => LightValue::Rgb(color.into()),
162 }
163 }
164}
165
166impl From<fidl_fuchsia_hardware_light::Rgb> for LightValue {
167 fn from(src: fidl_fuchsia_hardware_light::Rgb) -> Self {
168 LightValue::Rgb(ColorRgb {
169 red: src.red as f32,
170 green: src.green as f32,
171 blue: src.blue as f32,
172 })
173 }
174}
175
176impl From<LightValue> for fidl_fuchsia_settings::LightValue {
177 fn from(src: LightValue) -> Self {
178 match src {
179 LightValue::Simple(on) => fidl_fuchsia_settings::LightValue::On(on),
180 LightValue::Brightness(brightness) => {
181 fidl_fuchsia_settings::LightValue::Brightness(brightness)
182 }
183 LightValue::Rgb(color) => fidl_fuchsia_settings::LightValue::Color(color.into()),
184 }
185 }
186}
187
188#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
189pub struct ColorRgb {
190 pub red: f32,
191 pub green: f32,
192 pub blue: f32,
193}
194
195impl ColorRgb {
196 pub(crate) fn is_finite(&self) -> bool {
197 self.red.is_finite() && self.green.is_finite() && self.blue.is_finite()
198 }
199}
200
201impl From<fidl_fuchsia_ui_types::ColorRgb> for ColorRgb {
202 fn from(src: fidl_fuchsia_ui_types::ColorRgb) -> Self {
203 ColorRgb { red: src.red, green: src.green, blue: src.blue }
204 }
205}
206
207impl From<ColorRgb> for fidl_fuchsia_ui_types::ColorRgb {
208 fn from(src: ColorRgb) -> Self {
209 fidl_fuchsia_ui_types::ColorRgb { red: src.red, green: src.green, blue: src.blue }
210 }
211}
212
213impl TryFrom<ColorRgb> for fidl_fuchsia_hardware_light::Rgb {
216 type Error = &'static str;
217 fn try_from(src: ColorRgb) -> Result<Self, Self::Error> {
218 if src.red > 1.0
219 || src.green > 1.0
220 || src.blue > 1.0
221 || src.red < 0.0
222 || src.green < 0.0
223 || src.blue < 0.0
224 {
225 return Err("values must be between 0.0 and 1.0 inclusive");
226 }
227
228 Ok(fidl_fuchsia_hardware_light::Rgb {
229 red: src.red as f64,
230 green: src.green as f64,
231 blue: src.blue as f64,
232 })
233 }
234}
235
236#[cfg(test)]
237mod tests {
238 use crate::light::types::ColorRgb;
239
240 #[fuchsia::test]
241 fn test_try_from_rgb() {
242 assert!(fidl_fuchsia_hardware_light::Rgb::try_from(ColorRgb {
243 red: -0.0,
244 green: 0.1,
245 blue: 1.0
246 })
247 .is_ok());
248
249 assert!(fidl_fuchsia_hardware_light::Rgb::try_from(ColorRgb {
250 red: 0.0 - f32::EPSILON,
251 green: 0.1,
252 blue: 0.2
253 })
254 .is_err());
255
256 assert!(fidl_fuchsia_hardware_light::Rgb::try_from(ColorRgb {
257 red: 0.3,
258 green: 1.0 + f32::EPSILON,
259 blue: 0.2
260 })
261 .is_err());
262
263 assert_eq!(
264 fidl_fuchsia_hardware_light::Rgb::try_from(ColorRgb {
265 red: 0.0,
266 green: 1.0,
267 blue: 0.5
268 }),
269 Ok(fidl_fuchsia_hardware_light::Rgb { red: 0.0, green: 1.0, blue: 0.5 })
270 );
271 }
272}