dsl: use only Dsl trait

This commit is contained in:
🪞👃🪞 2025-05-19 00:06:03 +03:00
parent 3bc739328e
commit 90f5699fff
11 changed files with 430 additions and 406 deletions

View file

@ -1,25 +1,18 @@
use crate::*;
use std::marker::PhantomData;
/// A [Command] that can be constructed from a [Token].
pub trait DslCommand<'state, C>: TryFromDsl<'state, C> + Command<C> {}
impl<'state, C, T: TryFromDsl<'state, C> + Command<C>> DslCommand<'state, C> for T {}
/// [Input] state that can be matched against a [Value].
pub trait DslInput: Input {
fn matches_dsl (&self, token: &str) -> bool;
}
pub trait DslInput: Input { fn matches_dsl (&self, token: &str) -> bool; }
/// A pre-configured mapping of input events to commands.
pub trait KeyMap<'state, S, C: DslCommand<'state, 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 command (&'state self, state: &'state S, input: &'state I) -> Option<C>;
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C>;
}
/// A [SourceIter] can be a [KeyMap].
impl<'state, S, C: DslCommand<'state, S>, I: DslInput> KeyMap<'state, S, C, I> for SourceIter<'state> {
fn command (&'state self, state: &'state S, input: &'state I) -> Option<C> {
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() {
iter = rest;
@ -29,8 +22,8 @@ impl<'state, S, C: DslCommand<'state, S>, I: DslInput> KeyMap<'state, S, C, I> f
match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = C::try_from_expr(state, &mut exp_iter) {
return Some(command)
if let Some(command) = Dsl::<C>::take(state, &mut exp_iter)? {
return Ok(Some(command))
}
}
},
@ -40,13 +33,13 @@ impl<'state, S, C: DslCommand<'state, S>, I: DslInput> KeyMap<'state, S, C, I> f
_ => panic!("invalid config (expected expression)")
}
}
None
Ok(None)
}
}
/// A [TokenIter] can be a [KeyMap].
impl<'state, S, C: DslCommand<'state, S>, I: DslInput> KeyMap<'state, S, C, I> for TokenIter<'state> {
fn command (&'state self, state: &'state S, input: &'state I) -> Option<C> {
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() {
match next {
@ -55,8 +48,8 @@ impl<'state, S, C: DslCommand<'state, S>, I: DslInput> KeyMap<'state, S, C, I> f
match e.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = C::try_from_expr(state, &mut e) {
return Some(command)
if let Some(command) = Dsl::<C>::take(state, &mut e)? {
return Ok(Some(command))
}
}
},
@ -66,44 +59,29 @@ impl<'state, S, C: DslCommand<'state, S>, I: DslInput> KeyMap<'state, S, C, I> f
_ => panic!("invalid config (expected expression, got: {next:?})")
}
}
None
Ok(None)
}
}
pub type InputLayerCond<'state, S> = Box<dyn Fn(&S)->bool + Send + Sync + 'state>;
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<'state, S, C, I, M>
where
C: DslCommand<'state, S>,
I: DslInput,
M: KeyMap<'state, S, C, I> + Send + Sync
{
__: &'state PhantomData<(S, C, I)>,
pub layers: Vec<(InputLayerCond<'state, S>, M)>,
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)>,
}
impl<'state, S, C, I, M> Default for InputMap<'state, S, C, I, M>
where
C: DslCommand<'state, S>,
I: DslInput,
M: KeyMap<'state, S, C, I> + Send + Sync
{
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 {
fn default () -> Self {
Self {
__: &PhantomData,
layers: vec![]
}
Self { __: PhantomData, layers: vec![] }
}
}
impl<'state, S, C, I, M> InputMap<'state, S, C, I, M>
where
C: DslCommand<'state, S>,
I: DslInput,
M: KeyMap<'state, S, C, I> + Send + Sync
{
impl<S, C, I, M> InputMap<S, C, I, M>
where S: Dsl<C> + 'static, C: Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
pub fn new (keymap: M) -> Self {
Self::default().layer(keymap)
}
@ -112,46 +90,38 @@ where
self
}
pub fn add_layer (&mut self, keymap: M) -> &mut Self {
self.add_layer_if(Box::new(|_|true), keymap);
self.add_layer_if(Box::new(|_|Ok(true)), keymap);
self
}
pub fn layer_if (mut self, condition: InputLayerCond<'state, S>, keymap: M) -> Self {
pub fn layer_if (mut self, condition: InputLayerCond<S>, keymap: M) -> Self {
self.add_layer_if(condition, keymap);
self
}
pub fn add_layer_if (&mut self, condition: InputLayerCond<'state, S>, keymap: M) -> &mut Self {
pub fn add_layer_if (&mut self, condition: InputLayerCond<S>, keymap: M) -> &mut Self {
self.layers.push((Box::new(condition), keymap));
self
}
}
impl<'state, S, C, I, M> std::fmt::Debug for InputMap<'state, S, C, I, M>
where
C: DslCommand<'state, S>,
I: DslInput,
M: KeyMap<'state, S, C, I> + Send + Sync
{
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 {
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<'state, S, C, I, M> KeyMap<'state, S, C, I> for InputMap<'state, S, C, I, M>
where
C: DslCommand<'state, S>,
I: DslInput,
M: KeyMap<'state, S, C, I> + Send + Sync
{
fn command (&'state self, state: &'state S, input: &'state I) -> Option<C> {
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 {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
for (condition, keymap) in self.layers.iter() {
if !condition(state) {
if !condition(state)? {
continue
}
if let Some(command) = keymap.command(state, input) {
return Some(command)
if let Some(command) = keymap.keybind_resolve(state, input)? {
return Ok(Some(command))
}
}
None
Ok(None)
}
}