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
|
use std::{collections::HashMap, fmt::Display, ops::Deref, str::FromStr};
use anyhow::Context;
use keymaps::{key_repr::Key, map_tree::MapTrie};
use serde::{Deserialize, Serialize};
pub mod commands;
#[derive(Deserialize, Serialize, Debug)]
#[allow(clippy::module_name_repetitions)]
pub struct RawKeyMap(HashMap<Key, KeyConfig>);
#[derive(Clone, Deserialize, Serialize, Debug, PartialEq, PartialOrd)]
/// What values to use for: `riverctl <map_mode> <mode> <mods> <key> <command..>`
pub struct KeyConfig {
command: Vec<String>,
#[serde(default = "default_mode")]
modes: Vec<String>,
#[serde(default = "MapMode::default")]
map_mode: MapMode,
}
impl FromStr for KeyMap {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let raw: RawKeyMap =
serde_json::from_str(s).context("Failed to parse the keymap config file as json.")?;
let mut out = MapTrie::<KeyConfig>::new();
for (key, value) in raw.0 {
out.insert(&[key], value.clone())
.with_context(|| format!("Failed to insert mapping {key} -> {value}"))?;
}
Ok(Self(out))
}
}
impl Display for KeyConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.command.join(" ").as_str())
}
}
fn default_mode() -> Vec<String> {
vec!["normal".to_owned()]
}
#[derive(Copy, Deserialize, Serialize, Debug, Clone, Default, PartialEq, PartialOrd)]
enum MapMode {
#[default]
Map,
MapMouse,
Unmap,
}
impl Display for MapMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
<Self as std::fmt::Debug>::fmt(self, f)
}
}
#[derive(Debug)]
pub struct KeyMap(MapTrie<KeyConfig>);
impl Display for KeyMap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Deref for KeyMap {
type Target = MapTrie<KeyConfig>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
|