mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
well, it compiles. fails on run, though
This commit is contained in:
parent
a362028ae7
commit
d14d67172c
6 changed files with 96 additions and 185 deletions
|
|
@ -1,28 +1,25 @@
|
|||
use crate::*;
|
||||
use std::{sync::Arc, fmt::Debug};
|
||||
use Value::*;
|
||||
/// 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 }
|
||||
($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 Context<$type> for $App {
|
||||
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
|
||||
impl<'a> ViewContext<'a, $Output> for $State {
|
||||
fn get_content_custom (&'a $self, value: &Value<'a>) -> Option<RenderBox<'a, $Output>> {
|
||||
if let Value::Sym(s) = value {
|
||||
match *s { $($sym => Some($body),)* _ => None }
|
||||
} else {
|
||||
panic!("expected symbol")
|
||||
}
|
||||
}
|
||||
)*)?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An ephemeral wrapper around view state and view description,
|
||||
// that is meant to be constructed and returned from [Content::content].
|
||||
pub struct View<'a, T>(pub &'a T, pub TokenIter<'a>);
|
||||
impl<'a, O: Output + 'a, T: ViewContext<'a, O>> Content<O> for View<'a, T> {
|
||||
fn content (&self) -> impl Render<O> {
|
||||
|
|
@ -35,97 +32,36 @@ impl<'a, O: Output + 'a, T: ViewContext<'a, O>> Content<O> for View<'a, T> {
|
|||
return None
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders from EDN source and context.
|
||||
///
|
||||
/// Generic over:
|
||||
///
|
||||
/// * `O` - Output target.
|
||||
/// * `S` - State provider.
|
||||
/// * `A` - Atom storage type.
|
||||
#[derive(Default, Clone)] pub enum AtomView<'a, O, S, A> {
|
||||
_Unused(std::marker::PhantomData<&'a O>),
|
||||
#[default] Inert,
|
||||
Src(S, &'a str),
|
||||
Ref(S, &'a A),
|
||||
Own(S, A),
|
||||
Err(Arc<str>)
|
||||
}
|
||||
impl<'a, O, S, A> Debug for AtomView<'a, O, S, A>
|
||||
where S: Debug, A: Debug {
|
||||
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
match self {
|
||||
Self::Inert =>
|
||||
write!(f, "AtomView::Inert"),
|
||||
Self::Src(state, view) =>
|
||||
write!(f, "AtomView::Src(state={state:?} view={view:?}"),
|
||||
Self::Ref(state, view) =>
|
||||
write!(f, "AtomView::Ref(state={state:?} view={view:?}"),
|
||||
Self::Own(state, view) =>
|
||||
write!(f, "AtomView::Arc(state={state:?} view={view:?}"),
|
||||
Self::Err(error) =>
|
||||
write!(f, "AtomView::Err({error})"),
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, O, S, A> Content<O> for AtomView<'a, O, S, A>
|
||||
where O: Output, S: ViewContext<'a, O>, A: Send + Sync {
|
||||
fn content (&self) -> impl Render<O> {
|
||||
match self {
|
||||
Self::Inert => { panic!("inert rendered") },
|
||||
Self::Err(e) => { panic!("render error: {e}") },
|
||||
Self::Src(state, source) => {},
|
||||
Self::Ref(state, atom) => {},
|
||||
Self::Own(state, atom) => {},
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output, T> ViewContext<'a, E> for T where T: Sized + Send + Sync
|
||||
// Provides components to the view.
|
||||
pub trait ViewContext<'a, E: Output + 'a>: Send + Sync
|
||||
+ Context<bool>
|
||||
+ Context<usize>
|
||||
+ Context<E::Unit>
|
||||
+ Context<Box<dyn Render<E> + 'a>> {}
|
||||
/// Provides values to the template
|
||||
pub trait ViewContext<'a, E: Output>: Sized + Send + Sync
|
||||
+ Context<bool>
|
||||
+ Context<usize>
|
||||
+ Context<E::Unit>
|
||||
+ Context<Box<dyn Render<E> + 'a>>
|
||||
{
|
||||
fn get_bool (&self, atom: &Value) -> Option<bool> {
|
||||
Some(match atom {
|
||||
Num(n) => match *n { 0 => false, _ => true },
|
||||
Sym(x) => match *x {
|
||||
":false" | ":f" => false, ":true" | ":t" => true,
|
||||
_ => return Context::get(self, atom)
|
||||
},
|
||||
_ => return Context::get(self, atom)
|
||||
})
|
||||
fn get_content (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>> {
|
||||
if let Some(builtin) = self.get_content_builtin(value) {
|
||||
Some(builtin)
|
||||
} else {
|
||||
self.get_content_custom(value)
|
||||
}
|
||||
}
|
||||
fn get_usize (&self, atom: &Value) -> Option<usize> {
|
||||
Some(match atom {Num(n) => *n, _ => return Context::get(self, atom)})
|
||||
}
|
||||
fn get_unit (&self, atom: &Value) -> Option<E::Unit> {
|
||||
Some(match atom {Num(n) => E::Unit::from(*n as u16), _ => return Context::get(self, atom)})
|
||||
}
|
||||
fn get_content (&self, atom: &Value) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
try_delegate!(self, *atom, When::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Either::<RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Align::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Bsp::<RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Fill::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Fixed::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Min::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Max::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Shrink::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Expand::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Push::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Pull::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Margin::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Padding::<_, RenderBox<'a, E>>);
|
||||
Some(Context::get_or_fail(self, atom))
|
||||
fn get_content_custom (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>>;
|
||||
fn get_content_builtin (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>> {
|
||||
try_delegate!(self, *value, When::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Either::<RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Align::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Bsp::<RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Fill::<RenderBox<'a, E>>);
|
||||
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
|
||||
}
|
||||
}
|
||||
#[macro_export] macro_rules! try_delegate {
|
||||
|
|
@ -135,17 +71,10 @@ pub trait ViewContext<'a, E: Output>: Sized + Send + Sync
|
|||
}
|
||||
}
|
||||
}
|
||||
// A function that returns a `RenderBox.
|
||||
pub type AtomCallback<'a, O, State> =
|
||||
dyn Fn(&'a State)-> RenderBox<'a, O> + Send + Sync + 'a;
|
||||
// A box containing a function that returns a `RenderBox.
|
||||
pub type AtomRenderCallback<'a, O, State> =
|
||||
Box<AtomCallback<'a, O, State>>;
|
||||
|
||||
#[macro_export] macro_rules! try_from_expr {
|
||||
(<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident, $atoms:ident|$body:expr) => {
|
||||
impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<T> for $Struct {
|
||||
fn try_from_atom ($state: &T, atom: Value) -> Option<Self> {
|
||||
impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<$l, T> for $Struct {
|
||||
fn try_from_atom ($state: &$l T, atom: Value<$l>) -> Option<Self> {
|
||||
if let Value::Exp(0, $atoms) = atom {
|
||||
$body;
|
||||
}
|
||||
|
|
@ -154,28 +83,3 @@ pub type AtomRenderCallback<'a, O, State> =
|
|||
}
|
||||
}
|
||||
}
|
||||
/// Implement `Context` for a context and content type.
|
||||
///
|
||||
/// This enables support for layout expressions.
|
||||
#[macro_export] macro_rules! provide_content {
|
||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<E: Output> Context<Box<dyn Render<E> + '_>> for $State {
|
||||
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<E> + '_>> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl Context<Box<dyn Render<$Output> + '_>> for $State {
|
||||
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<$Output> + '_>> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue