once again, why did i begin to refactor this

This commit is contained in:
🪞👃🪞 2025-01-18 00:13:36 +01:00
parent 297f9b30df
commit 798de37172
15 changed files with 582 additions and 602 deletions

View file

@ -1,46 +1,77 @@
use crate::*;
pub struct KeyMap<'a, T>(AtomIterator<'a>);
impl<'a, T> KeyMap<'a, T> {
pub const fn new (source: &'a str) -> Self {
Self(AtomIterator::new(TokenIterator::new(source)))
pub trait KeyMap {
/// Try to find a command that matches the currently pressed key
fn command <S, C: EdnCommand<S>> (&self, state: &S, input: &impl EdnInput) -> Option<C>;
}
pub struct SourceKeyMap<'a>(&'a str);
impl<'a> KeyMap for SourceKeyMap<'a> {
fn command <S, C: EdnCommand<S>> (&self, state: &S, input: &impl EdnInput) -> Option<C> {
todo!();
None
}
/// Try to find a binding matching the currently pressed key
pub fn command <S, C> (&self, state: &S, input: &impl EdnInput) -> Option<C>
where
C: Command<S> + EdnCommand<S>
{
use Atom::*;
let mut command: Option<C> = None;
match self {
Self::TokenIterator(t) => todo!(),
Self::TokenSlice(t) => todo!(),
Self::TokenVec(t) => todo!(),
//Self::AtomIterator(t) => todo!(),
Self::AtomSlice(t) => todo!(),
Self::Atom(t) => todo!(),
}
for item in self.0.iter() {
if let Exp(e) = item {
match e.as_slice() {
[Sym(key), c, args @ ..] if input.matches_edn(key) => {
command = C::from_edn(state, c, args);
if command.is_some() {
break
}
}
pub struct ParsedKeyMap<'a>(TokenIterator<'a>);
impl<'a> KeyMap for ParsedKeyMap<'a> {
fn command <S, C: EdnCommand<S>> (&self, state: &S, input: &impl EdnInput) -> Option<C> {
todo!();
None
}
}
pub struct RefKeyMap<'a>(TokenIterator<'a>);
impl<'a> KeyMap for RefKeyMap<'a> {
fn command <S, C: EdnCommand<S>> (&self, state: &S, input: &impl EdnInput) -> Option<C> {
todo!();
//for token in self.0 {
//match token?.kind() {
//TokenKind::Exp => match atoms.as_slice() {
//[key, command, args @ ..] => match (key.kind(), key.text()) {
//(TokenKind::Sym, key) => {
//if input.matches_edn(key) {
//let command = C::from_edn(state, command, args);
//if command.is_some() {
//return command
//}
//}
//},
//_ => panic!("invalid config: {item}")
//},
//_ => panic!("invalid config: {item}")
//}
//_ => panic!("invalid config: {item}")
//}
//}
None
}
}
pub struct ArcKeyMap(Vec<ArcAtom>);
impl<'a> KeyMap for ArcKeyMap {
fn command <S, C: EdnCommand<S>> (&self, state: &S, input: &impl EdnInput) -> Option<C> {
for atom in self.0.iter() {
match atom {
ArcAtom::Exp(atoms) => match atoms.as_slice() {
[key, command, args @ ..] => match (key.kind(), key.text()) {
(TokenKind::Sym, key) => {
if input.matches_edn(key) {
let command = C::from_edn(state, command, args);
if command.is_some() {
return command
}
}
},
_ => panic!("invalid config: {atom}")
},
_ => {}
_ => panic!("invalid config: {atom}")
}
} else {
panic!("invalid config")
_ => panic!("invalid config: {atom}")
}
}
command
None
}
}
/// [Input] state that can be matched against an [Atom].
pub trait EdnInput: Input {
fn matches_edn (&self, token: &str) -> bool;
fn get_event (_: &Atom<impl AsRef<str>>) -> Option<Self::Event> {
fn get_event <'a> (_: &impl Atom<'a>) -> Option<Self::Event> {
None
}
}
@ -48,8 +79,8 @@ pub trait EdnInput: Input {
pub trait EdnCommand<C>: Command<C> {
fn from_edn <'a> (
state: &C,
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>]
head: &impl Atom<'a>,
tail: &'a [impl Atom<'a>]
) -> Option<Self>;
}
/** Implement `EdnCommand` for given `State` and `Command` */
@ -78,8 +109,8 @@ pub trait EdnCommand<C>: Command<C> {
impl<$T: $Trait> EdnCommand<$T> for $Command {
fn from_edn <'a> (
$state: &$T,
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>]
head: &impl Atom,
tail: &'a [impl Atom]
) -> Option<Self> {
$(if let (Atom::Key($key), [ // if the identifier matches
// bind argument ids
@ -88,7 +119,7 @@ pub trait EdnCommand<C>: Command<C> {
$(, $rest @ ..)?
]) = (head.to_ref(), tail) {
$(
$(let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);)?
$(let $arg: Option<$type> = Context::<$type>::get($state, $arg);)?
)*
//$(edn_command!(@bind $state => $arg $(?)? : $type);)*
return Some($command)
@ -121,8 +152,8 @@ pub trait EdnCommand<C>: Command<C> {
impl EdnCommand<$State> for $Command {
fn from_edn <'a> (
$state: &$State,
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>],
head: &impl Atom,
tail: &'a [impl Atom],
) -> Option<Self> {
$(if let (Atom::Key($key), [ // if the identifier matches
// bind argument ids
@ -131,7 +162,7 @@ pub trait EdnCommand<C>: Command<C> {
$(, $rest @ ..)?
]) = (head.to_ref(), tail) {
$(
$(let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);)?
$(let $arg: Option<$type> = Context::<$type>::get($state, $arg);)?
)*
//$(edn_command!(@bind $state => $arg $(?)? : $type);)*
return Some($command)
@ -141,10 +172,10 @@ pub trait EdnCommand<C>: Command<C> {
}
};
(@bind $state:ident =>$arg:ident ? : $type:ty) => {
let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);
let $arg: Option<$type> = Context::<$type>::get($state, $arg);
};
(@bind $state:ident => $arg:ident : $type:ty) => {
let $arg: $type = EdnProvide::<$type>::get_or_fail($state, $arg);
let $arg: $type = Context::<$type>::get_or_fail($state, $arg);
};
}
#[cfg(test)] #[test] fn test_edn_keymap () -> Usually<()> {