I am writing a macro which creates a struct managing user input. I am using the crates bitflags and sdl2. Return is an example for the key Return.
This macro takes a list of all possible inputs and then
($($flag:ident = $value:expr;)+) => { ... }
Creates a new bitflag with the name of the input
bitflags!( struct KeyType: u64 { $( const $flag = $value;// UPPER_CASE is the norm for globals: 'RETURN' )+ } );Checks if the key is pressed, using the
Keycodeenum.match event { $(Event::KeyDown { keycode: Some(Keycode::$flag), .. } => { self.flags.insert($flag); },)+ _ => () }// All enum fields start with a capital letter: 'Return'Creates a getter function:
$( pub fn $flag(&self) -> bool { // lower_case is the norm for functions: 'return' self.flags.contains($flag) } )+
Can I adapt $flag to fit all three requirements (flag, Flag, FLAG)?