wip: distribute layout operator parsing

This commit is contained in:
🪞👃🪞 2025-01-13 23:21:56 +01:00
parent 4af6e011b6
commit fa70a42bad
12 changed files with 150 additions and 81 deletions

View file

@ -37,52 +37,60 @@ pub trait EdnViewData<'a, E: Output>:
match item {
Nil => Box::new(()),
Exp(e) => if let [head, tail @ ..] = e.as_slice() {
if let Some(builtin) = When::<_, Box<dyn Render<E>>>::try_from_edn(self, head, tail) {
return builtin.boxed()
}
if let Some(builtin) = Either::<_, Box<dyn Render<E>>, Box<dyn Render<E>>>::try_from_edn(self, head, tail) {
return builtin.boxed()
}
panic!("{item:?}");
match (head, tail) {
(Key("when"), [c, a]) =>
When(self.arg(c).unwrap(), self.item(a)).boxed(),
(Key("either"), [c, a, b]) =>
Either(self.arg(c).unwrap(), self.item(a), self.item(b)).boxed(),
//(Key("when"), [c, a]) =>
//When(self.arg(c).unwrap(), self.get_content(a)).boxed(),
//(Key("either"), [c, a, b]) =>
//Either(self.arg(c).unwrap(), self.get_content(a), self.get_content(b)).boxed(),
(Key("align/c"), [a]) => Align::c(self.item(a)).boxed(),
(Key("align/x"), [a]) => Align::x(self.item(a)).boxed(),
(Key("align/y"), [a]) => Align::y(self.item(a)).boxed(),
(Key("align/n"), [a]) => Align::n(self.item(a)).boxed(),
(Key("align/s"), [a]) => Align::s(self.item(a)).boxed(),
(Key("align/e"), [a]) => Align::e(self.item(a)).boxed(),
(Key("align/w"), [a]) => Align::w(self.item(a)).boxed(),
(Key("align/nw"), [a]) => Align::nw(self.item(a)).boxed(),
(Key("align/ne"), [a]) => Align::ne(self.item(a)).boxed(),
(Key("align/sw"), [a]) => Align::sw(self.item(a)).boxed(),
(Key("align/se"), [a]) => Align::se(self.item(a)).boxed(),
(Key("align/c"), [a]) => Align::c(self.get_content(a)).boxed(),
(Key("align/x"), [a]) => Align::x(self.get_content(a)).boxed(),
(Key("align/y"), [a]) => Align::y(self.get_content(a)).boxed(),
(Key("align/n"), [a]) => Align::n(self.get_content(a)).boxed(),
(Key("align/s"), [a]) => Align::s(self.get_content(a)).boxed(),
(Key("align/e"), [a]) => Align::e(self.get_content(a)).boxed(),
(Key("align/w"), [a]) => Align::w(self.get_content(a)).boxed(),
(Key("align/nw"), [a]) => Align::nw(self.get_content(a)).boxed(),
(Key("align/ne"), [a]) => Align::ne(self.get_content(a)).boxed(),
(Key("align/sw"), [a]) => Align::sw(self.get_content(a)).boxed(),
(Key("align/se"), [a]) => Align::se(self.get_content(a)).boxed(),
(Key("bsp/a"), [a, b]) => Bsp::a(self.item(a), self.item(b),).boxed(),
(Key("bsp/b"), [a, b]) => Bsp::b(self.item(a), self.item(b),).boxed(),
(Key("bsp/e"), [a, b]) => Bsp::e(self.item(a), self.item(b),).boxed(),
(Key("bsp/n"), [a, b]) => Bsp::n(self.item(a), self.item(b),).boxed(),
(Key("bsp/s"), [a, b]) => Bsp::s(self.item(a), self.item(b),).boxed(),
(Key("bsp/w"), [a, b]) => Bsp::w(self.item(a), self.item(b),).boxed(),
(Key("bsp/a"), [a, b]) => Bsp::a(self.get_content(a), self.get_content(b),).boxed(),
(Key("bsp/b"), [a, b]) => Bsp::b(self.get_content(a), self.get_content(b),).boxed(),
(Key("bsp/e"), [a, b]) => Bsp::e(self.get_content(a), self.get_content(b),).boxed(),
(Key("bsp/n"), [a, b]) => Bsp::n(self.get_content(a), self.get_content(b),).boxed(),
(Key("bsp/s"), [a, b]) => Bsp::s(self.get_content(a), self.get_content(b),).boxed(),
(Key("bsp/w"), [a, b]) => Bsp::w(self.get_content(a), self.get_content(b),).boxed(),
(Key("fill/x"), [a]) => Fill::x(self.item(a)).boxed(),
(Key("fill/y"), [a]) => Fill::y(self.item(a)).boxed(),
(Key("fill/xy"), [a]) => Fill::xy(self.item(a)).boxed(),
(Key("fill/x"), [a]) => Fill::x(self.get_content(a)).boxed(),
(Key("fill/y"), [a]) => Fill::y(self.get_content(a)).boxed(),
(Key("fill/xy"), [a]) => Fill::xy(self.get_content(a)).boxed(),
(Key("fixed/x"), [x, a]) => Fixed::x(self.arg(x).unwrap(), self.item(a)).boxed(),
(Key("fixed/y"), [y, a]) => Fixed::y(self.arg(y).unwrap(), self.item(a)).boxed(),
(Key("fixed/xy"), [x, y, a]) => Fixed::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.item(a)).boxed(),
(Key("fixed/x"), [x, a]) => Fixed::x(self.arg(x).unwrap(), self.get_content(a)).boxed(),
(Key("fixed/y"), [y, a]) => Fixed::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),
(Key("fixed/xy"), [x, y, a]) => Fixed::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.get_content(a)).boxed(),
(Key("max/x"), [x, a]) => Max::x(self.arg(x).unwrap(), self.item(a)).boxed(),
(Key("max/y"), [y, a]) => Max::y(self.arg(y).unwrap(), self.item(a)).boxed(),
(Key("max/x"), [x, a]) => Max::x(self.arg(x).unwrap(), self.get_content(a)).boxed(),
(Key("max/y"), [y, a]) => Max::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),
(Key("push/x"), [x, a]) => Push::x(self.arg(x).unwrap(), self.item(a)).boxed(),
(Key("push/y"), [y, a]) => Push::y(self.arg(y).unwrap(), self.item(a)).boxed(),
(Key("push/x"), [x, a]) => Push::x(self.arg(x).unwrap(), self.get_content(a)).boxed(),
(Key("push/y"), [y, a]) => Push::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),
(Key("pad/x"), [x, a]) => Padding::x(self.arg(x).unwrap(), self.item(a)).boxed(),
(Key("pad/y"), [y, a]) => Padding::y(self.arg(y).unwrap(), self.item(a)).boxed(),
(Key("pad/xy"), [x, y, a]) => Padding::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.item(a)).boxed(),
(Key("pad/x"), [x, a]) => Padding::x(self.arg(x).unwrap(), self.get_content(a)).boxed(),
(Key("pad/y"), [y, a]) => Padding::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),
(Key("pad/xy"), [x, y, a]) => Padding::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.get_content(a)).boxed(),
(Key("grow/x"), [x, a]) => Margin::x(self.arg(x).unwrap(), self.item(a)).boxed(),
(Key("grow/y"), [y, a]) => Margin::y(self.arg(y).unwrap(), self.item(a)).boxed(),
(Key("grow/xy"), [x, y, a]) => Margin::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.item(a)).boxed(),
(Key("grow/x"), [x, a]) => Margin::x(self.arg(x).unwrap(), self.get_content(a)).boxed(),
(Key("grow/y"), [y, a]) => Margin::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),
(Key("grow/xy"), [x, y, a]) => Margin::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.get_content(a)).boxed(),
_ => todo!("{:?} {:?}", &head, &tail)
}
@ -103,7 +111,7 @@ impl<'a, E: Output, T> EdnViewData<'a, E> for T where T:
/// Renders from EDN source and context.
#[derive(Default)]
pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E>> {
pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> {
#[default]
Inert,
_Unused(PhantomData<&'a E>),
@ -111,12 +119,21 @@ pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E>> {
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
Err(String)
}
impl<'a, E: Output, T: EdnViewData<'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(_) => write!(f, "EdnView::Inert"),
Self::Ok(state, view) => write!(f, "EdnView::Ok(state={state:?} view={view:?}"),
Self::Err(error) => write!(f, "EdnView::Err({error})"),
}
}
}
impl<'a, E: Output, T: EdnViewData<'a, E>> EdnView<'a, E, T> {
impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
pub fn from_source (state: T, source: &'a str) -> Self {
match EdnItem::read_one(&source) {
Ok((layout, _)) => Self::Ok(state, layout),
Err(error) => Self::Err(format!("{error}"))
Err(error) => Self::Err(format!("{error} in {source}"))
}
}
pub fn from_items (state: T, items: Vec<EdnItem<&'a str>>) -> Self {
@ -124,14 +141,13 @@ impl<'a, E: Output, T: EdnViewData<'a, E>> EdnView<'a, E, T> {
}
}
impl<E: Output, T: for<'a>EdnViewData<'a, E> + Send + Sync> Content<E> for EdnView<'_, E, T> {
impl<E: Output, T: for<'a>EdnViewData<'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)
},
Self::Ok(state, layout) => state.get_content(layout),
Self::Err(_error) => {
Box::new(())//&format!("EdnView error: {error:?}")) // FIXME: String is not Render
panic!("{self:?}");
//TODO:&format!("EdnView error: {error:?}")) // FIXME: String is not Render
},
_ => todo!()
}

