mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
remove Atom. almost there
This commit is contained in:
parent
dc7b713108
commit
cf1fd5b45a
20 changed files with 539 additions and 739 deletions
|
|
@ -1,26 +1,38 @@
|
|||
use crate::*;
|
||||
/// Map EDN tokens to parameters of a given type for a given context
|
||||
pub trait Context<U>: Sized {
|
||||
fn get (&self, _atom: &impl Atom) -> Option<U> {
|
||||
pub trait TryFromAtom<T>: Sized {
|
||||
fn try_from_atom (state: &T, value: Value) -> Option<Self> {
|
||||
if let Value::Exp(0, iter) = value { return Self::try_from_expr(state, iter) }
|
||||
None
|
||||
}
|
||||
fn get_or_fail (&self, atom: &impl Atom) -> U {
|
||||
fn try_from_expr (_state: &T, _iter: TokenIter) -> Option<Self> {
|
||||
None
|
||||
}
|
||||
}
|
||||
pub trait TryIntoAtom<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")
|
||||
}
|
||||
}
|
||||
impl<T: Context<U>, U> Context<U> for &T {
|
||||
fn get (&self, atom: &impl Atom) -> Option<U> {
|
||||
fn get (&self, atom: &Value) -> Option<U> {
|
||||
(*self).get(atom)
|
||||
}
|
||||
fn get_or_fail (&self, atom: &impl Atom) -> U {
|
||||
fn get_or_fail (&self, atom: &Value) -> U {
|
||||
(*self).get_or_fail(atom)
|
||||
}
|
||||
}
|
||||
impl<T: Context<U>, U> Context<U> for Option<T> {
|
||||
fn get (&self, atom: &impl Atom) -> Option<U> {
|
||||
fn get (&self, atom: &Value) -> Option<U> {
|
||||
self.as_ref().map(|s|s.get(atom)).flatten()
|
||||
}
|
||||
fn get_or_fail (&self, atom: &impl Atom) -> U {
|
||||
fn get_or_fail (&self, atom: &Value) -> U {
|
||||
self.as_ref().map(|s|s.get_or_fail(atom)).expect("no provider")
|
||||
}
|
||||
}
|
||||
|
|
@ -29,9 +41,10 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
|
|||
// Provide a value to the EDN template
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl Context<$type> for $State {
|
||||
fn get (&$self, atom: &impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
#[allow(unreachable_code)]
|
||||
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
|
|
@ -40,9 +53,10 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
|
|||
// Provide a value more generically
|
||||
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<$lt> Context<$lt, $type> for $State {
|
||||
fn get (&$lt $self, atom: &impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
#[allow(unreachable_code)]
|
||||
fn get (&$lt $self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
|
|
@ -56,10 +70,10 @@ 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: &impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
(TokenKind::Num, _) => atom.num() as $type,
|
||||
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
Value::Num(n) => *n as $type,
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
|
|
@ -68,10 +82,10 @@ 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: &impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
(TokenKind::Num, _) => atom.num() as $type,
|
||||
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
Value::Num(n) => *n as $type,
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
|
|
@ -84,17 +98,21 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
|
|||
#[macro_export] macro_rules! provide_content {
|
||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
use RefAtom::*;
|
||||
Some(match atom.to_ref() { $(RefAtom::Sym($pat) => $expr),*, _ => return None })
|
||||
fn get (&'a $self, atom: &'a Value) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
use RefAtom::*;
|
||||
Some(match atom.to_ref() { $(Sym($pat) => $expr),*, _ => return None })
|
||||
fn get (&'a $self, atom: &'a Value) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue