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
use crate::platform::{KeyModifier, KeySpecial};
use crate::Key;
use nom::branch::alt;
use nom::bytes::complete::{tag, tag_no_case};
use nom::character::complete::alphanumeric1;
use nom::combinator::map;
use nom::multi::separated_list1;
use nom::IResult;
use std::str::FromStr;
fn parse_alt_key(i: &str) -> IResult<&str, KeyModifier> {
let parser = tag_no_case(KeyModifier::ALT.into());
map(parser, |t: &str| KeyModifier::from_str(t).unwrap())(i)
}
fn parse_ctrl_key(i: &str) -> IResult<&str, KeyModifier> {
let parser = alt((
tag_no_case("ctrl"),
tag_no_case(KeyModifier::CONTROL.into()),
));
map(parser, |t: &str| KeyModifier::from_str(t).unwrap())(i)
}
fn parse_super_key(i: &str) -> IResult<&str, KeyModifier> {
let parser = tag_no_case(KeyModifier::SUPER.into());
map(parser, |t: &str| KeyModifier::from_str(t).unwrap())(i)
}
fn parse_modifier_key(i: &str) -> IResult<&str, KeyModifier> {
alt((parse_alt_key, parse_ctrl_key, parse_super_key))(i)
}
fn parse_key(i: &str) -> IResult<&str, Key> {
let modifier_parser = map(parse_modifier_key, Key::Modifier);
let key_parser = map(alphanumeric1, |s: &str| -> Key {
if s.len() == 1 {
return Key::Alphanumeric(s.chars().next().unwrap());
}
if let Ok(special_key) = KeySpecial::from_str(s) {
return Key::Special(special_key);
}
panic!("Not a valid special key or alphanumeric key: {}", s);
});
alt((modifier_parser, key_parser))(i)
}
pub fn parse_key_string(i: &str) -> IResult<&str, Vec<Key>> {
separated_list1(tag("+"), parse_key)(i)
}