tek/output/src/view.rs

165 lines
6.2 KiB
Rust

use crate::*;
use std::{sync::Arc, fmt::Debug};
use Value::*;
/// Define an EDN-backed view.
///
/// This consists of:
/// * render callback (implementation of [Content])
/// * value providers (implementations of [Context])
#[macro_export] macro_rules! view {
($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
})
}
}
}
}
/// Renders from EDN source and context.
///
/// Generic over:
///
/// * `O` - Output target.
/// * `S` - State provider.
/// * `A` - Atom storage type.
#[derive(Default, Clone)] pub enum AtomView<'a, O, S, A> {
_Unused(std::marker::PhantomData<&'a O>),
#[default] Inert,
Src(S, &'a str),
Ref(S, &'a A),
Own(S, A),
Err(Arc<str>)
}
impl<'a, O, S, A> Debug for AtomView<'a, O, S, A>
where S: Debug, A: Debug {
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
Self::Inert =>
write!(f, "AtomView::Inert"),
Self::Src(state, view) =>
write!(f, "AtomView::Src(state={state:?} view={view:?}"),
Self::Ref(state, view) =>
write!(f, "AtomView::Ref(state={state:?} view={view:?}"),
Self::Own(state, view) =>
write!(f, "AtomView::Arc(state={state:?} view={view:?}"),
Self::Err(error) =>
write!(f, "AtomView::Err({error})"),
_ => unreachable!()
}
}
}
impl<'a, O, S, A> Content<O> for AtomView<'a, O, S, A>
where O: Output, S: ViewContext<'a, O>, A: Send + Sync {
fn content (&self) -> impl Render<O> {
match self {
Self::Inert => { panic!("inert rendered") },
Self::Err(e) => { panic!("render error: {e}") },
Self::Src(state, source) => {},
Self::Ref(state, atom) => {},
Self::Own(state, atom) => {},
_ => unreachable!()
}
}
}
impl<'a, E: Output, T> ViewContext<'a, E> for T where T: Sized + Send + Sync
+ Context<bool>
+ Context<usize>
+ Context<E::Unit>
+ Context<Box<dyn Render<E> + 'a>> {}
/// Provides values to the template
pub trait ViewContext<'a, E: Output>: Sized + Send + Sync
+ Context<bool>
+ Context<usize>
+ Context<E::Unit>
+ Context<Box<dyn Render<E> + 'a>>
{
fn get_bool (&self, atom: &Value) -> Option<bool> {
Some(match atom {
Num(n) => match *n { 0 => false, _ => true },
Sym(x) => match *x {
":false" | ":f" => false, ":true" | ":t" => true,
_ => return Context::get(self, atom)
},
_ => return Context::get(self, atom)
})
}
fn get_usize (&self, atom: &Value) -> Option<usize> {
Some(match atom {Num(n) => *n, _ => return Context::get(self, atom)})
}
fn get_unit (&self, atom: &Value) -> Option<E::Unit> {
Some(match atom {Num(n) => E::Unit::from(*n as u16), _ => return Context::get(self, atom)})
}
fn get_content (&self, atom: &Value) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
try_delegate!(self, *atom, When::<RenderBox<'a, E>>);
try_delegate!(self, *atom, Either::<RenderBox<'a, E>, RenderBox<'a, E>>);
try_delegate!(self, *atom, Align::<RenderBox<'a, E>>);
try_delegate!(self, *atom, Bsp::<RenderBox<'a, E>, RenderBox<'a, E>>);
try_delegate!(self, *atom, Fill::<RenderBox<'a, E>>);
try_delegate!(self, *atom, Fixed::<_, RenderBox<'a, E>>);
try_delegate!(self, *atom, Min::<_, RenderBox<'a, E>>);
try_delegate!(self, *atom, Max::<_, RenderBox<'a, E>>);
try_delegate!(self, *atom, Shrink::<_, RenderBox<'a, E>>);
try_delegate!(self, *atom, Expand::<_, RenderBox<'a, E>>);
try_delegate!(self, *atom, Push::<_, RenderBox<'a, E>>);
try_delegate!(self, *atom, Pull::<_, RenderBox<'a, E>>);
try_delegate!(self, *atom, Margin::<_, RenderBox<'a, E>>);
try_delegate!(self, *atom, Padding::<_, RenderBox<'a, E>>);
Some(Context::get_or_fail(self, atom))
}
}
#[macro_export] macro_rules! try_delegate {
($s:ident, $atom:expr, $T:ty) => {
if let Some(value) = <$T>::try_from_atom($s, $atom) {
return Some(value.boxed())
}
}
}
// A function that returns a `RenderBox.
pub type AtomCallback<'a, O, State> =
dyn Fn(&'a State)-> RenderBox<'a, O> + Send + Sync + 'a;
// A box containing a function that returns a `RenderBox.
pub type AtomRenderCallback<'a, O, State> =
Box<AtomCallback<'a, O, State>>;
#[macro_export] macro_rules! try_from_expr {
(<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident, $atoms:ident|$body:expr) => {
impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<T> for $Struct {
fn try_from_atom ($state: &T, atom: Value) -> Option<Self> {
if let Value::Exp(0, $atoms) = atom {
$body;
}
None
}
}
}
}