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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Contains the layout information

use std::fmt::{Binary, Display, Formatter, LowerExp, LowerHex, Octal, UpperExp, UpperHex};

#[cfg(feature = "serde-serialize")]
use serde::{Deserialize, Serialize};

mod layout_fr_ch;
pub use layout_fr_ch::*;

mod keypress;
pub use keypress::*;

mod key_name;
pub use key_name::*;

mod position;
pub use position::*;

/// Code for key light. This represent the key position in the buffer
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct KeyLight {
    /// Code representing the light position
    code: u8,
}

impl KeyLight {
    /// Create a new [`KeyLight`]
    pub const fn new(n: u8) -> Self {
        Self { code: n }
    }

    /// Get the key code.
    pub const fn code(self) -> u8 {
        self.code
    }

    /// Get the key code as mutbale.
    pub fn code_mut(&mut self) -> &mut u8 {
        &mut self.code
    }
}

impl Display for KeyLight {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.code())
    }
}

impl Binary for KeyLight {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:b}", self.code())
    }
}

impl UpperHex for KeyLight {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:X}", self.code())
    }
}

impl LowerHex for KeyLight {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:x}", self.code())
    }
}

impl Octal for KeyLight {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:o}", self.code())
    }
}

impl LowerExp for KeyLight {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:e}", self.code())
    }
}

impl UpperExp for KeyLight {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:E}", self.code())
    }
}

///  associative data for a key.
#[derive(Clone, Debug, PartialEq, PartialOrd, Default)]
pub struct KeyInfo {
    /// Name of the key
    key_name: KeyName,
    /// Light code of the key
    key_code_light: KeyLight,
    /// Code of key press events
    key_code_press: KeyCode,
    /// Position on the keyboard
    key_pos: Position,
}

impl KeyInfo {
    /// Create a new key info.
    pub const fn new(
        key_code_light: KeyLight,
        key_code_press: KeyCode,
        key_name: KeyName,
        key_pos: Position,
    ) -> Self {
        Self {
            key_name,
            key_code_light,
            key_code_press,
            key_pos,
        }
    }

    /// Get the key code ofr the led.
    pub const fn key_code_light(&self) -> &KeyLight {
        &self.key_code_light
    }

    /// Get the key code for press events
    pub const fn key_code_press(&self) -> &KeyCode {
        &self.key_code_press
    }

    /// Get the key name
    pub const fn key_name(&self) -> &KeyName {
        &self.key_name
    }

    /// Get the key code ofr the led.
    pub fn key_code_light_mut(&mut self) -> &mut KeyLight {
        &mut self.key_code_light
    }

    /// Get the key code for press events
    pub fn key_code_press_mut(&mut self) -> &mut KeyCode {
        &mut self.key_code_press
    }

    /// Get the key name
    pub fn key_name_mut(&mut self) -> &mut KeyName {
        &mut self.key_name
    }
}

/// Defines a Keyboard layout
pub trait Layout {
    /// returns the layout
    fn layout(&self) -> &[KeyInfo];

    /// Find key info from y [`KeyName`]
    fn find_from_key_name(&self, key_name: KeyName) -> Option<&KeyInfo> {
        self.layout().iter().find(|info| info.key_name == key_name)
    }

    /// Find key info from a [`KeyCode`]
    fn find_from_key_code(&self, key_code: KeyCode) -> Option<&KeyInfo> {
        self.layout()
            .iter()
            .find(|info| info.key_code_press == key_code)
    }

    /// Find key info from a [`KeyLight`]
    fn find_from_key_light(&self, key_code: KeyLight) -> Option<&KeyInfo> {
        self.layout()
            .iter()
            .find(|info| info.key_code_light == key_code)
    }
}