From 3861439c499313bc3c40eaf77678702b02804f12 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Thu, 24 Apr 2025 19:33:05 +0300 Subject: [PATCH] feat(input): add defcom! macro --- input/src/command.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/input/src/command.rs b/input/src/command.rs index 23b2464..d15f78a 100644 --- a/input/src/command.rs +++ b/input/src/command.rs @@ -1,4 +1,18 @@ use crate::*; + +#[macro_export] macro_rules! defcom { + (|$self:ident, $app:ident:$App:ty| $($Command:ident { $( + $Variant:ident $(($($param:ident: $Param:ty),+))? => $expr:expr + )* $(,)? })*) => { + $(#[derive(Clone, Debug)] pub enum $Command { + $($Variant $(($($Param),+))?),* + })* + $(command!(|$self: $Command, $app: $App|match $self { + $($Command::$Variant $(($($param),+))? => $expr),* + });)* + } +} + #[macro_export] macro_rules! command { ($(<$($l:lifetime),+>)?|$self:ident:$Command:ty,$state:ident:$State:ty|$handler:expr) => { impl$(<$($l),+>)? Command<$State> for $Command { @@ -8,6 +22,7 @@ use crate::*; } }; } + 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 @@ -16,6 +31,7 @@ pub trait Command: Send + Sync + Sized { Ok(self.execute(state)?.map(wrap)) } } + impl> Command for Option { fn execute (self, _: &mut S) -> Perhaps { Ok(None)