ughhh needs special case

This commit is contained in:
🪞👃🪞 2025-01-18 16:03:06 +01:00
parent cf1fd5b45a
commit a362028ae7
4 changed files with 124 additions and 97 deletions

View file

@ -92,28 +92,36 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
}
};
}
/// Implement `Context` for a context and content type.
/// Implement `Context` for a context and the boolean type.
///
/// This enables support for layout expressions.
#[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 Value) -> Option<Box<dyn Render<E> + 'a>> {
Some(match (atom.kind(), atom.text()) {
/// 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, atom: &Value) -> Option<$type> {
Some(match atom {
Value::Num(n) => match *n { 0 => false, _ => true },
Value::Sym(":false") | Value::Sym(":f") => false,
Value::Sym(":true") | Value::Sym(":t") => true,
$(Value::Sym($pat) => $expr,)*
_ => return Context::get(self, atom)
})
}
}
};
// 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> {
Some(match atom {
Value::Num(n) => match *n { 0 => false, _ => true },
Value::Sym(":false") | Value::Sym(":f") => false,
Value::Sym(":true") | Value::Sym(":t") => true,
$(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 Value) -> Option<Box<dyn Render<$Output> + 'a>> {
Some(match (atom.kind(), atom.text()) {
$(Value::Sym($pat) => $expr,)*
_ => return None
})
}
}
}
}

View file

@ -1,5 +1,5 @@
use crate::*;
#[derive(Copy, Clone, Debug, PartialEq)] pub struct TokenIter<'a>(pub &'a str);
#[derive(Copy, Clone, Debug, Default, PartialEq)] pub struct TokenIter<'a>(pub &'a str);
const_iter!(<'a>|self: TokenIter<'a>| => Token<'a> => self.next_mut().map(|(result, _)|result));
impl<'a> From<&'a str> for TokenIter<'a> {fn from (source: &'a str) -> Self{Self::new(source)}}
impl<'a> TokenIter<'a> {