This commit is contained in:
🪞👃🪞 2025-07-14 22:22:45 +03:00
parent 6c3a0964ec
commit 7271081fc9
10 changed files with 119 additions and 296 deletions

View file

@ -1,11 +1,22 @@
use crate::*;
/// A collection of input bind.
/// A collection of input bindings.
///
/// Each contained layer defines a mapping from input event to command invocation
/// over a given state. Furthermore, each layer may have an associated cond,
/// so that only certain layers are active at a given time depending on state.
#[derive(Debug, Default)] pub struct InputMap<I, T: Dsl>(std::collections::BTreeMap<I, T>);
impl<I, T: Dsl> InputMap<I, T> {
#[derive(Debug, Default)] pub struct InputMap<I, T: Dsl>(
/// Map of input event (key combination) to
/// all command expressions bound to it by
/// all loaded input layers.
pub BTreeMap<I, Vec<InputBinding<T>>>
);
#[derive(Debug, Default)] pub struct InputBinding<T: Dsl> {
condition: Option<T>,
command: T,
description: Option<Arc<str>>,
source: Option<Arc<PathBuf>>,
}
impl<I: Debug + Ord, T: Dsl> InputMap<I, T> {
/// Create input layer collection from path to text file.
pub fn from_path <P: Debug + AsRef<Path>> (path: P) -> Usually<Self> {
if !exists(path.as_ref())? {
@ -18,29 +29,38 @@ impl<I, T: Dsl> InputMap<I, T> {
Self::from_dsl(CstIter::from(source.as_ref()))
}
/// Create input layer collection from DSL.
pub fn from_dsl (dsl: impl Dsl) -> Usually<Self> {
pub fn from_dsl <D: Dsl> (dsl: D) -> Usually<Self> {
use DslVal::*;
let mut input_map: Self = Self(Default::default());
while let Exp(_, mut exp) = dsl.val() {
let mut input_map: BTreeMap<I, Vec<InputBinding<T>>> = Default::default();
let mut index = 0;
while let Some(Exp(_, mut exp)) = dsl.nth(index) {
let val = exp.nth(0).map(|x|x.val());
match val {
Some(Str(path)) => {
let path = PathBuf::from(path.as_ref());
let path = PathBuf::from(path.as_ref());
let module = InputMap::<I, T>::from_path(&path)?;
for (key, val) in module.0.into_iter() {
todo!("import {exp:?} {key:?} {val:?} {path:?}");
if !input_map.contains_key(&key) {
input_map.insert(key, vec![]);
}
}
},
Some(Exp(_, expr)) if let Some(Sym(sym)) = expr.nth(0) => {
//input_map.unconditional.push(expr);
todo!("binding");
Some(Sym(sym)) => {
//let key: I = sym.into();
//if !input_map.contains_key(&key) {
//input_map.insert(key, vec![]);
//}
todo!("binding {exp:?} {sym:?}");
},
Some(Exp(_, expr)) if let Some(Key(key)) = expr.nth(0) && key.as_ref() == "if" => {
todo!("conditional binding");
Some(Key(key)) if key.as_ref() == "if" => {
todo!("conditional binding {exp:?} {key:?}");
},
_ => return Result::Err(format!("invalid token in keymap: {val:?}").into()),
}
index += 1;
}
Ok(input_map)
Ok(Self(input_map))
}
/// Evaluate the active layers for a given state,
/// returning the command to be executed, if any.