mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
dsl: modularize
This commit is contained in:
parent
4fc0db5777
commit
cf253c28f9
6 changed files with 224 additions and 397 deletions
|
|
@ -1,9 +1,5 @@
|
|||
use crate::*;
|
||||
use std::{sync::Arc, collections::BTreeMap, path::{Path, PathBuf}, fs::{exists, read_to_string}};
|
||||
/// Map of each event (e.g. key combination) to
|
||||
/// all command expressions bound to it by
|
||||
/// all loaded input layers.
|
||||
type EventMapImpl<E, C> = BTreeMap<E, Vec<Binding<C>>>;
|
||||
/// A collection of input bindings.
|
||||
///
|
||||
/// Each contained layer defines a mapping from input event to command invocation
|
||||
|
|
@ -14,15 +10,33 @@ type EventMapImpl<E, C> = BTreeMap<E, Vec<Binding<C>>>;
|
|||
/// When the first non-conditional or true conditional binding is executed,
|
||||
/// that .event()binding's value is returned.
|
||||
#[derive(Debug)]
|
||||
pub struct EventMap<E, C>(pub EventMapImpl<E, C>);
|
||||
pub struct EventMap<E, C>(
|
||||
/// Map of each event (e.g. key combination) to
|
||||
/// all command expressions bound to it by
|
||||
/// all loaded input layers.
|
||||
pub BTreeMap<E, Vec<Binding<C>>>
|
||||
);
|
||||
/// An input binding.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Binding<C> {
|
||||
pub command: C,
|
||||
pub commands: Arc<[C]>,
|
||||
pub condition: Option<Condition>,
|
||||
pub description: Option<Arc<str>>,
|
||||
pub source: Option<Arc<PathBuf>>,
|
||||
}
|
||||
impl<C> Binding<C> {
|
||||
pub fn from_dsl (dsl: impl Dsl) -> Usually<Self> {
|
||||
let command: Option<C> = None;
|
||||
let condition: Option<Condition> = None;
|
||||
let description: Option<Arc<str>> = None;
|
||||
let source: Option<Arc<PathBuf>> = None;
|
||||
if let Some(command) = command {
|
||||
Ok(Self { commands: [command].into(), condition, description, source })
|
||||
} else {
|
||||
Err(format!("no command in {dsl:?}").into())
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Input bindings are only returned if this evaluates to true
|
||||
#[derive(Clone)]
|
||||
pub struct Condition(Arc<Box<dyn Fn()->bool + Send + Sync>>);
|
||||
|
|
@ -59,72 +73,4 @@ impl<E: Clone + Ord, C> EventMap<E, C> {
|
|||
.map(|bb|bb.iter().filter(|b|b.condition.as_ref().map(|c|(c.0)()).unwrap_or(true)).next())
|
||||
.flatten()
|
||||
}
|
||||
/// Create event map from path to text file.
|
||||
pub fn load_from_path <'s> (
|
||||
&'s mut self, path: impl AsRef<Path>
|
||||
) -> Usually<&'s mut Self> where Self: DslInto<C> + DslInto<E> {
|
||||
if exists(path.as_ref())? {
|
||||
let source = read_to_string(&path)?;
|
||||
let path: Arc<PathBuf> = Arc::new(path.as_ref().into());
|
||||
self.load_from_source(&source, &Some(&path))
|
||||
} else {
|
||||
return Err(format!("(e5) not found: {:?}", path.as_ref()).into())
|
||||
}
|
||||
}
|
||||
/// Create event map from DSL tokenizer.
|
||||
pub fn load_from_source (
|
||||
&mut self, dsl: impl Dsl, path: &Option<&Arc<PathBuf>>
|
||||
) -> Usually<&mut Self> where Self: DslInto<C> + DslInto<E> {
|
||||
dsl.each(|dsl|self.load_from_source_one(&dsl, path).map(move|_|()))?;
|
||||
Ok(self)
|
||||
}
|
||||
/// Load one event binding into the event map.
|
||||
pub fn load_from_source_one <'s> (
|
||||
&'s mut self, dsl: impl Dsl, path: &Option<&Arc<PathBuf>>
|
||||
) -> Usually<&'s mut Self> where Self: DslInto<C> + DslInto<E> {
|
||||
if let Some(exp) = dsl.head()?.exp()?
|
||||
&& let Some(sym) = exp.head()?.sym()?
|
||||
&& let Some(tail) = exp.tail()?
|
||||
{
|
||||
let event = self.dsl_into_or_else(&sym, ||panic!())?;
|
||||
let command = self.dsl_into_or_else(&tail, ||panic!())?;
|
||||
Ok(self.add(event, Binding {
|
||||
command, condition: None, description: None, source: path.cloned()
|
||||
}))
|
||||
} else {
|
||||
Err(format!("unexpected: {:?}", dsl.head()?).into())
|
||||
}
|
||||
}
|
||||
//})Ok(if let Some(sym) = dsl.head()?.exp()?.head()?.sym()? {
|
||||
//if let Some(tail) = dsl.head()?.exp()?.tail()? {
|
||||
//let event: E = sym.into();
|
||||
//let binding: Binding<C> = Binding { command: tail.into(), condition: None, description: None, source: None };
|
||||
//if let Some(bindings) = map.0.get_mut(&event) {
|
||||
//bindings.push(binding);
|
||||
//} else {
|
||||
//map.0.insert(event, vec![binding]);
|
||||
//}
|
||||
//} else {
|
||||
//panic!("empty binding: {}", dsl.head()?.exp()?.unwrap_or_default())
|
||||
//}
|
||||
//} else if let Some(ref text) = dsl.text()? {
|
||||
//map.0.extend(Self::from_path(PathBuf::from(text))?.0);
|
||||
//} else {
|
||||
//return Err(format!("unexpected: {dsl:?}").into())
|
||||
//}));
|
||||
//Ok(map)
|
||||
//}
|
||||
}
|
||||
impl<C> Binding<C> {
|
||||
pub fn from_dsl (dsl: impl Dsl) -> Usually<Self> {
|
||||
let command: Option<C> = None;
|
||||
let condition: Option<Condition> = None;
|
||||
let description: Option<Arc<str>> = None;
|
||||
let source: Option<Arc<PathBuf>> = None;
|
||||
if let Some(command) = command {
|
||||
Ok(Self { command, condition, description, source })
|
||||
} else {
|
||||
Err(format!("no command in {dsl:?}").into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue