mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
feat(input): add defcom! macro
This commit is contained in:
parent
fa6f6dab1d
commit
3861439c49
1 changed files with 16 additions and 0 deletions
|
|
@ -1,4 +1,18 @@
|
||||||
use crate::*;
|
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 {
|
#[macro_export] macro_rules! command {
|
||||||
($(<$($l:lifetime),+>)?|$self:ident:$Command:ty,$state:ident:$State:ty|$handler:expr) => {
|
($(<$($l:lifetime),+>)?|$self:ident:$Command:ty,$state:ident:$State:ty|$handler:expr) => {
|
||||||
impl$(<$($l),+>)? Command<$State> for $Command {
|
impl$(<$($l),+>)? Command<$State> for $Command {
|
||||||
|
|
@ -8,6 +22,7 @@ use crate::*;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Command<S>: Send + Sync + Sized {
|
pub trait Command<S>: Send + Sync + Sized {
|
||||||
fn execute (self, state: &mut S) -> Perhaps<Self>;
|
fn execute (self, state: &mut S) -> Perhaps<Self>;
|
||||||
fn delegate <T> (self, state: &mut S, wrap: impl Fn(Self)->T) -> Perhaps<T>
|
fn delegate <T> (self, state: &mut S, wrap: impl Fn(Self)->T) -> Perhaps<T>
|
||||||
|
|
@ -16,6 +31,7 @@ pub trait Command<S>: Send + Sync + Sized {
|
||||||
Ok(self.execute(state)?.map(wrap))
|
Ok(self.execute(state)?.map(wrap))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, T: Command<S>> Command<S> for Option<T> {
|
impl<S, T: Command<S>> Command<S> for Option<T> {
|
||||||
fn execute (self, _: &mut S) -> Perhaps<Self> {
|
fn execute (self, _: &mut S) -> Perhaps<Self> {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue