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 }) } } )*)? } } pub struct View<'a, T>(pub &'a T, pub TokenIter<'a>); impl<'a, O: Output + 'a, T: ViewContext<'a, O>> Content for View<'a, T> { fn content (&self) -> impl Render { 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: /// /// * `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) } 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 for AtomView<'a, O, S, A> where O: Output, S: ViewContext<'a, O>, A: Send + Sync { fn content (&self) -> impl Render { 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 + Context + Context + Context + 'a>> {} /// Provides values to the template pub trait ViewContext<'a, E: Output>: Sized + Send + Sync + Context + Context + Context + Context + 'a>> { fn get_bool (&self, atom: &Value) -> Option { 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 { Some(match atom {Num(n) => *n, _ => return Context::get(self, atom)}) } fn get_unit (&self, atom: &Value) -> Option { Some(match atom {Num(n) => E::Unit::from(*n as u16), _ => return Context::get(self, atom)}) } fn get_content (&self, atom: &Value) -> Option + 'a>> where E: 'a { try_delegate!(self, *atom, When::>); try_delegate!(self, *atom, Either::, RenderBox<'a, E>>); try_delegate!(self, *atom, Align::>); try_delegate!(self, *atom, Bsp::, RenderBox<'a, E>>); try_delegate!(self, *atom, Fill::>); 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>; #[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 for $Struct { fn try_from_atom ($state: &T, atom: Value) -> Option { if let Value::Exp(0, $atoms) = atom { $body; } None } } } } /// 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 Context + '_>> for $State { fn get (&$self, atom: &Value) -> Option + '_>> { Some(match atom { $(Value::Sym($pat) => $expr,)* _ => return None }) } } }; ($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { impl Context + '_>> for $State { fn get (&$self, atom: &Value) -> Option + '_>> { Some(match atom { $(Value::Sym($pat) => $expr,)* _ => return None }) } } } }