Skip to content

Commit abfe488

Browse files
committed
generate index
1 parent 7ca8de9 commit abfe488

File tree

9 files changed

+1043
-95
lines changed

9 files changed

+1043
-95
lines changed

scripts/rust_gen_convert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def fix_macos_impl_value(v: str):
188188
# if v.startswith("ANSI_"): return "KeyCode::" + v
189189
if v.startswith("return Err(())"): return None
190190
if v.startswith("ANSI_"): return "KeyCodeExt::kVK_ANSI_" + v.removeprefix("ANSI_").title().replace("_", "")
191-
try: return f"KeyCode::from({int(v)})"
191+
try: return f"CGKeyCode::from({int(v)})"
192192
except: pass
193193
return v
194194
key_map_enigo_cg = {k.replace("Key::", "Enigo::"): fix_macos_impl_value(v) for k,v in data if "Key::" in k and "(" not in k} | \

src/convert/_build.rs

Lines changed: 128 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,41 @@ impl<'a> GeneralTemplate<'a> {
109109
}
110110
}
111111

112-
enum KeySourceType {
113-
Dep, Mirror,
112+
#[derive(Debug, Clone)]
113+
pub struct ImportEntry {
114+
pub gate: &'static str,
115+
pub import: &'static str,
116+
}
117+
118+
impl ImportEntry {
119+
pub fn new(gate: &'static str, import: &'static str) -> Self {
120+
Self { gate, import }
121+
}
122+
}
123+
124+
#[derive(Debug, Clone, askama::Template)]
125+
#[template(ext = "txt", source = r#"
126+
{% for f in from_imports -%}
127+
{% let i = loop.index -%}
128+
{% for t in to_imports -%}
129+
{% let j = loop.index %}
130+
#[cfg(all({{f.gate}}, {{t.gate}}))]
131+
mod {{from_source|lower}}_to_{{to_source|lower}}_{{i}}_{{j}} {
132+
{{f.import|indent(4)}}
133+
{{t.import|indent(4)}}
134+
include!("{{filename}}");
135+
}
136+
{% endfor -%}
137+
{% endfor -%}
138+
"#)]
139+
pub struct ImportTemplate {
140+
pub filename: String,
141+
pub from: &'static str,
142+
pub to: &'static str,
143+
pub from_source: &'static str,
144+
pub to_source: &'static str,
145+
pub from_imports: Vec<ImportEntry>,
146+
pub to_imports: Vec<ImportEntry>,
114147
}
115148

116149
#[expect(unused)]
@@ -134,6 +167,41 @@ impl KeyType {
134167
}
135168
}
136169