View file

@ -1,15 +1,38 @@
use crate::*;
/// 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<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
pub struct Either<E, A, B>(pub PhantomData<E>, pub bool, pub A, pub B);
impl<E, A, B> Either<E, A, B> {
pub fn new (c: bool, a: A, b: B) -> Self {
Self(Default::default(), c, a, b)
}
}
impl<'a, T, E, A, B> TryFromEdn<'a, T> for Either<E, A, B>
where
T: EdnProvide<'a, bool> + EdnProvide<'a, B> + EdnProvide<'a, A> + 'a,
E: Output,
A: Render<E> + 'a,
B: Render<E> + 'a,
{
fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option<Self> {
use EdnItem::*;
if let (Key("either"), [condition, content, alternative]) = (head, tail) {
Some(Self::new(
state.get(condition).expect("either: no condition"),
state.get(content).expect("either: no content"),
state.get(alternative).expect("either: no alternative")
))
} else {
None
}
}
}
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<E, A, B> {
fn layout (&self, to: E::Area) -> E::Area {
let Self(cond, a, b) = self;
let Self(_, cond, a, b) = self;
if *cond { a.layout(to) } else { b.layout(to) }
}
fn render (&self, to: &mut E) {
let Self(cond, a, b) = self;
let Self(_, cond, a, b) = self;
if *cond { a.render(to) } else { b.render(to) }
}
}

