FromDsl -> Namespace

This commit is contained in:
🪞👃🪞 2025-05-21 00:06:36 +03:00
parent 455d6d00d5
commit f714302f21
8 changed files with 33 additions and 32 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<'source, S, C: FromDsl<'source, S> + Command<S>, I: DslInput> {
pub trait KeyMap<'source, S, C: Namespace<'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, C: FromDsl<'source, S> + Command<S>, I: DslInput>
impl<'source, S, C: Namespace<'source, S> + Command<S>, I: DslInput>
KeyMap<'source, S, C, I>
for SourceIter<'source> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
@ -24,7 +24,7 @@ for SourceIter<'source> {
match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = FromDsl::take_from(state, &mut exp_iter)? {
if let Some(command) = Namespace::take_from(state, &mut exp_iter)? {
return Ok(Some(command))
}
}
@ -40,7 +40,7 @@ for SourceIter<'source> {
}
/// A [TokenIter] can be a [KeyMap].
impl<'source, S, C: FromDsl<'source, S> + Command<S>, I: DslInput>
impl<'source, S, C: Namespace<'source, S> + Command<S>, I: DslInput>
KeyMap<'source, S, C, I>
for TokenIter<'source> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
@ -52,7 +52,7 @@ for TokenIter<'source> {
match e.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = FromDsl::take_from(state, &mut e)? {
if let Some(command) = Namespace::take_from(state, &mut e)? {
return Ok(Some(command))
}
}
@ -67,23 +67,24 @@ for TokenIter<'source> {
}
}
pub type InputLayerCond<'source, S> = Box<dyn Fn(&S)->Usually<bool> + 'source>;
pub type InputCondition<'source, S> =
Box<dyn Fn(&S)->Usually<bool> + Send + Sync + 'source>;
/// A collection of pre-configured mappings of input events to commands,
/// which may be made available subject to given conditions.
pub struct InputMap<'source,
S,
C: Command<S> + FromDsl<'source, S>,
C: Command<S> + Namespace<'source, S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> {
__: PhantomData<&'source (S, C, I)>,
pub layers: Vec<(InputLayerCond<'source, S>, M)>,
pub layers: Vec<(InputCondition<'source, S>, M)>,
}
impl<'source,
S,
C: Command<S> + FromDsl<'source, S>,
C: Command<S> + Namespace<'source, S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> Default for InputMap<'source, S, C, I, M>{
@ -94,7 +95,7 @@ impl<'source,
impl<'source,
S,
C: Command<S> + FromDsl<'source, S>,
C: Command<S> + Namespace<'source, S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> InputMap<'source, S, C, I, M> {
@ -109,11 +110,11 @@ impl<'source,
self.add_layer_if(Box::new(|_|Ok(true)), keymap);
self
}
pub fn layer_if (mut self, condition: InputLayerCond<'source, S>, keymap: M) -> Self {
pub fn layer_if (mut self, condition: InputCondition<'source, S>, keymap: M) -> Self {
self.add_layer_if(condition, keymap);
self
}
pub fn add_layer_if (&mut self, condition: InputLayerCond<'source, S>, keymap: M) -> &mut Self {
pub fn add_layer_if (&mut self, condition: InputCondition<'source, S>, keymap: M) -> &mut Self {
self.layers.push((Box::new(condition), keymap));
self
}
@ -121,7 +122,7 @@ impl<'source,
impl<'source,
S,
C: Command<S> + FromDsl<'source, S>,
C: Command<S> + Namespace<'source, S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> std::fmt::Debug for InputMap<'source, S, C, I, M> {
@ -133,7 +134,7 @@ impl<'source,
/// An [InputMap] can be a [KeyMap].
impl<'source,
S,
C: Command<S> + FromDsl<'source, S>,
C: Command<S> + Namespace<'source, S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> KeyMap<'source, S, C, I> for InputMap<'source, S, C, I, M> {