mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
ughhh needs special case
This commit is contained in:
parent
cf1fd5b45a
commit
a362028ae7
4 changed files with 124 additions and 97 deletions
|
|
@ -7,44 +7,35 @@ use Value::*;
|
|||
/// * render callback (implementation of [Content])
|
||||
/// * value providers (implementations of [Context])
|
||||
#[macro_export] macro_rules! view {
|
||||
($Output:ty: |$self:ident: $App:ty| $content:expr; {
|
||||
($Output:ty: |$self:ident: $App:ty| $content:expr $(;{
|
||||
$( $type:ty { $($sym:literal => $value:expr),* } );*
|
||||
}) => {
|
||||
})?) => {
|
||||
impl Content<$Output> for $App {
|
||||
fn content(&$self) -> impl Render<$Output> { $content }
|
||||
}
|
||||
$(
|
||||
$($(
|
||||
impl Context<$type> for $App {
|
||||
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
|
||||
}
|
||||
}
|
||||
)*
|
||||
)*)?
|
||||
}
|
||||
}
|
||||
/// Implements `Context` for content components and 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 (&$self, atom: &Value) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
Some(match atom.to_ref() {
|
||||
$(Atom::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 (&$self, atom: &Value) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
Some(match atom.to_ref() {
|
||||
$(Atom::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
})
|
||||
|
||||
pub struct View<'a, T>(pub &'a T, pub TokenIter<'a>);
|
||||
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() {
|
||||
if let Some(content) = self.0.get_content(&value) {
|
||||
return Some(content)
|
||||
}
|
||||
}
|
||||
return None
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders from EDN source and context.
|
||||
///
|
||||
/// Generic over:
|
||||
|
|
@ -163,3 +154,28 @@ pub type AtomRenderCallback<'a, O, State> =
|
|||
}
|
||||
}
|
||||
}
|
||||
/// Implement `Context` for a context and content type.
|
||||
///
|
||||
/// This enables support for layout expressions.
|
||||
#[macro_export] macro_rules! provide_content {
|
||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<E: Output> Context<Box<dyn Render<E> + '_>> for $State {
|
||||
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<E> + '_>> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl Context<Box<dyn Render<$Output> + '_>> for $State {
|
||||
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<$Output> + '_>> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue