mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
once again, why did i begin to refactor this
This commit is contained in:
parent
297f9b30df
commit
798de37172
15 changed files with 582 additions and 602 deletions
|
|
@ -1,11 +1,10 @@
|
|||
use crate::*;
|
||||
use std::{sync::Arc, marker::PhantomData};
|
||||
use Atom::*;
|
||||
/// Define an EDN-backed view.
|
||||
///
|
||||
/// This consists of:
|
||||
/// * render callback (implementation of [Content])
|
||||
/// * value providers (implementations of [EdnProvide])
|
||||
/// * value providers (implementations of [Context])
|
||||
#[macro_export] macro_rules! edn_view {
|
||||
($Output:ty: |$self:ident: $App:ty| $content:expr; {
|
||||
$( $type:ty { $($sym:literal => $value:expr),* } );*
|
||||
|
|
@ -14,19 +13,19 @@ use Atom::*;
|
|||
fn content(&$self) -> impl Render<$Output> { $content }
|
||||
}
|
||||
$(
|
||||
impl<'a> EdnProvide<'a, $type> for $App {
|
||||
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<$type> {
|
||||
impl<'a> Context<'a, $type> for $App {
|
||||
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<$type> {
|
||||
Some(match edn.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
/// Implements `EdnProvide` for content components and expressions
|
||||
/// Implements `Context` 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 (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
|
||||
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
$(Atom::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
|
|
@ -35,8 +34,8 @@ use Atom::*;
|
|||
}
|
||||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> EdnProvide<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
||||
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
||||
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
$(Atom::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
|
|
@ -46,14 +45,14 @@ use Atom::*;
|
|||
}
|
||||
}
|
||||
/// Renders from EDN source and context.
|
||||
#[derive(Default)] pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> {
|
||||
#[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: EdnViewData<'a, E> + std::fmt::Debug> std::fmt::Debug for EdnView<'a, E, T> {
|
||||
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(_) =>
|
||||
|
|
@ -66,24 +65,24 @@ impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> std::fmt::Debug for
|
|||
}
|
||||
}
|
||||
}
|
||||
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> 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: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
|
||||
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<Atom<impl AsRef<str>>>) -> Self {
|
||||
pub fn from_items (state: T, items: Vec<impl Atom<'a>>) -> Self {
|
||||
Self::Ok(state, Atom::Exp(items).to_arc())
|
||||
}
|
||||
}
|
||||
impl<E: Output, T: for<'a>EdnViewData<'a, E> + Send + Sync + std::fmt::Debug> Content<E> for EdnView<'_, E, T> {
|
||||
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),
|
||||
|
|
@ -107,31 +106,31 @@ macro_rules! edn_try_delegate {
|
|||
}
|
||||
|
||||
/// 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>>
|
||||
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 Atom<impl AsRef<str>>) -> Option<bool> {
|
||||
fn get_bool (&'a self, item: &'a impl Atom<'a>) -> Option<bool> {
|
||||
Some(match &item {
|
||||
Sym(s) => match s.as_ref() {
|
||||
":false" | ":f" => false,
|
||||
":true" | ":t" => true,
|
||||
_ => return EdnProvide::get(self, item)
|
||||
_ => return Context::get(self, item)
|
||||
},
|
||||
Num(0) => false,
|
||||
Num(_) => true,
|
||||
_ => return EdnProvide::get(self, item)
|
||||
_ => return Context::get(self, item)
|
||||
})
|
||||
}
|
||||
fn get_usize (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<usize> {
|
||||
Some(match &item { Num(n) => *n, _ => return EdnProvide::get(self, item) })
|
||||
fn get_usize (&'a self, item: &'a impl Atom<'a>) -> Option<usize> {
|
||||
Some(match &item { Num(n) => *n, _ => return Context::get(self, item) })
|
||||
}
|
||||
fn get_unit (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<E::Unit> {
|
||||
Some(match &item { Num(n) => (*n as u16).into(), _ => return EdnProvide::get(self, item) })
|
||||
fn get_unit (&'a self, item: &'a impl Atom<'a>) -> Option<E::Unit> {
|
||||
Some(match &item { Num(n) => (*n as u16).into(), _ => return Context::get(self, item) })
|
||||
}
|
||||
fn get_content (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
fn get_content (&'a self, item: &'a impl Atom<'a>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
Some(match item {
|
||||
Nil => Box::new(()),
|
||||
Exp(ref e) => {
|
||||
|
|
@ -149,9 +148,9 @@ pub trait EdnViewData<'a, E: Output>:
|
|||
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>>);
|
||||
EdnProvide::get_or_fail(self, &item)
|
||||
Context::get_or_fail(self, &item)
|
||||
},
|
||||
_ => EdnProvide::get_or_fail(self, &item)
|
||||
_ => Context::get_or_fail(self, &item)
|
||||
})
|
||||
//panic!("no content")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue