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,22 +1,22 @@
use crate::*;
#[derive(Debug, Copy, Clone, Default)] pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<A>(Alignment, A);
try_from_atoms!(<'a, E>: Align<RenderBox<'a, E>>: |state, atoms| {
let head = atoms.next()?;
if head.kind() != TokenKind::Key { return None }
match head.text() {
"align/c" => return Some(Self::c(state.get_content(&atoms.next()?).expect("no content"))),
"align/x" => return Some(Self::x(state.get_content(&atoms.next()?).expect("no content"))),
"align/y" => return Some(Self::y(state.get_content(&atoms.next()?).expect("no content"))),
"align/n" => return Some(Self::n(state.get_content(&atoms.next()?).expect("no content"))),
"align/s" => return Some(Self::s(state.get_content(&atoms.next()?).expect("no content"))),
"align/e" => return Some(Self::e(state.get_content(&atoms.next()?).expect("no content"))),
"align/w" => return Some(Self::w(state.get_content(&atoms.next()?).expect("no content"))),
"align/nw" => return Some(Self::nw(state.get_content(&atoms.next()?).expect("no content"))),
"align/ne" => return Some(Self::ne(state.get_content(&atoms.next()?).expect("no content"))),
"align/sw" => return Some(Self::sw(state.get_content(&atoms.next()?).expect("no content"))),
"align/se" => return Some(Self::se(state.get_content(&atoms.next()?).expect("no content"))),
_ => {}
try_from_expr!(<'a, E>: Align<RenderBox<'a, E>>: |state, iter| {
if let Some((Token { value: Value::Key(key), .. }, _)) = iter.next() {
match key {
"align/c" => return Some(Self::c(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/x" => return Some(Self::x(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/y" => return Some(Self::y(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/n" => return Some(Self::n(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/s" => return Some(Self::s(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/e" => return Some(Self::e(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/w" => return Some(Self::w(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/nw" => return Some(Self::nw(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/ne" => return Some(Self::ne(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/sw" => return Some(Self::sw(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/se" => return Some(Self::se(state.get_content(&iter.next()?.0.value).expect("no content"))),
_ => {}
}
}
});
impl<A> Align<A> {

View file

@ -31,35 +31,35 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
}
}
}
try_from_atoms!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, atoms| {
let head = atoms.next()?;
if head.kind() != TokenKind::Key { return None }
match head.text() {
"bsp/n" => return Some(Self::n(
state.get_content(&atoms.next()?).expect("no south"),
state.get_content(&atoms.next()?).expect("no north")
)),
"bsp/s" => return Some(Self::s(
state.get_content(&atoms.next()?).expect("no north"),
state.get_content(&atoms.next()?).expect("no south")
)),
"bsp/e" => return Some(Self::e(
state.get_content(&atoms.next()?).expect("no west"),
state.get_content(&atoms.next()?).expect("no east")
)),
"bsp/w" => return Some(Self::w(
state.get_content(&atoms.next()?).expect("no east"),
state.get_content(&atoms.next()?).expect("no west")
)),
"bsp/a" => return Some(Self::a(
state.get_content(&atoms.next()?).expect("no above"),
state.get_content(&atoms.next()?).expect("no below")
)),
"bsp/b" => return Some(Self::b(
state.get_content(&atoms.next()?).expect("no above"),
state.get_content(&atoms.next()?).expect("no below")
)),
_ => {}
try_from_expr!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
if let Some((Token { value: Value::Key(key), .. }, _)) = iter.next() {
match key {
"bsp/n" => return Some(Self::n(
state.get_content(&iter.next()?.0.value).expect("no south"),
state.get_content(&iter.next()?.0.value).expect("no north")
)),
"bsp/s" => return Some(Self::s(
state.get_content(&iter.next()?.0.value).expect("no north"),
state.get_content(&iter.next()?.0.value).expect("no south")
)),
"bsp/e" => return Some(Self::e(
state.get_content(&iter.next()?.0.value).expect("no west"),
state.get_content(&iter.next()?.0.value).expect("no east")
)),
"bsp/w" => return Some(Self::w(
state.get_content(&iter.next()?.0.value).expect("no east"),
state.get_content(&iter.next()?.0.value).expect("no west")
)),
"bsp/a" => return Some(Self::a(
state.get_content(&iter.next()?.0.value).expect("no above"),
state.get_content(&iter.next()?.0.value).expect("no below")
)),
"bsp/b" => return Some(Self::b(
state.get_content(&iter.next()?.0.value).expect("no above"),
state.get_content(&iter.next()?.0.value).expect("no below")
)),
_ => {}
}
}
});
impl<A, B> Bsp<A, B> {

View file

@ -5,29 +5,29 @@ impl<A> When<A> { pub fn new (c: bool, a: A) -> Self { Self(c, a) } }
/// Show one item if a condition is true and another if the condition is false
pub struct Either<A, B>(pub bool, pub A, pub B);
impl<A, B> Either<A, B> { pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } }
try_from_atoms!(<'a, E>: When<RenderBox<'a, E>>: |state, atoms| {
let head = atoms.next()?;
if (head.kind(), head.text()) == (TokenKind::Key, "when") {
let condition = atoms.next();
if let Some(ref condition) = condition {
let condition = state.get_bool(condition).expect("no condition");
if let Some(ref content) = atoms.next() {
let content = state.get_content(content).expect("no atom");
try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| {
if let Some((Token { value: Value::Key("when"), .. }, _)) = iter.next() {
let iter = iter.clone();
let condition = iter.next();
if let Some((ref condition, _)) = condition {
let condition = state.get_bool(&condition.value).expect("no condition");
if let Some((ref content, _)) = iter.next() {
let content = state.get_content(&content.value).expect("no atom");
return Some(Self(condition, content))
}
}
}
});
try_from_atoms!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, atoms| {
let head = atoms.next()?;
if (head.kind(), head.text()) == (TokenKind::Key, "either") {
let condition = atoms.next();
if let Some(ref condition) = condition {
let condition = state.get_bool(condition).expect("no condition");
if let Some(ref content1) = atoms.next() {
let content1 = state.get_content(content1).expect("no content1");
if let Some(ref content2) = atoms.next() {
let content2 = state.get_content(content2).expect("no content2");
try_from_expr!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
if let Some((Token { value: Value::Key("either"), .. }, _)) = iter.next() {
let iter = iter.clone();
let condition = iter.next();
if let Some((ref condition, _)) = condition {
let condition = state.get_bool(&condition.value).expect("no condition");
if let Some((ref content1, _)) = iter.next() {
let content1 = state.get_content(&content1.value).expect("no content1");
if let Some((ref content2, _)) = iter.next() {
let content2 = state.get_content(&content2.value).expect("no content2");
return Some(Self(condition, content1, content2))
}
}

View file

@ -1,5 +1,4 @@
use crate::*;
use RefAtom::*;
pub fn map_south<O: Output>(
item_offset: O::Unit,
item_height: O::Unit,

View file

@ -1,5 +1,4 @@
use crate::*;
use RefAtom::*;
/// Defines an enum that transforms its content
/// along either the X axis, the Y axis, or both.
///
@ -13,21 +12,17 @@ macro_rules! transform_xy {
pub fn y (item: T) -> Self { Self::Y(item) }
pub fn xy (item: T) -> Self { Self::XY(item) }
}
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<RenderBox<'a, E>> {
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> {
let head = atoms.next()?;
if head.kind() != TokenKind::Key { return None }
Some(match head.text() {
$x => Self::x(
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
$y => Self::y(
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
$xy => Self::xy(
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
_ => return None
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<T>
for $Enum<RenderBox<'a, E>> {
fn try_from_expr (state: &T, iter: TokenIter) -> Option<Self> {
Some(if let Some((Token { value: Value::Key($x), .. }, _)) = iter.next() {
Self::x(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
} else if let Some((Token { value: Value::Key($y), .. }, _)) = iter.next() {
Self::y(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
} else if let Some((Token { value: Value::Key($xy), .. }, _)) = iter.next() {
Self::xy(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
} else {
return None
})
}
}
@ -56,25 +51,24 @@ macro_rules! transform_xy_unit {
pub fn y (y: U, item: T) -> Self { Self::Y(y, item) }
pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) }
}
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<E::Unit, RenderBox<'a, E>> {
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> {
let head = atoms.next()?;
if head.kind() != TokenKind::Key { return None }
Some(match head.text() {
$x => Self::x(
state.get_unit(&atoms.next().expect("no x")).expect("no x"),
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
$y => Self::y(
state.get_unit(&atoms.next().expect("no y")).expect("no y"),
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
$xy => Self::xy(
state.get_unit(&atoms.next().expect("no x")).expect("no x"),
state.get_unit(&atoms.next().expect("no y")).expect("no y"),
state.get_content(&atoms.next().expect("no content")).expect("no content"),
),
_ => return None
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<T>
for $Enum<E::Unit, RenderBox<'a, E>> {
fn try_from_expr (state: &T, iter: TokenIter) -> Option<Self> {
Some(if let Some((Token { value: Value::Key($x), .. }, _)) = iter.next() {
let x = state.get_unit(&iter.next().expect("no x").0.value).expect("no x");
let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
Self::x(x, c)
} else if let Some((Token { value: Value::Key($y), .. }, _)) = iter.next() {
let y = state.get_unit(&iter.next().expect("no y").0.value).expect("no y");
let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
Self::y(y, c)
} else if let Some((Token { value: Value::Key($xy), .. }, _)) = iter.next() {
let x = state.get_unit(&iter.next().expect("no x").0.value).expect("no x");
let y = state.get_unit(&iter.next().expect("no y").0.value).expect("no y");
let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
Self::xy(x, y, c)
} else {
return None
})
}
}

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
}
}