Skip to content

Commit 7ca8de9

Browse files
committed
add attr for enigo
1 parent a9f4c64 commit 7ca8de9

File tree

6 files changed

+841
-23
lines changed

6 files changed

+841
-23
lines changed

src/convert/_build.rs

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,26 @@ mod filters {
4242
}
4343
}
4444

45+
#[derive(Debug, Clone)]
46+
pub struct Entry<'a, S: 'a> {
47+
pub k: S,
48+
pub v: S,
49+
pub attr_k: Option<S>,
50+
pub attr_v: Option<S>,
51+
marked: std::marker::PhantomData<&'a ()>,
52+
}
53+
4554
#[derive(Debug, Clone, askama::Template)]
4655
#[template(ext = "txt", source = r#"// This file is auto-generated. Do not edit manually.
4756
4857
4958
pub fn {{from | lower}}_to_{{to | lower}}(value: {{from}}) -> Option<{{to}}> {
5059
let result = match value {
51-
{% for (k, v) in map -%}
52-
{{ k | pad_right(*k_len) }} => {{prefix}}{{ v | pad_right(*v_len) }}{{suffix}},
60+
{% for entry in entries -%}
61+
{% if let Some(attr) = entry.attr_k -%}
62+
{{ attr | indent(4) }}
63+
{% endif -%}
64+
{{ entry.k | pad_right(*k_len) }} => {{prefix}}{{ entry.v | pad_right(*v_len) }}{{suffix}},
5365
{% endfor -%}
5466
_ => return None,
5567
};
@@ -67,7 +79,7 @@ pub struct GeneralTemplate<'a> {
6779
pub to: &'a str,
6880
pub prefix: &'a str,
6981
pub suffix: &'a str,
70-
pub map: Vec<(Cow<'a, str>, Cow<'a, str>)>,
82+
pub entries: Vec<Entry<'a, Cow<'a, str>>>,
7183
pub k_len: usize,
7284
pub v_len: usize,
7385
}
@@ -81,26 +93,31 @@ impl<'a> GeneralTemplate<'a> {
8193
to,
8294
prefix,
8395
suffix,
84-
map: vec![],
96+
entries: vec![],
8597
k_len: 0,
8698
v_len: 0,
8799
}
88100
}
89101

90-
pub fn build(self, map: Vec<(Cow<'a, str>, Cow<'a, str>)>) -> String {
102+
pub fn build(self, map: Vec<Entry<'a, Cow<'a, str>>>) -> String {
91103
Self {
92-
k_len: map.iter().map(|(k, _)| k.len()).max().unwrap_or(0),
93-
v_len: map.iter().map(|(_, v)| v.len()).max().unwrap_or(0),
94-
map,
104+
k_len: map.iter().map(|e| e.k.len()).max().unwrap_or(0),
105+
v_len: map.iter().map(|e| e.v.len()).max().unwrap_or(0),
106+
entries: map,
95107
..self
96108
}.render().unwrap()
97109
}
98110
}
99111

112+
enum KeySourceType {
113+
Dep, Mirror,
114+
}
115+
100116
#[expect(unused)]
101117
#[derive(Debug, Clone, Copy)]
102118
enum KeyType {
103119
HUT, Winput, WinVk, VkValue, HutKeyboardValue, Enigo, KeySym, CG,
120+
EnigoDep, EnigoMirror,
104121
}
105122

106123
impl KeyType {
@@ -111,7 +128,7 @@ impl KeyType {
111128
KeyType::WinVk => "Vk",
112129
KeyType::VkValue => unimplemented!(),
113130
KeyType::HutKeyboardValue => unimplemented!(),
114-
KeyType::Enigo => "Enigo",
131+
KeyType::Enigo | KeyType::EnigoDep | KeyType::EnigoMirror => "Enigo",
115132
KeyType::KeySym => "KeySym",
116133
KeyType::CG => "CGKeyCode",
117134
}
@@ -124,12 +141,19 @@ impl KeyType {
124141
KeyType::WinVk => line.vk,
125142
KeyType::VkValue => line.vk_value,
126143
KeyType::HutKeyboardValue => line.hut_keyboard_value,
127-
KeyType::Enigo => line.enigo,
144+
KeyType::Enigo | KeyType::EnigoDep | KeyType::EnigoMirror => line.enigo,
128145
KeyType::KeySym => line.keysym,
129146
KeyType::CG => line.cg,
130147
}
131148
}
132149

