Compare commits

...

2 commits

Author SHA1 Message Date
21f7f6b38a 0.13.0: release
Some checks are pending
/ build (push) Waiting to run
2025-05-03 02:14:03 +03:00
3df1938626 dsl: InputLayerCond; collect macros 2025-05-03 02:13:22 +03:00
8 changed files with 132 additions and 121 deletions

20
Cargo.lock generated
View file

@ -304,9 +304,9 @@ checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.15.2" version = "0.15.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3"
dependencies = [ dependencies = [
"allocator-api2", "allocator-api2",
"equivalent", "equivalent",
@ -791,9 +791,9 @@ dependencies = [
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "1.0.5" version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d97817398dd4bb2e6da002002db259209759911da105da92bec29ccb12cf58bf" checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"errno", "errno",
@ -928,13 +928,13 @@ dependencies = [
"fastrand", "fastrand",
"getrandom 0.3.2", "getrandom 0.3.2",
"once_cell", "once_cell",
"rustix 1.0.5", "rustix 1.0.7",
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
name = "tengri" name = "tengri"
version = "0.12.0" version = "0.13.0"
dependencies = [ dependencies = [
"tengri_dsl", "tengri_dsl",
"tengri_input", "tengri_input",
@ -944,7 +944,7 @@ dependencies = [
[[package]] [[package]]
name = "tengri_dsl" name = "tengri_dsl"
version = "0.12.0" version = "0.13.0"
dependencies = [ dependencies = [
"itertools 0.14.0", "itertools 0.14.0",
"konst", "konst",
@ -955,7 +955,7 @@ dependencies = [
[[package]] [[package]]
name = "tengri_input" name = "tengri_input"
version = "0.12.0" version = "0.13.0"
dependencies = [ dependencies = [
"tengri_dsl", "tengri_dsl",
"tengri_tui", "tengri_tui",
@ -963,7 +963,7 @@ dependencies = [
[[package]] [[package]]
name = "tengri_output" name = "tengri_output"
version = "0.12.0" version = "0.13.0"
dependencies = [ dependencies = [
"proptest", "proptest",
"proptest-derive", "proptest-derive",
@ -974,7 +974,7 @@ dependencies = [
[[package]] [[package]]
name = "tengri_tui" name = "tengri_tui"
version = "0.12.0" version = "0.13.0"
dependencies = [ dependencies = [
"atomic_float", "atomic_float",
"better-panic", "better-panic",

View file

@ -1,5 +1,5 @@
[workspace.package] [workspace.package]
version = "0.12.0" version = "0.13.0"
[workspace] [workspace]
resolver = "2" resolver = "2"

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") 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> { impl<'a> From<SourceIter<'a>> for TokenIter<'a> {
fn from (source: SourceIter<'a>) -> Self{ fn from (source: SourceIter<'a>) -> Self{
Self(source) Self(source)

View file

@ -24,6 +24,7 @@
} }
} }
/// Implement `Context` for one or more base structs, types, and keys. */
#[macro_export] macro_rules! expose { #[macro_export] macro_rules! expose {
($([$self:ident:$State:ty] $(([$($Type:tt)*] $(($pat:literal $expr:expr))*))*)*) => { ($([$self:ident:$State:ty] $(([$($Type:tt)*] $(($pat:literal $expr:expr))*))*)*) => {
$(expose!(@impl [$self: $State] { $([$($Type)*] => { $($pat => $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 { #[macro_export] macro_rules! impose {
([$self:ident:$Struct:ty] $(($Command:ty : $(($cmd:literal $args:tt $result:expr))*))*) => { ([$self:ident:$Struct:ty] $(($Command:ty : $(($cmd:literal $args:tt $result:expr))*))*) => {
$(atom_command!($Command: |$self: $Struct| { $(($cmd $args $result))* });)* $(atom_command!($Command: |$self: $Struct| { $(($cmd $args $result))* });)*

View file

@ -114,6 +114,20 @@ mod dsl_macros;
Ok(()) 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> { //#[cfg(test)] #[test] fn test_examples () -> Result<(), ParseError> {
//// Let's pretend to render some view. //// Let's pretend to render some view.
//let source = include_str!("../../tek/src/view_arranger.edn"); //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, /// A collection of pre-configured mappings of input events to commands,
/// which may be made available subject to given conditions. /// 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)>, __: &'a PhantomData<(S, C, I)>,
pub layers: Vec<( pub layers: Vec<(InputLayerCond<'a, S>, M)>,
fn(&S)->bool,
M
)>,
} }
impl<'a, S, C, I, M> Default for InputMap<'a, S, C, I, M> impl<'a, S, C, I, M> Default for InputMap<'a, S, C, I, M>
@ -108,15 +112,15 @@ where
self self
} }
pub fn add_layer (&mut self, keymap: M) -> &mut 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 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.add_layer_if(condition, keymap);
self self
} }
pub fn add_layer_if (&mut self, condition: fn(&S)->bool, keymap: M) -> &mut Self { pub fn add_layer_if (&mut self, condition: InputLayerCond<'a, S>, keymap: M) -> &mut Self {
self.layers.push((condition, keymap)); self.layers.push((Box::new(condition), keymap));
self self
} }
} }

View file

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