mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
163 lines
6.5 KiB
Rust
163 lines
6.5 KiB
Rust
use crate::*;
|
|
use std::{sync::Arc, marker::PhantomData};
|
|
/// 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<'a> Context<'a, $type> for $App {
|
|
fn get (&'a $self, edn: &'a impl Atom) -> Option<$type> {
|
|
Some(match edn.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 (&'a $self, edn: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> {
|
|
Some(match edn.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 (&'a $self, edn: &'a impl Atom) -> Option<Box<dyn Render<$Output> + 'a>> {
|
|
Some(match edn.to_ref() {
|
|
$(Atom::Sym($pat) => $expr),*,
|
|
_ => return None
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// Renders from EDN source and context.
|
|
#[derive(Default)] pub enum EdnView<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> {
|
|
#[default] Inert,
|
|
Ok(T, Atom<Arc<str>>),
|
|
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
|
|
Err(String),
|
|
_Unused(PhantomData<&'a E>),
|
|
}
|
|
impl<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> std::fmt::Debug for EdnView<'a, E, T> {
|
|
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
match self {
|
|
Self::Inert | Self::_Unused(_) =>
|
|
write!(f, "EdnView::Inert"),
|
|
Self::Ok(state, view) =>
|
|
write!(f, "EdnView::Ok(state={state:?} view={view:?}"),
|
|
Self::Err(error) =>
|
|
write!(f, "EdnView::Err({error})"),
|
|
_ => unreachable!()
|
|
}
|
|
}
|
|
}
|
|
impl<'a, E: Output, T> ViewContext<'a, E> for T where T:
|
|
Context<'a, bool> +
|
|
Context<'a, usize> +
|
|
Context<'a, E::Unit> +
|
|
Context<'a, Box<dyn Render<E> + 'a>>
|
|
{}
|
|
impl<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
|
|
pub fn from_source (state: T, source: &'a str) -> Self {
|
|
match Atom::read_one(&source) {
|
|
Ok((layout, _)) => Self::Ok(state, layout),
|
|
Err(error) => Self::Err(format!("{error} in {source}"))
|
|
}
|
|
}
|
|
pub fn from_items (state: T, items: Vec<impl Atom>) -> Self {
|
|
Self::Ok(state, Atom::Exp(items).to_arc())
|
|
}
|
|
}
|
|
impl<E: Output, T: for<'a>ViewContext<'a, E> + Send + Sync + std::fmt::Debug> Content<E> for EdnView<'_, E, T> {
|
|
fn content (&self) -> impl Render<E> {
|
|
match self {
|
|
Self::Ok(state, layout) => state.get_content(layout),
|
|
Self::Err(_error) => {
|
|
panic!("{self:?}");
|
|
//TODO:&format!("EdnView error: {error:?}")) // FIXME: String is not Render
|
|
},
|
|
_ => todo!()
|
|
}
|
|
}
|
|
}
|
|
|
|
macro_rules! edn_try_delegate {
|
|
($s:ident, $e:expr, $T:ty) => {
|
|
if let [head, tail @ ..] = $e.as_slice() {
|
|
if let Some(content) = <$T>::try_from_edn($s, head, tail) {
|
|
return Some(content.boxed())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Provides values to the template
|
|
pub trait ViewContext<'a, E: Output>:
|
|
Context<'a, bool> +
|
|
Context<'a, usize> +
|
|
Context<'a, E::Unit> +
|
|
Context<'a, Box<dyn Render<E> + 'a>>
|
|
{
|
|
fn get_bool (&'a self, item: &'a impl Atom) -> Option<bool> {
|
|
Some(match &item {
|
|
Sym(s) => match s.as_ref() {
|
|
":false" | ":f" => false,
|
|
":true" | ":t" => true,
|
|
_ => return Context::get(self, item)
|
|
},
|
|
Num(0) => false,
|
|
Num(_) => true,
|
|
_ => return Context::get(self, item)
|
|
})
|
|
}
|
|
fn get_usize (&'a self, item: &'a impl Atom) -> Option<usize> {
|
|
Some(match &item { Num(n) => *n, _ => return Context::get(self, item) })
|
|
}
|
|
fn get_unit (&'a self, item: &'a impl Atom) -> Option<E::Unit> {
|
|
Some(match &item { Num(n) => (*n as u16).into(), _ => return Context::get(self, item) })
|
|
}
|
|
fn get_content (&'a self, item: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
|
Some(match item {
|
|
Nil => Box::new(()),
|
|
Exp(ref e) => {
|
|
edn_try_delegate!(self, e, When::<_, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Either::<_, RenderBox<'a, E>, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Align::<_, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Bsp::<_, RenderBox<'a, E>, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Fill::<_, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Fixed::<_, _, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Min::<_, _, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Max::<_, _, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Shrink::<_, _, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Expand::<_, _, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Push::<_, _, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Pull::<_, _, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Margin::<_, _, RenderBox<'a, E>>);
|
|
edn_try_delegate!(self, e, Padding::<_, _, RenderBox<'a, E>>);
|
|
Context::get_or_fail(self, &item)
|
|
},
|
|
_ => Context::get_or_fail(self, &item)
|
|
})
|
|
//panic!("no content")
|
|
}
|
|
}
|
|
// A function that returns a `RenderBox.
|
|
pub type EdnCallback<'a, O, State> =
|
|
dyn Fn(&'a State)-> RenderBox<'a, O> + Send + Sync + 'a;
|
|
// A box containing a function that returns a `RenderBox.
|
|
pub type EdnRenderCallback<'a, O, State> =
|
|
Box<EdnCallback<'a, O, State>>;
|