read explicit lifetime to FromDsl
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-20 22:02:51 +03:00
parent 7c1cddc759
commit 455d6d00d5
14 changed files with 252 additions and 286 deletions

View file

@ -5,13 +5,15 @@ 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: Dsl<C>, C: Command<S>, I: DslInput> {
pub trait KeyMap<'source, S, C: FromDsl<'source, 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<'source, S: Dsl<C>, C: Command<S>, I: DslInput> KeyMap<S, C, I> for SourceIter<'source> {
impl<'source, S, C: FromDsl<'source, S> + Command<S>, I: DslInput>
KeyMap<'source, 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 +24,7 @@ impl<'source, S: Dsl<C>, C: Command<S>, I: DslInput> KeyMap<S, C, I> for SourceI
match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = state.take(&mut exp_iter)? {
if let Some(command) = FromDsl::take_from(state, &mut exp_iter)? {
return Ok(Some(command))
}
}
@ -38,7 +40,9 @@ impl<'source, S: Dsl<C>, C: Command<S>, I: DslInput> KeyMap<S, C, I> for SourceI
}
/// A [TokenIter] can be a [KeyMap].
impl<'source, S: Dsl<C>, C: Command<S>, I: DslInput> KeyMap<S, C, I> for TokenIter<'source> {
impl<'source, S, C: FromDsl<'source, S> + Command<S>, I: DslInput>
KeyMap<'source, 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 +52,7 @@ impl<'source, S: Dsl<C>, C: Command<S>, I: DslInput> KeyMap<S, C, I> for TokenIt
match e.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = state.take(&mut e)? {
if let Some(command) = FromDsl::take_from(state, &mut e)? {
return Ok(Some(command))
}
}
@ -63,25 +67,37 @@ impl<'source, S: Dsl<C>, C: Command<S>, I: DslInput> KeyMap<S, C, I> for TokenIt
}
}
pub type InputLayerCond<S> = Box<dyn Fn(&S)->Usually<bool> + Send + Sync>;
pub type InputLayerCond<'source, S> = Box<dyn Fn(&S)->Usually<bool> + 'source>;
/// 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 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)>,
pub struct InputMap<'source,
S,
C: Command<S> + FromDsl<'source, S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> {
__: PhantomData<&'source (S, C, I)>,
pub layers: Vec<(InputLayerCond<'source, S>, M)>,
}
impl<S, C, I, M> Default for InputMap<S, C, I, M>
where S: Dsl<C>, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
impl<'source,
S,
C: Command<S> + FromDsl<'source, S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> Default for InputMap<'source, S, C, I, M>{
fn default () -> Self {
Self { __: PhantomData, layers: vec![] }
}
}
impl<S: 'static, C, I, M> InputMap<S, C, I, M>
where S: Dsl<C>, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
impl<'source,
S,
C: Command<S> + FromDsl<'source, S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> InputMap<'source, S, C, I, M> {
pub fn new (keymap: M) -> Self {
Self::default().layer(keymap)
}
@ -93,26 +109,34 @@ where S: Dsl<C>, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
self.add_layer_if(Box::new(|_|Ok(true)), keymap);
self
}
pub fn layer_if (mut self, condition: InputLayerCond<S>, keymap: M) -> Self {
pub fn layer_if (mut self, condition: InputLayerCond<'source, S>, keymap: M) -> Self {
self.add_layer_if(condition, keymap);
self
}
pub fn add_layer_if (&mut self, condition: InputLayerCond<S>, keymap: M) -> &mut Self {
pub fn add_layer_if (&mut self, condition: InputLayerCond<'source, S>, keymap: M) -> &mut Self {
self.layers.push((Box::new(condition), keymap));
self
}
}
impl<S, C, I, M> std::fmt::Debug for InputMap<S, C, I, M>
where S: Dsl<C>, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
impl<'source,
S,
C: Command<S> + FromDsl<'source, S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> std::fmt::Debug for InputMap<'source, S, C, I, M> {
fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "[InputMap: {} layer(s)]", self.layers.len())
}
}
/// An [InputMap] can be a [KeyMap].
impl<S, C, I, M> KeyMap<S, C, I> for InputMap<S, C, I, M>
where S: Dsl<C>, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
impl<'source,
S,
C: Command<S> + FromDsl<'source, S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> KeyMap<'source, S, C, I> for InputMap<'source, S, C, I, M> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
for (condition, keymap) in self.layers.iter() {
if !condition(state)? {