170+
pub fn import_mirror(self) -> Vec<ImportEntry> {
171+
match self {
172+
KeyType::Winput => vec![
173+
ImportEntry::new("mirror_winput_vk", "use crate::mirror::winput::Vk;"),
174+
],
175+
KeyType::WinVk => vec![
176+
ImportEntry::new("mirror_windows_vk", "use crate::mirror::windows::{self as keys, VIRTUAL_KEY as Vk};"),
177+
],
178+
KeyType::EnigoMirror => vec![
179+
ImportEntry::new("mirror_enigo", "use crate::mirror::enigo::Key as Enigo;"),
180+
],
181+
KeyType::KeySym => unimplemented!(),
182+
KeyType::CG => vec![
183+
ImportEntry::new("any(dep_macos, mirror_macos)", "#[cfg(dep_macos)]\nuse crate::deps::macos::KeyCode;\n#[cfg(not(dep_macos))]\n#[cfg(mirror_macos)]\nuse crate::mirror::macos::KeyCode;\nuse crate::mirror::macos_ext::{CGKeyCode, KeyCodeExt};"),
184+
],
185+
_ => return vec![],
186+
}
187+
}
188+
189+
pub fn import_dep(self) -> Vec<ImportEntry> {
190+
match self {
191+
KeyType::HUT => vec![
192+
ImportEntry::new("dep_hut_04", "use crate::deps::hut_04::{AsUsage, Button, Consumer, GenericDesktop, KeyboardKeypad, Usage};"),
193+
ImportEntry::new("dep_hut_03", "use crate::deps::hut_03::{AsUsage, Button, Consumer, GenericDesktop, KeyboardKeypad, Usage};"),
194+
],
195+
KeyType::Winput => vec![],
196+
KeyType::WinVk => vec![
197+
ImportEntry::new("dep_windows_vk", "use crate::deps::windows::{self as keys, VIRTUAL_KEY as Vk};"),
198+
],
199+
KeyType::EnigoDep => vec![ImportEntry::new("dep_enigo", "use crate::deps::enigo::Key as Enigo;")],
200+
KeyType::KeySym => unimplemented!(),
201+
_ => return vec![],
202+
}
203+
}
204+
137205
pub fn get_line<'a>(self, line: &'a Line<&'a str>) -> &'a str {
138206
match self {
139207
KeyType::HUT => line.hut,
@@ -227,6 +295,52 @@ impl Gen {
227295
GeneralTemplate::create(from.name(), to.name(), to.as_value_prefix(), to.as_value_suffix())
228296
.build(map)
229297
}
298+
299+
pub fn build_imports(self, filename: &str) -> String {
300+
let (from, to) = (self.0, self.1);
301+
let content = vec![
302+
ImportTemplate {
303+
filename: filename.to_string(),
304+
from: from.name(),
305+
to: to.name(),
306+
from_source: "mirror",
307+
to_source: "mirror",
308+
from_imports: from.import_mirror(),
309+
to_imports: to.import_mirror(),
310+
}.render().unwrap(),
311+
ImportTemplate {
312+
filename: filename.to_string(),
313+
from: from.name(),
314+
to: to.name(),
315+
from_source: "mirror",
316+
to_source: "dep",
317+
from_imports: from.import_mirror(),
318+
to_imports: to.import_dep(),
319+
}.render().unwrap(),
320+
ImportTemplate {
321+
filename: filename.to_string(),
322+
from: from.name(),
323+
to: to.name(),
324+
from_source: "dep",
325+
to_source: "mirror",
326+
from_imports: from.import_dep(),
327+
to_imports: to.import_mirror(),
328+
}.render().unwrap(),
329+
ImportTemplate {
330+
filename: filename.to_string(),
331+
from: from.name(),
332+
to: to.name(),
333+
from_source: "dep",
334+
to_source: "dep",
335+
from_imports: from.import_dep(),
336+
to_imports: to.import_dep(),
337+
}.render().unwrap(),
338+
].into_iter().filter(|i| !i.trim().is_empty()).map(|i| i.trim_end().trim_start_matches('\n').to_string()).collect::<Vec<_>>().join("\n");
339+
if content.trim().is_empty() {
340+
return String::new();
341+
}
342+
format!("mod generated_{}_to_{} {{\n {}\n}}\n\n", format!("{:?}", from).to_lowercase(), format!("{:?}", to).to_lowercase(), content.trim())
343+
}
230344
}
231345

232346
fn save_file<P: AsRef<Path>, S: AsRef<str>>(filename: P, content: S) -> std::io::Result<()> {
@@ -289,18 +403,27 @@ pub fn main() {
289403
let csv = csv_content.lines().filter(|i| !i.trim().is_empty() && !i.trim().starts_with(";"))
290404
.map(|i| i.split(',').collect::<Vec<_>>().into()).collect::<Vec<Line<_>>>();
291405

406+
let mut index_mod = String::new();
292407
for tuple in [
293-
(KeyType::EnigoMirror, KeyType::WinVk),
294-
(KeyType::EnigoDep, KeyType::WinVk),
295-
(KeyType::EnigoMirror, KeyType::Winput),
296408
(KeyType::Winput, KeyType::HUT),
297409
(KeyType::Winput, KeyType::EnigoMirror),
410+
(KeyType::Winput, KeyType::EnigoDep),
411+
(KeyType::EnigoMirror, KeyType::Winput),
412+
(KeyType::EnigoDep, KeyType::Winput),
413+
(KeyType::EnigoMirror, KeyType::WinVk),
414+
(KeyType::EnigoDep, KeyType::WinVk),
415+
(KeyType::EnigoMirror, KeyType::CG),
416+
(KeyType::EnigoDep, KeyType::CG),
298417
(KeyType::Winput, KeyType::CG),
299418
] {
300419
let (from, to) = tuple;
301420
let filename = format!("generated.{from:?}_to_{to:?}.rs");
302421
let content = Gen(from, to).build_general(&csv);
303422
save_file(format!("{output_path}/{filename}"), content)
304423
.expect("Failed to write generated.rs");
424+
425+
let imports_str = Gen(from, to).build_imports(&filename);
426+
index_mod.push_str(&imports_str);
305427
}
428+
save_file(format!("{output_path}/generated._index.rs"), index_mod).expect("failed to write index.rs");
306429
}

src/convert/convert.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@
176176
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::IlluminationDown , #[macos] , todo!(Keysym) , todo!(CG) ,
177177
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::IlluminationToggle , #[macos] , todo!(Keysym) , todo!(CG) ,
178178
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::IlluminationUp , #[macos] , todo!(Keysym) , todo!(CG) ,
179-
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::Launchpad , #[macos] , todo!(Keysym) , KeyCode::from(131) ,
179+
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::Launchpad , #[macos] , todo!(Keysym) , CGKeyCode::from(131) ,
180180
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::LaunchPanel , #[macos] , todo!(Keysym) , todo!(CG) ,
181181
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::Linefeed , #[linux] , Keysym::Linefeed , todo!(CG) ,
182182
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::MediaFast , #[macos] , todo!(Keysym) , todo!(CG) ,
183183
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::MediaRewind , #[macos] , todo!(Keysym) , todo!(CG) ,
184184
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::MicMute , #[linux] , Keysym::XF86_AudioMicMute , todo!(CG) ,
185-
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::MissionControl , #[macos] , todo!(Keysym) , KeyCode::from(160) ,
185+
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::MissionControl , #[macos] , todo!(Keysym) , CGKeyCode::from(160) ,
186186
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::Power , #[macos] , todo!(Keysym) , todo!(CG) ,
187187
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::RCommand , #[macos] , todo!(Keysym) , KeyCode::RIGHT_COMMAND ,
188188
todo!(HUT) , na!(Vk) , todo!(VK_) , n!() , n!() , Enigo::Redo , #[linux] , Keysym::Redo , todo!(CG) ,

0 commit comments

Comments
 (0)