@@ -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)]\n use crate::deps::macos::KeyCode;\n #[cfg(not(dep_macos))]\n #[cfg(mirror_macos)]\n use crate::mirror::macos::KeyCode;\n use 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
232346fn 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}
0 commit comments