-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.rs
74 lines (63 loc) · 1.79 KB
/
token.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
use crate::ParseError;
use crate::evdev_mappings;
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct KeyAttribute: u8 {
const None = 0b00000000;
const Send = 0b00000001;
const OnRelease = 0b00000010;
const Both = Self::Send.bits() | Self::OnRelease.bits();
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModifierRepr(pub String);
#[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)]
pub enum Modifier {
Super,
Alt,
Altgr,
Control,
Shift,
Any,
Omission,
}
impl From<ModifierRepr> for Modifier {
fn from(value: ModifierRepr) -> Self {
match value.0.to_lowercase().as_str() {
"ctrl" => Modifier::Control,
"control" => Modifier::Control,
"super" | "mod4" | "meta" => Modifier::Super,
"alt" => Modifier::Alt,
"mod1" => Modifier::Alt,
"altgr" => Modifier::Altgr,
"mod5" => Modifier::Altgr,
"shift" => Modifier::Shift,
"any" => Modifier::Any,
"_" => Modifier::Omission,
_ => panic!("{:?} is not a modifier", value),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Key {
pub key: evdev::Key,
pub attribute: KeyAttribute,
}
impl Key {
pub fn new(key: evdev::Key, attribute: KeyAttribute) -> Self {
Self { key, attribute }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyRepr {
pub key: String,
pub attribute: KeyAttribute,
}
impl TryFrom<KeyRepr> for Key {
type Error = ParseError;
fn try_from(value: KeyRepr) -> Result<Self, Self::Error> {
let key = evdev_mappings::convert(&value.key)?;
let attribute = value.attribute;
Ok(Self { key, attribute })
}
}