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
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::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct KeyLight {
code: u8,
}
impl KeyLight {
pub const fn new(n: u8) -> Self {
Self { code: n }
}
pub const fn code(self) -> u8 {
self.code
}
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())
}
}
#[derive(Clone, Debug, PartialEq, PartialOrd, Default)]
pub struct KeyInfo {
key_name: KeyName,
key_code_light: KeyLight,
key_code_press: KeyCode,
key_pos: Position,
}
impl KeyInfo {
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,
}
}
pub const fn key_code_light(&self) -> &KeyLight {
&self.key_code_light
}
pub const fn key_code_press(&self) -> &KeyCode {
&self.key_code_press
}
pub const fn key_name(&self) -> &KeyName {
&self.key_name
}
pub fn key_code_light_mut(&mut self) -> &mut KeyLight {
&mut self.key_code_light
}
pub fn key_code_press_mut(&mut self) -> &mut KeyCode {
&mut self.key_code_press
}
pub fn key_name_mut(&mut self) -> &mut KeyName {
&mut self.key_name
}
}
pub trait Layout {
fn layout(&self) -> &[KeyInfo];
fn find_from_key_name(&self, key_name: KeyName) -> Option<&KeyInfo> {
self.layout().iter().find(|info| info.key_name == key_name)
}
fn find_from_key_code(&self, key_code: KeyCode) -> Option<&KeyInfo> {
self.layout()
.iter()
.find(|info| info.key_code_press == key_code)
}
fn find_from_key_light(&self, key_code: KeyLight) -> Option<&KeyInfo> {
self.layout()
.iter()
.find(|info| info.key_code_light == key_code)
}
}