mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
This commit is contained in:
parent
643658ab16
commit
a601d3d806
3 changed files with 84 additions and 99 deletions
|
|
@ -1,10 +1,9 @@
|
|||
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
|
||||
|
|
@ -16,7 +15,6 @@ type EventMapImpl<E, C> = BTreeMap<E, Vec<Binding<C>>>;
|
|||
/// that .event()binding's value is returned.
|
||||
#[derive(Debug)]
|
||||
pub struct EventMap<E, C>(EventMapImpl<E, C>);
|
||||
|
||||
/// An input binding.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Binding<C> {
|
||||
|
|
@ -25,18 +23,14 @@ pub struct Binding<C> {
|
|||
pub description: Option<Arc<str>>,
|
||||
pub source: Option<Arc<PathBuf>>,
|
||||
}
|
||||
|
||||
/// Input bindings are only returned if this evaluates to true
|
||||
#[derive(Clone)]
|
||||
pub struct Condition(Arc<Box<dyn Fn()->bool + Send + Sync>>);
|
||||
|
||||
impl_debug!(Condition |self, w| { write!(w, "*") });
|
||||
|
||||
/// Default is always empty map regardless if `E` and `C` implement [Default].
|
||||
impl<E, C> Default for EventMap<E, C> {
|
||||
fn default () -> Self { Self(Default::default()) }
|
||||
}
|
||||
|
||||
impl<E: Clone + Ord, C> EventMap<E, C> {
|
||||
/// Create a new event map
|
||||
pub fn new () -> Self {
|
||||
|
|
@ -68,7 +62,7 @@ impl<E: Clone + Ord, C> EventMap<E, C> {
|
|||
/// Create event map from path to text file.
|
||||
pub fn from_path <P: AsRef<Path>> (path: P) -> Usually<Self> where E: From<Arc<str>> {
|
||||
if exists(path.as_ref())? {
|
||||
Self::from_source(read_and_leak(path)?)
|
||||
Self::from_source(read_to_string(path)?)
|
||||
} else {
|
||||
return Err(format!("(e5) not found: {:?}", path.as_ref()).into())
|
||||
}
|
||||
|
|
@ -117,7 +111,6 @@ impl<E: Clone + Ord, C> EventMap<E, C> {
|
|||
Ok(map)
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> Binding<C> {
|
||||
fn from_dsl (dsl: impl Dsl) -> Usually<Self> {
|
||||
let mut command: Option<C> = None;
|
||||
|
|
@ -131,18 +124,9 @@ impl<C> Binding<C> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn unquote (x: &str) -> &str {
|
||||
let mut chars = x.chars();
|
||||
chars.next();
|
||||
//chars.next_back();
|
||||
chars.as_str()
|
||||
}
|
||||
|
||||
fn read_and_leak (path: impl AsRef<Path>) -> Usually<&'static str> {
|
||||
Ok(leak(String::from_utf8(std::fs::read(path.as_ref())?)?))
|
||||
}
|
||||
|
||||
fn leak (x: impl AsRef<str>) -> &'static str {
|
||||
Box::leak(x.as_ref().into())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ use crate::*;
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "dsl"))] #[test] fn test_dsl_keymap () -> Usually<()> {
|
||||
let _keymap = CstIter::new("");
|
||||
Ok(())
|
||||
}
|
||||
//#[cfg(all(test, feature = "dsl"))] #[test] fn test_dsl_keymap () -> Usually<()> {
|
||||
//let _keymap = CstIter::new("");
|
||||
//Ok(())
|
||||
//}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue