dsl: InputLayerCond; collect macros

This commit is contained in:
🪞👃🪞 2025-05-03 02:13:22 +03:00
parent 0d4ba4a54e
commit 3df1938626
6 changed files with 121 additions and 110 deletions

View file

@ -41,101 +41,3 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
self.as_ref().map(|s|s.get_or_fail(dsl)).expect("no provider")
}
}
/// Implement `Context` for a context and type.
#[macro_export] macro_rules! provide {
// Provide a value to the EDN template
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State {
#[allow(unreachable_code)]
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
// Provide a value more generically
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$lt> Context<$lt, $type> for $State {
#[allow(unreachable_code)]
fn get (&$lt $self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
}
/// Implement `Context` for a context and numeric type.
///
/// This enables support for numeric literals.
#[macro_export] macro_rules! provide_num {
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$T: $Trait> Context<$type> for $T {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
}
}
};
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
}
}
};
}
/// Implement `Context` for a context and the boolean type.
///
/// This enables support for boolean literals.
#[macro_export] macro_rules! provide_bool {
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$T: $Trait> Context<$type> for $T {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl {
Num(n) => match *n { 0 => false, _ => true },
Sym(":false") | Sym(":f") => false,
Sym(":true") | Sym(":t") => true,
$(Sym($pat) => $expr,)*
_ => return Context::get(self, dsl)
})
}
}
};
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl {
Num(n) => match *n { 0 => false, _ => true },
Sym(":false") | Sym(":f") => false,
Sym(":true") | Sym(":t") => true,
$(Sym($pat) => $expr,)*
_ => return None
})
}
}
};
}
#[cfg(test)] #[test] fn test_edn_context () {
struct Test;
provide_bool!(bool: |self: Test|{
":provide-bool" => true
});
let test = Test;
assert_eq!(test.get(&Value::Sym(":false")), Some(false));
assert_eq!(test.get(&Value::Sym(":true")), Some(true));
assert_eq!(test.get(&Value::Sym(":provide-bool")), Some(true));
assert_eq!(test.get(&Value::Sym(":missing-bool")), None);
assert_eq!(test.get(&Value::Num(0)), Some(false));
assert_eq!(test.get(&Value::Num(1)), Some(true));
}

View file

@ -34,6 +34,12 @@ impl<'a> Iterator for TokenIter<'a> {
}
}
impl<'a> From<&'a str> for TokenIter<'a> {
fn from (source: &'a str) -> Self{
Self(SourceIter(source))
}
}
impl<'a> From<SourceIter<'a>> for TokenIter<'a> {
fn from (source: SourceIter<'a>) -> Self{
Self(source)

View file

@ -24,6 +24,7 @@
}
}
/// Implement `Context` for one or more base structs, types, and keys. */
#[macro_export] macro_rules! expose {
($([$self:ident:$State:ty] $(([$($Type:tt)*] $(($pat:literal $expr:expr))*))*)*) => {
$(expose!(@impl [$self: $State] { $([$($Type)*] => { $($pat => $expr),* })* });)*
@ -51,6 +52,90 @@
};
}
/// Implement `Context` for a context and type.
#[macro_export] macro_rules! provide {
// Provide a value to the EDN template
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State {
#[allow(unreachable_code)]
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
// Provide a value more generically
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$lt> Context<$lt, $type> for $State {
#[allow(unreachable_code)]
fn get (&$lt $self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
}
/// Implement `Context` for a context and numeric type.
///
/// This enables support for numeric literals.
#[macro_export] macro_rules! provide_num {
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$T: $Trait> Context<$type> for $T {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
}
}
};
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
}
}
};
}
/// Implement `Context` for a context and the boolean type.
///
/// This enables support for boolean literals.
#[macro_export] macro_rules! provide_bool {
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$T: $Trait> Context<$type> for $T {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl {
Num(n) => match *n { 0 => false, _ => true },
Sym(":false") | Sym(":f") => false,
Sym(":true") | Sym(":t") => true,
$(Sym($pat) => $expr,)*
_ => return Context::get(self, dsl)
})
}
}
};
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl {
Num(n) => match *n { 0 => false, _ => true },
Sym(":false") | Sym(":f") => false,
Sym(":true") | Sym(":t") => true,
$(Sym($pat) => $expr,)*
_ => return None
})
}
}
};
}
#[macro_export] macro_rules! impose {
([$self:ident:$Struct:ty] $(($Command:ty : $(($cmd:literal $args:tt $result:expr))*))*) => {
$(atom_command!($Command: |$self: $Struct| { $(($cmd $args $result))* });)*

View file

@ -114,6 +114,20 @@ mod dsl_macros;
Ok(())
}
#[cfg(test)] #[test] fn test_dsl_context () {
struct Test;
provide_bool!(bool: |self: Test|{
":provide-bool" => true
});
let test = Test;
assert_eq!(test.get(&Value::Sym(":false")), Some(false));
assert_eq!(test.get(&Value::Sym(":true")), Some(true));
assert_eq!(test.get(&Value::Sym(":provide-bool")), Some(true));
assert_eq!(test.get(&Value::Sym(":missing-bool")), None);
assert_eq!(test.get(&Value::Num(0)), Some(false));
assert_eq!(test.get(&Value::Num(1)), Some(true));
}
//#[cfg(test)] #[test] fn test_examples () -> Result<(), ParseError> {
//// Let's pretend to render some view.
//let source = include_str!("../../tek/src/view_arranger.edn");