150+
pub fn get_attr<'a>(self, line: &'a Line<&'a str>) -> Option<&'a str> {
151+
match self {
152+
KeyType::Enigo | KeyType::EnigoDep | KeyType::EnigoMirror => Some(line.enigo_attr),
153+
_ => None,
154+
}
155+
}
156+
133157
pub fn as_value_prefix(self) -> Option<&'static str> {
134158
match self {
135159
KeyType::WinVk => Some("keys::"),
@@ -154,17 +178,42 @@ impl KeyType {
154178
let s = s.trim_start();
155179
Cow::Borrowed(s.trim().trim_end_matches("*"))
156180
}
181+
182+
pub fn get_attr_unchecked<'a>(self, s: &'a str) -> Option<Cow<'a, str>> {
183+
match self {
184+
KeyType::EnigoDep => build_attr_for_target_os(s),
185+
KeyType::EnigoMirror => build_attr_for_for_target(s),
186+
_ => None,
187+
}
188+
}
157189
}
158190

159191

160192
#[derive(Debug, Clone, Copy)]
161193
struct Gen(KeyType, KeyType);
162194

163195
impl Gen {
164-
pub fn build_kv<'a>(self, csv: &'a [Line<&'a str>]) -> Vec<(Cow<'a, str>, Cow<'a, str>)> {
196+
pub fn build_entry<'a>(self, line: &'a Line<&'a str>) -> Option<Entry<'a, Cow<'a, str>>> {
165197
let from = self.0;
166198
let to = self.1;
167-
csv.iter().map(|i| (from.get_line(i), to.get_line(i))).filter(|t| self.kv_is_valid(t)).map(|(k, v)| (from.get_content_unchecked(k), to.get_content_unchecked(v))).collect()
199+
let k = from.get_line(line);
200+
let v = to.get_line(line);
201+
if !self.kv_is_valid(&(k, v)) {
202+
return None;
203+
}
204+
let attr = from.get_attr(line);
205+
let attr2 = to.get_attr(line);
206+
Some(Entry {
207+
k: from.get_content_unchecked(k),
208+
v: to.get_content_unchecked(v),
209+
attr_k: attr.and_then(|i| from.get_attr_unchecked(i)),
210+
attr_v: attr2.and_then(|i| to.get_attr_unchecked(i)),
211+
marked: std::marker::PhantomData,
212+
})
213+
}
214+
215+
pub fn build_kv<'a>(self, csv: &'a [Line<&'a str>]) -> Vec<Entry<'a, Cow<'a, str>>> {
216+
csv.iter().filter_map(|line| self.build_entry(line)).collect()
168217
}
169218

170219
pub fn kv_is_valid<K: AsRef<str>, V: AsRef<str>>(self, (k, v): &(K, V)) -> bool {
@@ -199,6 +248,34 @@ fn save_file<P: AsRef<Path>, S: AsRef<str>>(filename: P, content: S) -> std::io:
199248
std::fs::write(path, content)
200249
}
201250

251+
fn build_attr_for_target_os<'a>(a: &'a str) -> Option<Cow<'a, str>> {
252+
let a = a.trim();
253+
if a.is_empty() {
254+
return None;
255+
}
256+
let a = a.trim_start_matches("#[").trim_end_matches("]");
257+
let aa = a.split('|').map(|i| format!("target_os = {:?}", i.trim())).collect::<Vec<_>>();
258+
if aa.len() == 1 {
259+
Some(Cow::Owned(format!("#[cfg({})]", aa[0])))
260+
} else {
261+
Some(Cow::Owned(format!("#[cfg(any({}))]", aa.join(", "))))
262+
}
263+
}
264+
265+
fn build_attr_for_for_target<'a>(a: &'a str) -> Option<Cow<'a, str>> {
266+
let a = a.trim();
267+
if a.is_empty() {
268+
return None;
269+
}
270+
let a = a.trim_start_matches("#[").trim_end_matches("]");
271+
let aa = a.split('|').map(|i| format!("for_{}", i.trim())).collect::<Vec<_>>();
272+
if aa.len() == 1 {
273+
Some(Cow::Owned(format!("#[cfg({})]", aa[0])))
274+
} else {
275+
Some(Cow::Owned(format!("#[cfg(any({}))]", aa.join(", "))))
276+
}
277+
}
278+
202279
pub fn main() {
203280
if std::env::var("DOCS_RS").is_ok() {
204281
return;
@@ -213,10 +290,11 @@ pub fn main() {
213290
.map(|i| i.split(',').collect::<Vec<_>>().into()).collect::<Vec<Line<_>>>();
214291

215292
for tuple in [
216-
(KeyType::Enigo, KeyType::WinVk),
217-
(KeyType::Enigo, KeyType::Winput),
293+
(KeyType::EnigoMirror, KeyType::WinVk),
294+
(KeyType::EnigoDep, KeyType::WinVk),
295+
(KeyType::EnigoMirror, KeyType::Winput),
218296
(KeyType::Winput, KeyType::HUT),
219-
(KeyType::Winput, KeyType::Enigo),
297+
(KeyType::Winput, KeyType::EnigoMirror),
220298
(KeyType::Winput, KeyType::CG),
221299
] {
222300
let (from, to) = tuple;

0 commit comments

Comments
 (0)