mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
163 lines
7.2 KiB
Rust
163 lines
7.2 KiB
Rust
use crate::*;
|
|
use std::marker::PhantomData;
|
|
use EdnItem::*;
|
|
|
|
#[macro_export] macro_rules! edn_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> EdnProvide<'a, $type> for $App {
|
|
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> {
|
|
Some(match edn.to_ref() { $(EdnItem::Sym($sym) => $value,)* _ => return None })
|
|
}
|
|
}
|
|
)*
|
|
}
|
|
}
|
|
|
|
/// Implements `EdnProvide` for content components and expressions
|
|
#[macro_export] macro_rules! edn_provide_content {
|
|
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
|
impl<'a, E: Output> EdnProvide<'a, Box<dyn Render<E> + 'a>> for $State {
|
|
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<Box<dyn Render<E> + 'a>> {
|
|
Some(match edn.to_ref() {
|
|
$(EdnItem::Sym($pat) => $expr),*,
|
|
_ => return None
|
|
})
|
|
}
|
|
}
|
|
};
|
|
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
|
impl<'a> EdnProvide<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
|
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<Box<dyn Render<$Output> + 'a>> {
|
|
Some(match edn.to_ref() {
|
|
$(EdnItem::Sym($pat) => $expr),*,
|
|
_ => return None
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// Renders from EDN source and context.
|
|
#[derive(Default)] pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> {
|
|
#[default] Inert,
|
|
Ok(T, EdnItem<&'a 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: EdnViewData<'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> EdnViewData<'a, E> for T where T:
|
|
EdnProvide<'a, bool> +
|
|
EdnProvide<'a, usize> +
|
|
EdnProvide<'a, E::Unit> +
|
|
EdnProvide<'a, Box<dyn Render<E> + 'a>>
|
|
{}
|
|
impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
|
|
pub fn from_source (state: T, source: &'a str) -> Self {
|
|
match EdnItem::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<EdnItem<&'a str>>) -> Self {
|
|
Self::Ok(state, EdnItem::Exp(items))
|
|
}
|
|
}
|
|
impl<E: Output, T: for<'a>EdnViewData<'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!()
|
|
}
|
|
}
|
|
}
|
|
/// Provides values to the template
|
|
pub trait EdnViewData<'a, E: Output>:
|
|
EdnProvide<'a, bool> +
|
|
EdnProvide<'a, usize> +
|
|
EdnProvide<'a, E::Unit> +
|
|
EdnProvide<'a, Box<dyn Render<E> + 'a>>
|
|
{
|
|
fn get_bool (&'a self, item: &'a EdnItem<&str>) -> Option<bool> {
|
|
Some(match &item {
|
|
Sym(":false") | Sym(":f") | Num(0) => false,
|
|
Sym(":true") | Sym(":t") | Num(_) => true,
|
|
_ => return EdnProvide::get(self, item)
|
|
})
|
|
}
|
|
fn get_usize (&'a self, item: &'a EdnItem<&str>) -> Option<usize> {
|
|
Some(match &item { Num(n) => *n, _ => return EdnProvide::get(self, item) })
|
|
}
|
|
fn get_unit (&'a self, item: &'a EdnItem<&str>) -> Option<E::Unit> {
|
|
Some(match &item { Num(n) => (*n as u16).into(), _ => return EdnProvide::get(self, item) })
|
|
}
|
|
fn get_content (&'a self, item: &'a EdnItem<&'a str>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
|
Some(match item {
|
|
Nil => Box::new(()),
|
|
Exp(e) => if let [head, tail @ ..] = e.as_slice() {
|
|
if let Some(builtin) = When::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Either::<_, RenderBox<'a, E>, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Align::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Bsp::<_, RenderBox<'a, E>, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Fill::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Fixed::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Min::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Max::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Shrink::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Expand::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Push::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Pull::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Margin::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else if let Some(builtin) = Padding::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
|
builtin.boxed()
|
|
} else {
|
|
EdnProvide::get_or_fail(self, &item)
|
|
}
|
|
} else {
|
|
EdnProvide::get_or_fail(self, &item)
|
|
},
|
|
_ => EdnProvide::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>>;
|