dsl: Provide -> Take, Receive -> Give (swap + shorten)

This commit is contained in:
🪞👃🪞 2025-05-23 21:39:29 +03:00
parent 583660c330
commit ddf0c05d5f
10 changed files with 64 additions and 55 deletions

View file

@ -5,13 +5,13 @@ use std::marker::PhantomData;
pub trait DslInput: Input { fn matches_dsl (&self, token: &str) -> bool; }
/// A pre-configured mapping of input events to commands.
pub trait KeyMap<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> {
pub trait KeyMap<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> {
/// Try to find a command that matches the current input event.
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C>;
}
/// A [SourceIter] can be a [KeyMap].
impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for SourceIter<'k> {
impl<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for SourceIter<'k> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
let mut iter = self.clone();
while let Some((token, rest)) = iter.next() {
@ -22,7 +22,7 @@ impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for
match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = Provide::provide(state, &mut exp_iter)? {
if let Some(command) = Take::provide(state, &mut exp_iter)? {
return Ok(Some(command))
}
}
@ -38,7 +38,7 @@ impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for
}
/// A [TokenIter] can be a [KeyMap].
impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for TokenIter<'k> {
impl<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for TokenIter<'k> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
let mut iter = self.clone();
while let Some(next) = iter.next() {
@ -48,7 +48,7 @@ impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for
match e.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = Provide::provide(state, &mut e)? {
if let Some(command) = Take::provide(state, &mut e)? {
return Ok(Some(command))
}
}
@ -69,7 +69,7 @@ pub type InputCondition<'k, S> = Box<dyn Fn(&S)->Usually<bool> + Send + Sync + '
/// which may be made available subject to given conditions.
pub struct InputMap<'k,
S,
C: Command<S> + Provide<'k, S>,
C: Command<S> + Take<'k, S>,
I: DslInput,
M: KeyMap<'k, S, C, I>
> {
@ -79,7 +79,7 @@ pub struct InputMap<'k,
impl<'k,
S,
C: Command<S> + Provide<'k, S>,
C: Command<S> + Take<'k, S>,
I: DslInput,
M: KeyMap<'k, S, C, I>
> Default for InputMap<'k, S, C, I, M>{
@ -88,7 +88,7 @@ impl<'k,
}
}
impl<'k, S, C: Command<S> + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
impl<'k, S, C: Command<S> + Take<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
InputMap<'k, S, C, I, M> {
pub fn new (keymap: M) -> Self {
Self::default().layer(keymap)
@ -111,7 +111,7 @@ InputMap<'k, S, C, I, M> {
}
}
impl<'k, S, C: Command<S> + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
impl<'k, S, C: Command<S> + Take<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
std::fmt::Debug for InputMap<'k, S, C, I, M> {
fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "[InputMap: {} layer(s)]", self.layers.len())
@ -121,7 +121,7 @@ std::fmt::Debug for InputMap<'k, S, C, I, M> {
/// An [InputMap] can be a [KeyMap].
impl<'k,
S,
C: Command<S> + Provide<'k, S>,
C: Command<S> + Take<'k, S>,
I: DslInput,
M: KeyMap<'k, S, C, I>
> KeyMap<'k, S, C, I> for InputMap<'k, S, C, I, M> {