EdnProvide

This commit is contained in:
🪞👃🪞 2025-01-12 13:32:11 +01:00
parent 794d4210c6
commit fc06fb863b
7 changed files with 126 additions and 61 deletions

34
input/src/edn_command.rs Normal file
View file

@ -0,0 +1,34 @@
use crate::*;
use EdnItem::*;
#[macro_export] macro_rules! edn_command {
($Command:ty : |$state:ident:$State:ty| {
$(($key:literal [$($arg:ident : $type:ty),*] $command:expr))*
}) => {
impl EdnCommand<$State> for $Command {
fn from_edn <'a> ($state: &$State, head: &EdnItem<&str>, tail: &'a [EdnItem<String>]) -> Self {
match (head, tail) {
$((EdnItem::Key($key), [$($arg),*]) => {
$(let $arg: $type = EdnProvide::<$type>::get_or_fail($state, $arg);)*
$command
},)*
_ => panic!("no such command")
}
}
}
}
}
/// Turns an EDN symbol sequence into a command enum variant.
pub trait EdnCommand<C>: Command<C> {
fn from_edn <'a> (state: &C, head: &EdnItem<&str>, tail: &'a [EdnItem<String>]) -> Self;
fn get_isize (state: &C, item: &EdnItem<&str>) -> Option<isize> {
None
}
fn get_usize (state: &C, item: &EdnItem<&str>) -> Option<usize> {
None
}
fn get_bool (state: &C, item: &EdnItem<&str>) -> Option<usize> {
None
}
}