generalize EdnItem.

maybe should rename it to Atom? ~90 instances of it
This commit is contained in:
🪞👃🪞 2025-01-17 19:47:35 +01:00
parent 1b9da07280
commit 143cd24e09
20 changed files with 314 additions and 286 deletions

View file

@ -1,16 +1,20 @@
use crate::*;
use std::sync::Arc;
pub trait EdnInput: Input {
fn matches_edn (&self, token: &str) -> bool;
fn get_event <S: AsRef<str>> (_: &EdnItem) -> Option<Self::Event> {
fn get_event (_: &EdnItem<impl AsRef<str>>) -> Option<Self::Event> {
None
}
}
/// Turns an EDN item sequence into a command enum variant.
pub trait EdnCommand<C>: Command<C> {
fn from_edn <'a> (state: &C, head: &EdnItem, tail: &'a [EdnItem])
-> Option<Self>;
fn from_edn <'a> (
state: &C,
head: &EdnItem<impl AsRef<str>>,
tail: &'a [EdnItem<impl AsRef<str>>]
) -> Option<Self>;
}
/** Implement `EdnCommand` for given `State` and `Command` */
@ -39,15 +43,15 @@ pub trait EdnCommand<C>: Command<C> {
impl<$T: $Trait> EdnCommand<$T> for $Command {
fn from_edn <'a> (
$state: &$T,
head: &EdnItem,
tail: &'a [EdnItem]
head: &EdnItem<impl AsRef<str>>,
tail: &'a [EdnItem<impl AsRef<str>>]
) -> Option<Self> {
$(if let (EdnItem::Key($key), [ // if the identifier matches
// bind argument ids
$($arg),*
// bind rest parameters
$(, $rest @ ..)?
]) = (head, tail) {
]) = (head.to_ref(), tail) {
$(
$(let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);)?
)*
@ -81,14 +85,16 @@ pub trait EdnCommand<C>: Command<C> {
))* }) => {
impl EdnCommand<$State> for $Command {
fn from_edn <'a> (
$state: &$State, head: &EdnItem, tail: &'a [EdnItem]
$state: &$State,
head: &EdnItem<impl AsRef<str>>,
tail: &'a [EdnItem<impl AsRef<str>>],
) -> Option<Self> {
$(if let (EdnItem::Key($key), [ // if the identifier matches
// bind argument ids
$($arg),*
// bind rest parameters
$(, $rest @ ..)?
]) = (head, tail) {
]) = (head.to_ref(), tail) {
$(
$(let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);)?
)*
@ -107,7 +113,7 @@ pub trait EdnCommand<C>: Command<C> {
};
}
pub struct EdnKeyMapToCommand(Vec<EdnItem>);
pub struct EdnKeyMapToCommand(Vec<EdnItem<Arc<str>>>);
impl EdnKeyMapToCommand {
/// Construct keymap from source text or fail
pub fn new (keymap: &str) -> Usually<Self> {