diff --git a/input/input.rs b/input/input.rs index 2695a47..5a22800 100644 --- a/input/input.rs +++ b/input/input.rs @@ -26,8 +26,8 @@ flex_trait_mut!(Handle { }); pub trait Command: Send + Sync + Sized { - fn execute (self, state: &mut S) -> Perhaps; - fn delegate (self, state: &mut S, wrap: impl Fn(Self)->T) -> Perhaps + fn execute (&self, state: &mut S) -> Perhaps; + fn delegate (&self, state: &mut S, wrap: impl Fn(Self)->T) -> Perhaps where Self: Sized { Ok(self.execute(state)?.map(wrap)) @@ -35,10 +35,10 @@ pub trait Command: Send + Sync + Sized { } impl> Command for Option { - fn execute (self, _: &mut S) -> Perhaps { + fn execute (&self, _: &mut S) -> Perhaps { Ok(None) } - fn delegate (self, _: &mut S, _: impl Fn(Self)->U) -> Perhaps + fn delegate (&self, _: &mut S, _: impl Fn(Self)->U) -> Perhaps where Self: Sized { Ok(None) @@ -49,13 +49,30 @@ impl> Command for Option { #[macro_export] macro_rules! command { ($(<$($l:lifetime),+>)?|$self:ident:$Command:ty,$state:ident:$State:ty|$handler:expr) => { impl$(<$($l),+>)? ::tengri::input::Command<$State> for $Command { - fn execute ($self, $state: &mut $State) -> Perhaps { + fn execute (&$self, $state: &mut $State) -> Perhaps { Ok($handler) } } }; } +#[macro_export] macro_rules! def_command (($Command:ident: |$state:ident: $State:ty| { + $($Variant:ident$({$($arg:ident:$Arg:ty),+ $(,)?})?=>$body:expr),* $(,)? +})=>{ + #[derive(Debug)] + pub enum $Command { + $($Variant $({ $($arg: $Arg),* })?),* + } + impl Command<$State> for $Command { + fn execute (&self, $state: &mut $State) -> Perhaps { + match self { + $(Self::$Variant $({ $($arg),* })? => $body,)* + _ => unimplemented!("Command<{}>: {self:?}", stringify!($State)), + } + } + } +}); + /// Implement [Handle] for given `State` and `handler`. #[macro_export] macro_rules! handle { (|$self:ident:$State:ty,$input:ident|$handler:expr) => {