-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.rs
61 lines (57 loc) · 1.54 KB
/
config.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
use crate::consts;
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
#[derive(Serialize, Deserialize, Debug)]
pub struct Keymap {
pub version: u8,
pub keyboard: String,
pub keymap: String,
pub layout: String,
pub layers: Vec<Vec<String>>,
}
impl Keymap {
pub fn new(keyboard: String, keymap: Vec<Vec<u8>>) -> Self {
Self {
version: 1,
keyboard,
keymap: "default".to_string(),
layout: "LAYOUT".to_string(),
layers: layers_from_keymap(keymap),
}
}
pub fn encode(&self) -> serde_json::Result<String> {
serde_json::to_string(self)
}
pub fn decode(keymap: &str) -> serde_json::Result<Self> {
serde_json::from_str(keymap)
}
}
pub fn layers_from_keymap(keymap: Vec<Vec<u8>>) -> Vec<Vec<String>> {
keymap
.iter()
.map(|layer| {
layer
.iter()
.map(|&keycode| consts::KEY_CODE_NAME[keycode as usize].to_string())
.collect()
})
.collect()
}
pub fn keymap_from_layers(layers: Vec<Vec<String>>) -> Vec<Vec<u8>> {
layers
.iter()
.map(|layer| {
layer
.iter()
.map(|name| {
consts::KEY_CODE_NAME
.iter()
.position(|x| x == name)
.unwrap_or(0)
.try_into()
.unwrap()
})
.collect()
})
.collect()
}