settings/keyboard/
types.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use serde::{Deserialize, Serialize};

#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
pub struct KeyboardInfo {
    pub(crate) keymap: Option<KeymapId>,
    pub(crate) autorepeat: Option<Autorepeat>,
}

impl KeyboardInfo {
    pub(crate) fn is_valid(&self) -> bool {
        self.autorepeat.map_or(true, |x| x.is_valid())
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub(crate) enum KeymapId {
    /// The US_QWERTY keymap. This is also the default if no settings are
    /// ever applied.
    UsQwerty,

    /// The FR AZERTY keymap.
    FrAzerty,

    /// The US Dvorak keymap.
    UsDvorak,

    /// The US Colemak keymap.
    UsColemak,
}

impl TryFrom<fidl_fuchsia_input::KeymapId> for KeymapId {
    type Error = String;

    fn try_from(src: fidl_fuchsia_input::KeymapId) -> Result<Self, Self::Error> {
        match src {
            fidl_fuchsia_input::KeymapId::UsQwerty => Ok(KeymapId::UsQwerty),
            fidl_fuchsia_input::KeymapId::FrAzerty => Ok(KeymapId::FrAzerty),
            fidl_fuchsia_input::KeymapId::UsDvorak => Ok(KeymapId::UsDvorak),
            fidl_fuchsia_input::KeymapId::UsColemak => Ok(KeymapId::UsColemak),
            _ => Err(format!("Received an invalid keymap id: {src:?}.")),
        }
    }
}

impl From<KeymapId> for fidl_fuchsia_input::KeymapId {
    fn from(src: KeymapId) -> Self {
        match src {
            KeymapId::UsQwerty => fidl_fuchsia_input::KeymapId::UsQwerty,
            KeymapId::FrAzerty => fidl_fuchsia_input::KeymapId::FrAzerty,
            KeymapId::UsDvorak => fidl_fuchsia_input::KeymapId::UsDvorak,
            KeymapId::UsColemak => fidl_fuchsia_input::KeymapId::UsColemak,
        }
    }
}

#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
pub(crate) struct Autorepeat {
    /// The delay between key actuation and autorepeat actuation. Meaningful values are positive
    /// integers. Zero means the field has been cleared.
    pub delay: i64,
    /// The period between two successive autorepeat actuations (1/rate). Meaningful values are
    /// positive integers. Zero means the field has been cleared.
    pub period: i64,
}

impl Autorepeat {
    pub(crate) fn is_valid(&self) -> bool {
        self.delay >= 0 && self.period >= 0
    }
}

impl From<fidl_fuchsia_settings::Autorepeat> for Autorepeat {
    fn from(src: fidl_fuchsia_settings::Autorepeat) -> Self {
        Autorepeat { delay: src.delay, period: src.period }
    }
}

impl From<Autorepeat> for fidl_fuchsia_settings::Autorepeat {
    fn from(src: Autorepeat) -> Self {
        fidl_fuchsia_settings::Autorepeat { delay: src.delay, period: src.period }
    }
}

#[cfg(test)]
mod tests {
    use crate::keyboard::types::KeymapId;

    #[fuchsia::test]
    fn test_try_from_keymapid() {
        assert!(KeymapId::try_from(fidl_fuchsia_input::KeymapId::UsQwerty).is_ok());

        assert!(KeymapId::try_from(fidl_fuchsia_input::KeymapId::unknown()).is_err());

        assert_eq!(
            KeymapId::try_from(fidl_fuchsia_input::KeymapId::FrAzerty).unwrap(),
            KeymapId::FrAzerty
        );
    }
}