View file

@ -1,11 +1,35 @@
use crate::*;
/// Show an item only when a condition is true.
pub struct When<A>(pub bool, pub A);
impl<E: Output, A: Render<E>> Content<E> for When<A> {
pub struct When<E, A>(pub PhantomData<E>, pub bool, pub A);
impl<E, A> When<E, A> {
pub fn new (c: bool, a: A) -> Self {
Self(Default::default(), c, a)
}
}
impl<'a, T, E, A> TryFromEdn<'a, T> for When<E, A>
where
T: EdnProvide<'a, bool> + EdnProvide<'a, A> + 'a,
E: Output,
A: Render<E> + 'a
{
fn try_from_edn (
state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]
) -> Option<Self> {
use EdnItem::*;
if let (Key("when"), [condition, content]) = (head, tail) {
Some(Self(
Default::default(),
state.get(condition).expect("when: no condition"),
state.get(content).expect("when: no content")
))
} else {
None
}
}
}
impl<E: Output, A: Render<E>> Content<E> for When<E, A> {
fn layout (&self, to: E::Area) -> E::Area {
let Self(cond, item) = self;
let Self(_, cond, item) = self;
let mut area = E::Area::zero();
if *cond {
let item_area = item.layout(to);
@ -17,7 +41,7 @@ impl<E: Output, A: Render<E>> Content<E> for When<A> {
area.into()
}
fn render (&self, to: &mut E) {
let Self(cond, item) = self;
let Self(_, cond, item) = self;
if *cond { item.render(to) }
}
}