View file

@ -70,14 +70,18 @@ impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for TokenIter
}
}
pub type InputLayerCond<'a, S> = Box<dyn Fn(&S)->bool + Send + Sync + 'a>;
/// 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, M: KeyMap<'a, S, C, I> + Send + Sync> {
pub struct InputMap<'a, S, C, I, M>
where
C: DslCommand<'a, S>,
I: DslInput,
M: KeyMap<'a, S, C, I> + Send + Sync
{
__: &'a PhantomData<(S, C, I)>,
pub layers: Vec<(
fn(&S)->bool,
M
)>,
pub layers: Vec<(InputLayerCond<'a, S>, M)>,
}
impl<'a, S, C, I, M> Default for InputMap<'a, S, C, I, M>
@ -108,15 +112,15 @@ where
self
}
pub fn add_layer (&mut self, keymap: M) -> &mut Self {
self.add_layer_if(|_|true, keymap);
self.add_layer_if(Box::new(|_|true), keymap);
self
}
pub fn layer_if (mut self, condition: fn(&S)->bool, keymap: M) -> Self {
pub fn layer_if (mut self, condition: InputLayerCond<'a, S>, keymap: M) -> Self {
self.add_layer_if(condition, keymap);
self
}
pub fn add_layer_if (&mut self, condition: fn(&S)->bool, keymap: M) -> &mut Self {
self.layers.push((condition, keymap));
pub fn add_layer_if (&mut self, condition: InputLayerCond<'a, S>, keymap: M) -> &mut Self {
self.layers.push((Box::new(condition), keymap));
self
}
}

View file

@ -27,14 +27,14 @@ use crate::*;
#[cfg(feature = "dsl")]
pub struct View<'a, T>(
pub &'a T,
pub SourceIter<'a>
pub TokenIter<'a>
);
#[cfg(feature = "dsl")]
impl<'a, O: Output + 'a, T: ViewContext<'a, O>> Content<O> for View<'a, T> {
fn content (&self) -> impl Render<O> {
let iter = self.1.clone();
while let Some((Token { value, .. }, _)) = iter.next() {
let mut iter = self.1.clone();
while let Some(Token { value, .. }) = iter.next() {
if let Some(content) = self.0.get_content(&value) {
return Some(content)
}