input: add InputMap; dsl/output/tui: Atom->Dsl
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-04-28 04:49:00 +03:00
parent 47b7f7e7f9
commit 35ad371205
15 changed files with 374 additions and 277 deletions

View file

@ -1,48 +1,56 @@
use crate::*;
pub trait TryFromAtom<'a, T>: Sized {
fn try_from_expr (_state: &'a T, _iter: TokenIter<'a>) -> Option<Self> { None }
pub trait TryFromDsl<'a, T>: Sized {
fn try_from_expr (_state: &'a T, _iter: TokenIter<'a>) -> Option<Self> {
None
}
fn try_from_atom (state: &'a T, value: Value<'a>) -> Option<Self> {
if let Exp(0, iter) = value { return Self::try_from_expr(state, iter) }
None
}
}
pub trait TryIntoAtom<T>: Sized {
pub trait TryIntoDsl<T>: Sized {
fn try_into_atom (&self) -> Option<Value>;
}
/// Map EDN tokens to parameters of a given type for a given context
pub trait Context<U>: Sized {
fn get (&self, _atom: &Value) -> Option<U> {
None
}
fn get_or_fail (&self, atom: &Value) -> U {
self.get(atom).expect("no value")
fn get_or_fail (&self, dsl: &Value) -> U {
self.get(dsl).expect("no value")
}
}
impl<T: Context<U>, U> Context<U> for &T {
fn get (&self, atom: &Value) -> Option<U> {
(*self).get(atom)
fn get (&self, dsl: &Value) -> Option<U> {
(*self).get(dsl)
}
fn get_or_fail (&self, atom: &Value) -> U {
(*self).get_or_fail(atom)
fn get_or_fail (&self, dsl: &Value) -> U {
(*self).get_or_fail(dsl)
}
}
impl<T: Context<U>, U> Context<U> for Option<T> {
fn get (&self, atom: &Value) -> Option<U> {
self.as_ref().map(|s|s.get(atom)).flatten()
fn get (&self, dsl: &Value) -> Option<U> {
self.as_ref().map(|s|s.get(dsl)).flatten()
}
fn get_or_fail (&self, atom: &Value) -> U {
self.as_ref().map(|s|s.get_or_fail(atom)).expect("no provider")
fn get_or_fail (&self, dsl: &Value) -> U {
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, atom: &Value) -> Option<$type> {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match atom { $(Sym($pat) => $expr,)* _ => return None })
Some(match dsl { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
@ -50,13 +58,14 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
($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, atom: &Value) -> Option<$type> {
fn get (&$lt $self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match atom { $(Sym($pat) => $expr,)* _ => return None })
Some(match dsl { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
}
/// Implement `Context` for a context and numeric type.
///
/// This enables support for numeric literals.
@ -64,22 +73,23 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
// 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, atom: &Value) -> Option<$type> {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match atom { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
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, atom: &Value) -> Option<$type> {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match atom { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
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.
@ -87,14 +97,14 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
// 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, atom: &Value) -> Option<$type> {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match atom {
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, atom)
_ => return Context::get(self, dsl)
})
}
}
@ -102,9 +112,9 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
// 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, atom: &Value) -> Option<$type> {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match atom {
Some(match dsl {
Num(n) => match *n { 0 => false, _ => true },
Sym(":false") | Sym(":f") => false,
Sym(":true") | Sym(":t") => true,
@ -115,6 +125,7 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
}
};
}
#[cfg(test)] #[test] fn test_edn_context () {
struct Test;
provide_bool!(bool: |self: Test|{

View file

@ -32,7 +32,7 @@ mod dsl_macros;
////include_str!("../../tui/examples/edn01.edn"),
////include_str!("../../tui/examples/edn02.edn"),
////] {
//////let items = Atom::read_all(example)?;
//////let items = Dsl::read_all(example)?;
//////panic!("{layout:?}");
//////let content = <dyn ViewContext<::tengri_engine::tui::Tui>>::from(&layout);
////}