settings/keyboard/
types.rs

1// Copyright 2021 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 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    /// The US_QWERTY keymap. This is also the default if no settings are
22    /// ever applied.
23    UsQwerty,
24
25    /// The FR AZERTY keymap.
26    FrAzerty,
27
28    /// The US Dvorak keymap.
29    UsDvorak,
30
31    /// The US Colemak keymap.
32    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    /// The delay between key actuation and autorepeat actuation. Meaningful values are positive
63    /// integers. Zero means the field has been cleared.
64    pub delay: i64,
65    /// The period between two successive autorepeat actuations (1/rate). Meaningful values are
66    /// positive integers. Zero means the field has been cleared.
67    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}