use crate::*; #[macro_export] macro_rules! view { ($Output:ty: |$self:ident: $State:ty| $expr:expr; { $($sym:literal => $body:expr),* $(,)? }) => { impl Content<$Output> for $State { fn content (&$self) -> impl Render<$Output> { $expr } } impl<'a> ViewContext<'a, $Output> for $State { fn get_content_sym (&'a $self, value: &Value<'a>) -> Option> { if let Value::Sym(s) = value { match *s { $($sym => Some($body.boxed()),)* _ => None } } else { panic!("expected content, got: {value:?}") } } } } } // An ephemeral wrapper around view state and view description, // that is meant to be constructed and returned from [Content::content]. #[cfg(feature = "dsl")] pub struct View<'a, T>( pub &'a T, pub TokenIter<'a> ); #[cfg(feature = "dsl")] impl<'a, O: Output + 'a, T: ViewContext<'a, O>> Content for View<'a, T> { fn content (&self) -> impl Render { let mut 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 } } // Provides components to the view. #[cfg(feature = "dsl")] pub trait ViewContext<'a, E: Output + 'a>: Send + Sync + Context + Context + Context { fn get_content (&'a self, value: &Value<'a>) -> Option> { match value { Value::Sym(_) => self.get_content_sym(value), Value::Exp(_, _) => self.get_content_exp(value), _ => panic!("only :symbols and (expressions) accepted here") } } fn get_content_sym (&'a self, value: &Value<'a>) -> Option>; fn get_content_exp (&'a self, value: &Value<'a>) -> Option> { try_delegate!(self, *value, When::>); try_delegate!(self, *value, Either::, RenderBox<'a, E>>); try_delegate!(self, *value, Align::>); try_delegate!(self, *value, Bsp::, RenderBox<'a, E>>); try_delegate!(self, *value, Fill::>); try_delegate!(self, *value, Fixed::<_, RenderBox<'a, E>>); try_delegate!(self, *value, Min::<_, RenderBox<'a, E>>); try_delegate!(self, *value, Max::<_, RenderBox<'a, E>>); try_delegate!(self, *value, Shrink::<_, RenderBox<'a, E>>); try_delegate!(self, *value, Expand::<_, RenderBox<'a, E>>); try_delegate!(self, *value, Push::<_, RenderBox<'a, E>>); try_delegate!(self, *value, Pull::<_, RenderBox<'a, E>>); try_delegate!(self, *value, Margin::<_, RenderBox<'a, E>>); try_delegate!(self, *value, Padding::<_, RenderBox<'a, E>>); None } } #[cfg(feature = "dsl")] #[macro_export] macro_rules! try_delegate { ($s:ident, $dsl:expr, $T:ty) => { if let Some(value) = <$T>::try_from_atom($s, $dsl) { return Some(value.boxed()) } } } #[cfg(feature = "dsl")] #[macro_export] macro_rules! try_from_expr { (<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident, $iter:ident|$body:expr) => { impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromDsl<$l, T> for $Struct { fn try_from_expr ($state: &$l T, $iter: TokenIter<'a>) -> Option { let mut $iter = $iter.clone(); $body; None } } } }