remove Atom. almost there

This commit is contained in:
🪞👃🪞 2025-01-18 15:37:53 +01:00
parent dc7b713108
commit cf1fd5b45a
20 changed files with 539 additions and 739 deletions

View file

@ -1,6 +1,6 @@
use crate::*;
use std::{sync::Arc, fmt::Debug};
use TokenKind::*;
use Value::*;
/// Define an EDN-backed view.
///
/// This consists of:
@ -14,8 +14,8 @@ use TokenKind::*;
fn content(&$self) -> impl Render<$Output> { $content }
}
$(
impl<'a> Context<'a, $type> for $App {
fn get (&'a $self, atom: &'a impl Atom) -> Option<$type> {
impl Context<$type> for $App {
fn get (&$self, atom: &Value) -> Option<$type> {
Some(match atom.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
}
}
@ -26,7 +26,7 @@ use TokenKind::*;
#[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, atom: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> {
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<E> + 'a>> {
Some(match atom.to_ref() {
$(Atom::Sym($pat) => $expr),*,
_ => return None
@ -36,7 +36,7 @@ use TokenKind::*;
};
($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, atom: &'a impl Atom) -> Option<Box<dyn Render<$Output> + 'a>> {
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<$Output> + 'a>> {
Some(match atom.to_ref() {
$(Atom::Sym($pat) => $expr),*,
_ => return None
@ -79,7 +79,7 @@ where S: Debug, A: Debug {
}
}
impl<'a, O, S, A> Content<O> for AtomView<'a, O, S, A>
where O: Output, S: ViewContext<'a, O>, A: Atom {
where O: Output, S: ViewContext<'a, O>, A: Send + Sync {
fn content (&self) -> impl Render<O> {
match self {
Self::Inert => { panic!("inert rendered") },
@ -103,49 +103,43 @@ pub trait ViewContext<'a, E: Output>: Sized + Send + Sync
+ Context<E::Unit>
+ Context<Box<dyn Render<E> + 'a>>
{
fn get_bool (&self, atom: &impl Atom) -> Option<bool> {
Some(match atom.kind() {
Num => match atom.num() { 0 => false, _ => true },
Sym => match atom.text() {
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_usize (&self, atom: &impl Atom) -> Option<usize> {
Some(match atom.kind() {
Num => atom.num(),
_ => return Context::get(self, atom)
})
fn get_usize (&self, atom: &Value) -> Option<usize> {
Some(match atom {Num(n) => *n, _ => return Context::get(self, atom)})
}
fn get_unit (&self, atom: &impl Atom) -> Option<E::Unit> {
Some(match atom.kind() {
Num => E::Unit::from(atom.num() as u16),
_ => 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: &impl Atom) -> 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>>);
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))
}
}
#[macro_export] macro_rules! try_delegate {
($s:ident, $atom:expr, $T:ty) => {
if let Some(value) = <$T>::try_from_atoms($s, $atom) {
if let Some(value) = <$T>::try_from_atom($s, $atom) {
return Some(value.boxed())
}
}
@ -157,17 +151,13 @@ pub type AtomCallback<'a, O, State> =
pub type AtomRenderCallback<'a, O, State> =
Box<AtomCallback<'a, O, State>>;
pub trait TryFromAtom<'a, T>: Sized {
fn try_from_atoms (state: &'a T, atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self>;
}
pub trait TryIntoAtom<'a, T>: Sized {
fn try_into_atoms (&self) -> Option<RefAtomsIterator<'a>>;
}
#[macro_export] macro_rules! try_from_atoms {
(<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident,$atoms:ident|$body:expr) => {
impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<$l, T> for $Struct {
fn try_from_atoms ($state: &$l T, mut $atoms: impl Iterator<Item = RefAtom<$l>> + $l) -> Option<Self> {
$body;
#[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> {
if let Value::Exp(0, $atoms) = atom {
$body;
}
None
}
}