-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefinition.rs
114 lines (106 loc) · 3.61 KB
/
definition.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
104
105
106
107
108
109
110
111
112
113
114
use itertools::Itertools;
use pest::iterators::Pair;
use crate::{
pair_to_string, parse_key,
range::Bounds,
token::{Key, KeyAttribute, Modifier},
KeyRepr, ModifierRepr, ParseError, Rule,
};
use std::{collections::BTreeSet, fmt::Display};
#[derive(Debug, PartialEq, Eq)]
pub struct Definition {
pub modifiers: BTreeSet<Modifier>,
pub key: Key,
}
impl Definition {
pub fn new(key: evdev::Key) -> Self {
Self {
modifiers: BTreeSet::default(),
key: Key::new(key, KeyAttribute::None),
}
}
pub fn with_modifiers(mut self, modifiers: &[Modifier]) -> Self {
self.modifiers = modifiers.iter().cloned().collect();
self
}
}
impl Display for Definition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[")?;
for modifier in self.modifiers.iter() {
write!(f, "{:?}, ", modifier)?;
}
write!(f, "{:?}", self.key)?;
write!(f, "]")
}
}
#[derive(Default)]
pub struct DefinitionUncompiled {
pub modifiers: Vec<Vec<Modifier>>,
pub keys: Vec<Key>,
}
impl DefinitionUncompiled {
pub fn ingest(&mut self, component: Pair<'_, Rule>) -> Result<(), ParseError> {
match component.as_rule() {
Rule::modifier => {
self.modifiers.push(vec![
ModifierRepr(pair_to_string(component).to_lowercase()).into()
])
}
Rule::modifier_shorthand | Rule::modifier_omit_shorthand => self.modifiers.push(
component
.into_inner()
.map(|component| ModifierRepr(pair_to_string(component)).into())
.collect(),
),
Rule::shorthand => {
for shorthand_component in component.into_inner() {
match shorthand_component.as_rule() {
Rule::key_in_shorthand => {
self.keys.push(parse_key(shorthand_component).try_into()?)
}
Rule::key_range => {
let (lower_bound, upper_bound) =
Bounds::new(shorthand_component).expand_keys()?;
let keys = (lower_bound..=upper_bound)
.map(|key| {
KeyRepr {
key: key.to_string(),
attribute: KeyAttribute::None,
}
.try_into()
})
.collect::<Result<Vec<Key>, ParseError>>()?;
self.keys.extend(keys);
}
_ => {}
}
}
}
Rule::key_normal => self.keys.push(parse_key(component).try_into()?),
_ => {}
};
Ok(())
}
pub fn compile(self) -> Vec<Definition> {
if self.modifiers.is_empty() {
return self
.keys
.into_iter()
.map(|key| Definition {
modifiers: BTreeSet::default(),
key,
})
.collect();
}
self.modifiers
.into_iter()
.multi_cartesian_product()
.cartesian_product(self.keys)
.map(|(modifiers, key)| Definition {
modifiers: modifiers.into_iter().collect(),
key,
})
.collect()
}
}