mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
generalize EdnItem.
maybe should rename it to Atom? ~90 instances of it
This commit is contained in:
parent
1b9da07280
commit
143cd24e09
20 changed files with 314 additions and 286 deletions
|
|
@ -1,7 +1,11 @@
|
|||
use crate::*;
|
||||
use std::marker::PhantomData;
|
||||
use std::{sync::Arc, marker::PhantomData};
|
||||
use EdnItem::*;
|
||||
|
||||
/// Define an EDN-backed view.
|
||||
///
|
||||
/// This consists of:
|
||||
/// * render callback (implementation of [Content])
|
||||
/// * value providers (implementations of [EdnProvide])
|
||||
#[macro_export] macro_rules! edn_view {
|
||||
($Output:ty: |$self:ident: $App:ty| $content:expr; {
|
||||
$( $type:ty { $($sym:literal => $value:expr),* } );*
|
||||
|
|
@ -11,19 +15,18 @@ use EdnItem::*;
|
|||
}
|
||||
$(
|
||||
impl<'a> EdnProvide<'a, $type> for $App {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem) -> Option<$type> {
|
||||
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> 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) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
$(EdnItem::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
|
|
@ -33,7 +36,7 @@ use EdnItem::*;
|
|||
};
|
||||
($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) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
$(EdnItem::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
|
|
@ -45,7 +48,7 @@ use EdnItem::*;
|
|||
/// 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),
|
||||
Ok(T, EdnItem<Arc<str>>),
|
||||
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
|
||||
Err(String),
|
||||
_Unused(PhantomData<&'a E>),
|
||||
|
|
@ -76,8 +79,8 @@ impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
|
|||
Err(error) => Self::Err(format!("{error} in {source}"))
|
||||
}
|
||||
}
|
||||
pub fn from_items (state: T, items: Vec<EdnItem>) -> Self {
|
||||
Self::Ok(state, EdnItem::Exp(items))
|
||||
pub fn from_items (state: T, items: Vec<EdnItem<impl AsRef<str>>>) -> Self {
|
||||
Self::Ok(state, EdnItem::Exp(items).to_arc())
|
||||
}
|
||||
}
|
||||
impl<E: Output, T: for<'a>EdnViewData<'a, E> + Send + Sync + std::fmt::Debug> Content<E> for EdnView<'_, E, T> {
|
||||
|
|
@ -110,7 +113,7 @@ pub trait EdnViewData<'a, E: Output>:
|
|||
EdnProvide<'a, E::Unit> +
|
||||
EdnProvide<'a, Box<dyn Render<E> + 'a>>
|
||||
{
|
||||
fn get_bool (&'a self, item: &'a EdnItem) -> Option<bool> {
|
||||
fn get_bool (&'a self, item: &'a EdnItem<impl AsRef<str>>) -> Option<bool> {
|
||||
Some(match &item {
|
||||
Sym(s) => match s.as_ref() {
|
||||
":false" | ":f" => false,
|
||||
|
|
@ -122,13 +125,13 @@ pub trait EdnViewData<'a, E: Output>:
|
|||
_ => return EdnProvide::get(self, item)
|
||||
})
|
||||
}
|
||||
fn get_usize (&'a self, item: &'a EdnItem) -> Option<usize> {
|
||||
fn get_usize (&'a self, item: &'a EdnItem<impl AsRef<str>>) -> Option<usize> {
|
||||
Some(match &item { Num(n) => *n, _ => return EdnProvide::get(self, item) })
|
||||
}
|
||||
fn get_unit (&'a self, item: &'a EdnItem) -> Option<E::Unit> {
|
||||
fn get_unit (&'a self, item: &'a EdnItem<impl AsRef<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) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
fn get_content (&'a self, item: &'a EdnItem<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
Some(match item {
|
||||
Nil => Box::new(()),
|
||||
Exp(ref e) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue