wip: directionalize!

can't fit all into 1 trait because of directionality
of trait implementation rules and constraints :(
This commit is contained in:
🪞👃🪞 2025-05-20 18:48:55 +03:00
parent f797a7143d
commit 7c1cddc759
10 changed files with 221 additions and 222 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<S, C: Dsl<S> + Command<S>, I: DslInput> {
pub trait KeyMap<S: Dsl<C>, C: 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<'source, S, C: Dsl<S> + Command<S>, I: DslInput> KeyMap<S, C, I> for SourceIter<'source> {
impl<'source, S: Dsl<C>, C: Command<S>, I: DslInput> KeyMap<S, C, I> for SourceIter<'source> {
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<'source, S, C: Dsl<S> + Command<S>, I: DslInput> KeyMap<S, C, I> for Source
match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = Dsl::take_from(state, &mut exp_iter)? {
if let Some(command) = state.take(&mut exp_iter)? {
return Ok(Some(command))
}
}
@ -38,7 +38,7 @@ impl<'source, S, C: Dsl<S> + Command<S>, I: DslInput> KeyMap<S, C, I> for Source
}
/// A [TokenIter] can be a [KeyMap].
impl<'source, S, C: Dsl<S> + Command<S>, I: DslInput> KeyMap<S, C, I> for TokenIter<'source> {
impl<'source, S: Dsl<C>, C: Command<S>, I: DslInput> KeyMap<S, C, I> for TokenIter<'source> {
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<'source, S, C: Dsl<S> + Command<S>, I: DslInput> KeyMap<S, C, I> for TokenI
match e.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = C::take_from(state, &mut e)? {
if let Some(command) = state.take(&mut e)? {
return Ok(Some(command))
}
}
@ -68,20 +68,20 @@ pub type InputLayerCond<S> = Box<dyn Fn(&S)->Usually<bool> + Send + Sync>;
/// A collection of pre-configured mappings of input events to commands,
/// which may be made available subject to given conditions.
pub struct InputMap<S, C, I, M>
where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
where S: Dsl<C>, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
__: PhantomData<(S, C, I)>,
pub layers: Vec<(InputLayerCond<S>, M)>,
}
impl<S, C, I, M> Default for InputMap<S, C, I, M>
where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
where S: Dsl<C>, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
fn default () -> Self {
Self { __: PhantomData, layers: vec![] }
}
}
impl<S: 'static, C, I, M> InputMap<S, C, I, M>
where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
where S: Dsl<C>, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
pub fn new (keymap: M) -> Self {
Self::default().layer(keymap)
}
@ -104,7 +104,7 @@ where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
}
impl<S, C, I, M> std::fmt::Debug for InputMap<S, C, I, M>
where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
where S: Dsl<C>, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "[InputMap: {} layer(s)]", self.layers.len())
}
@ -112,7 +112,7 @@ where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
/// An [InputMap] can be a [KeyMap].
impl<S, C, I, M> KeyMap<S, C, I> for InputMap<S, C, I, M>
where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
where S: Dsl<C>, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
for (condition, keymap) in self.layers.iter() {
if !condition(state)? {