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