mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
wip: finally, informative type errors from the macro
Some checks failed
/ build (push) Has been cancelled
Some checks failed
/ build (push) Has been cancelled
fixin mixin
This commit is contained in:
parent
abc87d3234
commit
583660c330
10 changed files with 152 additions and 205 deletions
|
|
@ -5,15 +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: Namespace<S> + Command<S>, I: DslInput> {
|
||||
pub trait KeyMap<'k, S, C: Provide<'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<'source, S, C: Namespace<S> + Command<S>, I: DslInput>
|
||||
KeyMap<'source, S, C, I>
|
||||
for SourceIter<'source> {
|
||||
impl<'k, S, C: Provide<'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() {
|
||||
|
|
@ -24,7 +22,7 @@ for SourceIter<'source> {
|
|||
match exp_iter.next() {
|
||||
Some(Token { value: Value::Sym(binding), .. }) => {
|
||||
if input.matches_dsl(binding) {
|
||||
if let Some(command) = Namespace::take_from(state, &mut exp_iter)? {
|
||||
if let Some(command) = Provide::provide(state, &mut exp_iter)? {
|
||||
return Ok(Some(command))
|
||||
}
|
||||
}
|
||||
|
|
@ -40,9 +38,7 @@ for SourceIter<'source> {
|
|||
}
|
||||
|
||||
/// A [TokenIter] can be a [KeyMap].
|
||||
impl<'source, S, C: Namespace<S> + Command<S>, I: DslInput>
|
||||
KeyMap<'source, S, C, I>
|
||||
for TokenIter<'source> {
|
||||
impl<'k, S, C: Provide<'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() {
|
||||
|
|
@ -52,7 +48,7 @@ for TokenIter<'source> {
|
|||
match e.next() {
|
||||
Some(Token { value: Value::Sym(binding), .. }) => {
|
||||
if input.matches_dsl(binding) {
|
||||
if let Some(command) = Namespace::take_from(state, &mut e)? {
|
||||
if let Some(command) = Provide::provide(state, &mut e)? {
|
||||
return Ok(Some(command))
|
||||
}
|
||||
}
|
||||
|
|
@ -67,38 +63,33 @@ for TokenIter<'source> {
|
|||
}
|
||||
}
|
||||
|
||||
pub type InputCondition<'source, S> =
|
||||
Box<dyn Fn(&S)->Usually<bool> + Send + Sync + 'source>;
|
||||
pub type InputCondition<'k, S> = Box<dyn Fn(&S)->Usually<bool> + Send + Sync + 'k>;
|
||||
|
||||
/// A collection of pre-configured mappings of input events to commands,
|
||||
/// which may be made available subject to given conditions.
|
||||
pub struct InputMap<'source,
|
||||
pub struct InputMap<'k,
|
||||
S,
|
||||
C: Command<S> + Namespace<S>,
|
||||
C: Command<S> + Provide<'k, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'source, S, C, I>
|
||||
M: KeyMap<'k, S, C, I>
|
||||
> {
|
||||
__: PhantomData<&'source (S, C, I)>,
|
||||
pub layers: Vec<(InputCondition<'source, S>, M)>,
|
||||
__: PhantomData<&'k (S, C, I)>,
|
||||
pub layers: Vec<(InputCondition<'k, S>, M)>,
|
||||
}
|
||||
|
||||
impl<'source,
|
||||
impl<'k,
|
||||
S,
|
||||
C: Command<S> + Namespace<S>,
|
||||
C: Command<S> + Provide<'k, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'source, S, C, I>
|
||||
> Default for InputMap<'source, S, C, I, M>{
|
||||
M: KeyMap<'k, S, C, I>
|
||||
> Default for InputMap<'k, S, C, I, M>{
|
||||
fn default () -> Self {
|
||||
Self { __: PhantomData, layers: vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source,
|
||||
S,
|
||||
C: Command<S> + Namespace<S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'source, S, C, I>
|
||||
> InputMap<'source, S, C, I, M> {
|
||||
impl<'k, S, C: Command<S> + Provide<'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)
|
||||
}
|
||||
|
|
@ -110,34 +101,30 @@ impl<'source,
|
|||
self.add_layer_if(Box::new(|_|Ok(true)), keymap);
|
||||
self
|
||||
}
|
||||
pub fn layer_if (mut self, condition: InputCondition<'source, S>, keymap: M) -> Self {
|
||||
pub fn layer_if (mut self, condition: InputCondition<'k, S>, keymap: M) -> Self {
|
||||
self.add_layer_if(condition, keymap);
|
||||
self
|
||||
}
|
||||
pub fn add_layer_if (&mut self, condition: InputCondition<'source, S>, keymap: M) -> &mut Self {
|
||||
pub fn add_layer_if (&mut self, condition: InputCondition<'k, S>, keymap: M) -> &mut Self {
|
||||
self.layers.push((Box::new(condition), keymap));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source,
|
||||
S,
|
||||
C: Command<S> + Namespace<S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'source, S, C, I>
|
||||
> std::fmt::Debug for InputMap<'source, S, C, I, M> {
|
||||
impl<'k, S, C: Command<S> + Provide<'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())
|
||||
}
|
||||
}
|
||||
|
||||
/// An [InputMap] can be a [KeyMap].
|
||||
impl<'source,
|
||||
impl<'k,
|
||||
S,
|
||||
C: Command<S> + Namespace<S>,
|
||||
C: Command<S> + Provide<'k, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'source, S, C, I>
|
||||
> KeyMap<'source, S, C, I> for InputMap<'source, S, C, I, M> {
|
||||
M: KeyMap<'k, S, C, I>
|
||||
> KeyMap<'k, S, C, I> for InputMap<'k, S, C, I, M> {
|
||||
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
|
||||
for (condition, keymap) in self.layers.iter() {
|
||||
if !condition(state)? {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue