Skip to main content

input_device_constants/
lib.rs

1// Copyright 2022 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 schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8// LINT.IfChange
9/// Generic types of supported input devices.
10#[derive(
11    Debug, Clone, Copy, Eq, Hash, PartialEq, PartialOrd, Ord, Deserialize, Serialize, JsonSchema,
12)]
13#[serde(rename_all = "lowercase")]
14pub enum InputDeviceType {
15    Keyboard,
16    LightSensor,
17    #[serde(rename = "button")]
18    ConsumerControls,
19    Mouse,
20    #[serde(rename = "touchscreen")]
21    Touch,
22}
23// LINT.ThenChange(/build/bazel_sdk/bazel_rules_fuchsia/fuchsia/private/assembly/fuchsia_product_configuration.bzl)
24
25impl InputDeviceType {
26    /// Parses a list of supported `InputDeviceType`s from a structured configuration
27    /// `supported_input_devices` list. Unknown device types are logged and skipped.
28    pub fn list_from_structured_config_list<'a, V, T>(list: V) -> Vec<Self>
29    where
30        V: IntoIterator<Item = &'a T>,
31        T: AsRef<str> + 'a,
32    {
33        list.into_iter()
34            .filter_map(|device| match device.as_ref() {
35                "button" => Some(InputDeviceType::ConsumerControls),
36                "keyboard" => Some(InputDeviceType::Keyboard),
37                "lightsensor" => Some(InputDeviceType::LightSensor),
38                "mouse" => Some(InputDeviceType::Mouse),
39                "touchscreen" => Some(InputDeviceType::Touch),
40                _ => {
41                    log::warn!("Ignoring unsupported device configuration: {}", device.as_ref());
42                    None
43                }
44            })
45            .collect()
46    }
47}
48
49impl std::fmt::Display for InputDeviceType {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        write!(f, "{:?}", self)
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn input_device_list_from_structured_config_list() {
61        let config = vec![
62            "touchscreen".to_string(),
63            "button".to_string(),
64            "keyboard".to_string(),
65            "mouse".to_string(),
66            "hamster".to_string(),
67            "lightsensor".to_string(),
68        ];
69        let expected = vec![
70            InputDeviceType::Touch,
71            InputDeviceType::ConsumerControls,
72            InputDeviceType::Keyboard,
73            InputDeviceType::Mouse,
74            InputDeviceType::LightSensor,
75        ];
76        let actual = InputDeviceType::list_from_structured_config_list(&config);
77        assert_eq!(actual, expected);
78    }
79
80    #[test]
81    fn input_device_list_from_structured_config_list_strs() {
82        let config = ["hamster", "button", "keyboard", "mouse"];
83        let expected = vec![
84            InputDeviceType::ConsumerControls,
85            InputDeviceType::Keyboard,
86            InputDeviceType::Mouse,
87        ];
88        let actual = InputDeviceType::list_from_structured_config_list(&config);
89        assert_eq!(actual, expected);
90    }
91}