mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
28 lines
855 B
Rust
28 lines
855 B
Rust
use crate::*;
|
|
#[macro_export] macro_rules! command {
|
|
($(<$($l:lifetime),+>)?|$self:ident:$Command:ty,$state:ident:$State:ty|$handler:expr) => {
|
|
impl$(<$($l),+>)? Command<$State> for $Command {
|
|
fn execute ($self, $state: &mut $State) -> Perhaps<Self> {
|
|
Ok($handler)
|
|
}
|
|
}
|
|
};
|
|
}
|
|
pub trait Command<S>: Send + Sync + Sized {
|
|
fn execute (self, state: &mut S) -> Perhaps<Self>;
|
|
fn delegate <T> (self, state: &mut S, wrap: impl Fn(Self)->T) -> Perhaps<T>
|
|
where Self: Sized
|
|
{
|
|
Ok(self.execute(state)?.map(wrap))
|
|
}
|
|
}
|
|
impl<S, T: Command<S>> Command<S> for Option<T> {
|
|
fn execute (self, _: &mut S) -> Perhaps<Self> {
|
|
Ok(None)
|
|
}
|
|
fn delegate <U> (self, _: &mut S, _: impl Fn(Self)->U) -> Perhaps<U>
|
|
where Self: Sized
|
|
{
|
|
Ok(None)
|
|
}
|
|
}
|