settings/keyboard/
types.rs1use serde::{Deserialize, Serialize};
6
7#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
8pub struct KeyboardInfo {
9 pub(crate) keymap: Option<KeymapId>,
10 pub(crate) autorepeat: Option<Autorepeat>,
11}
12
13impl KeyboardInfo {
14 pub(crate) fn is_valid(&self) -> bool {
15 self.autorepeat.map_or(true, |x| x.is_valid())
16 }
17}
18
19#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
20pub(crate) enum KeymapId {
21 UsQwerty,
24
25 FrAzerty,
27
28 UsDvorak,
30
31 UsColemak,
33}
34
35impl TryFrom<fidl_fuchsia_input::KeymapId> for KeymapId {
36 type Error = String;
37
38 fn try_from(src: fidl_fuchsia_input::KeymapId) -> Result<Self, Self::Error> {
39 match src {
40 fidl_fuchsia_input::KeymapId::UsQwerty => Ok(KeymapId::UsQwerty),
41 fidl_fuchsia_input::KeymapId::FrAzerty => Ok(KeymapId::FrAzerty),
42 fidl_fuchsia_input::KeymapId::UsDvorak => Ok(KeymapId::UsDvorak),
43 fidl_fuchsia_input::KeymapId::UsColemak => Ok(KeymapId::UsColemak),
44 _ => Err(format!("Received an invalid keymap id: {src:?}.")),
45 }
46 }
47}
48
49impl From<KeymapId> for fidl_fuchsia_input::KeymapId {
50 fn from(src: KeymapId) -> Self {
51 match src {
52 KeymapId::UsQwerty => fidl_fuchsia_input::KeymapId::UsQwerty,
53 KeymapId::FrAzerty => fidl_fuchsia_input::KeymapId::FrAzerty,
54 KeymapId::UsDvorak => fidl_fuchsia_input::KeymapId::UsDvorak,
55 KeymapId::UsColemak => fidl_fuchsia_input::KeymapId::UsColemak,
56 }
57 }
58}
59
60#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
61pub(crate) struct Autorepeat {
62 pub delay: i64,
65 pub period: i64,
68}
69
70impl Autorepeat {
71 pub(crate) fn is_valid(&self) -> bool {
72 self.delay >= 0 && self.period >= 0
73 }
74}
75
76impl From<fidl_fuchsia_settings::Autorepeat> for Autorepeat {
77 fn from(src: fidl_fuchsia_settings::Autorepeat) -> Self {
78 Autorepeat { delay: src.delay, period: src.period }
79 }
80}
81
82impl From<Autorepeat> for fidl_fuchsia_settings::Autorepeat {
83 fn from(src: Autorepeat) -> Self {
84 fidl_fuchsia_settings::Autorepeat { delay: src.delay, period: src.period }
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use crate::keyboard::types::KeymapId;
91
92 #[fuchsia::test]
93 fn test_try_from_keymapid() {
94 assert!(KeymapId::try_from(fidl_fuchsia_input::KeymapId::UsQwerty).is_ok());
95
96 assert!(KeymapId::try_from(fidl_fuchsia_input::KeymapId::unknown()).is_err());
97
98 assert_eq!(
99 KeymapId::try_from(fidl_fuchsia_input::KeymapId::FrAzerty).unwrap(),
100 KeymapId::FrAzerty
101 );
102 }
103}