input: add InputMap; dsl/output/tui: Atom->Dsl
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-04-28 04:49:00 +03:00
parent 47b7f7e7f9
commit 35ad371205
15 changed files with 374 additions and 277 deletions

162
input/src/input_dsl.rs Normal file
View file

@ -0,0 +1,162 @@
use crate::*;
/// A [Command] that can be constructed from a [Token].
pub trait DslCommand<'a, C>: TryFromDsl<'a, C> + Command<C> {}
impl<'a, C, T: TryFromDsl<'a, C> + Command<C>> DslCommand<'a, C> for T {}
/// [Input] state that can be matched against a [Value].
pub trait DslInput: Input {
fn matches_dsl (&self, token: &str) -> bool;
}
/// A pre-configured mapping of input events to commands.
pub trait KeyMap<'a, S, C: DslCommand<'a, S>, I: DslInput> {
/// Try to find a command that matches the current input event.
fn command (&'a self, state: &'a S, input: &'a I) -> Option<C>;
}
/// A [SourceIter] can be a [KeyMap].
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for SourceIter<'a> {
fn command (&'a self, state: &'a S, input: &'a I) -> Option<C> {
let mut iter = self.clone();
while let Some((token, rest)) = iter.next() {
iter = rest;
match token {
Token { value: Value::Exp(0, exp_iter), .. } => {
let mut exp_iter = exp_iter.clone();
match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = C::try_from_expr(state, exp_iter.clone()) {
return Some(command)
}
}
},
_ => panic!("invalid config (expected symbol)")
}
},
_ => panic!("invalid config (expected expression)")
}
}
None
}
}
/// A [TokenIter] can be a [KeyMap].
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for TokenIter<'a> {
fn command (&'a self, state: &'a S, input: &'a I) -> Option<C> {
let mut iter = self.clone();
while let Some(next) = iter.next() {
match next {
Token { value: Value::Exp(0, exp_iter), .. } => {
let mut exp_iter = exp_iter.clone();
match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = C::try_from_expr(state, exp_iter.clone()) {
return Some(command)
}
}
},
_ => panic!("invalid config (expected symbol)")
}
},
_ => panic!("invalid config (expected expression)")
}
}
None
}
}
/// A collection of pre-configured mappings of input events to commands,
/// which may be made available subject to given conditions.
pub struct InputMap<'a, S, C: DslCommand<'a, S>, I: DslInput> {
layers: Vec<(
Box<dyn Fn(&S)->bool + Send + Sync + 'a>,
Box<dyn KeyMap<'a, S, C, I> + Send + Sync + 'a>
)>,
}
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> Default for InputMap<'a, S, C, I> {
fn default () -> Self {
Self {
layers: vec![]
}
}
}
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> InputMap<'a, S, C, I> {
pub fn new (keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a) -> Self {
Self::default().layer(keymap)
}
pub fn layer (
mut self, keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
) -> Self {
self.add_layer(keymap);
self
}
pub fn add_layer (
&mut self, keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
) -> &mut Self {
self.add_layer_if(|_|true, keymap);
self
}
pub fn layer_if (
mut self,
condition: impl Fn(&S)->bool + Send + Sync + 'a,
keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
) -> Self {
self.add_layer_if(condition, keymap);
self
}
pub fn add_layer_if (
&mut self,
condition: impl Fn(&S)->bool + Send + Sync + 'a,
keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
) -> &mut Self {
self.layers.push((Box::new(condition), Box::new(keymap)));
self
}
}
/// An [InputMap] can be a [KeyMap].
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for InputMap<'a, S, C, I> {
fn command (&'a self, state: &'a S, input: &'a I) -> Option<C> {
todo!()
}
}
//fn handle <C> (&self, state: &mut T, input: &U) -> Perhaps<bool> {
//for layer in self.layers.iter() {
//if !(layer.0)(state) {
//continue
//}
//let command: Option<C> = SourceIter(layer.1).command::<_, _, _>(state, input);
//if let Some(command) = command {
//if let Some(undo) = command.execute(state)? {
////app.history.push(undo);
//}
//return Ok(Some(true))
//}
//}
//Ok(None)
//}
//}
//fn layer (mut self, keymap: &'static str) -> Self {
//self.add_layer(keymap);
//self
//}
//fn layer_if (mut self, condition: impl Fn(&T)->bool + 'static, keymap: &'static str) -> Self {
//self.add_layer_if(condition, keymap);
//self
//}
//fn add_layer (&mut self, keymap: &'static str) -> &mut Self {
//self.layers.push((Box::new(|_|true), keymap));
//self
//}
//fn add_layer_if (&mut self, condition: impl Fn(&T)->bool + 'static, keymap: &'static str) -> &mut Self {
//self.layers.push((Box::new(condition), keymap));
//self
//}
//}