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!()
}