use crate::*; use std::marker::PhantomData; use EdnItem::*; pub type EdnCallback<'a, O: Output, State> = dyn Fn(&'a State)-> RenderBox<'a, O> + Send + Sync + 'a; pub type EdnRenderCallback<'a, O: Output, State> = Box>; /// Provides values to the template pub trait EdnViewData { fn get_bool (&self, _sym: EdnItem<&str>) -> bool { false } fn get_unit (&self, _sym: EdnItem<&str>) -> E::Unit { 0.into() } fn get_usize (&self, _sym: EdnItem<&str>) -> usize { 0 } fn get_content <'a> (&'a self, _sym: EdnItem<&'a str>) -> RenderBox<'a, E> { Box::new(()) } } /// Renders from EDN source and context. pub enum EdnView> { _Unused(PhantomData), Ok(T, EdnItem), //render: BoxBox + Send + Sync + 'a> + Send + Sync + 'a> Err(String) } impl> EdnView { pub fn from_source (state: T, source: &str) -> Self { match EdnItem::read_one(&source) { Ok((layout, _)) => Self::Ok(state, layout), Err(error) => Self::Err(format!("{error}")) } } pub fn from_items (state: T, items: &[EdnItem<&str>]) -> Self { Self::Ok(state, EdnItem::Exp(items.iter().map(|i|(*i).clone()).collect())) } } impl + Send + Sync> Content for EdnView { fn content (&self) -> impl Render { use EdnItem::*; match self { Self::Ok(s, layout) => match layout { Nil => ().boxed(), Sym(t) => s.get_content(Sym(t.as_str())).boxed(), Key(t) => panic!("todo: add error handling to content() chain. unexpected key {t}"), Num(n) => panic!("todo: add error handling to content() chain. unexpected num {n}"), Exp(e) => if let [head, tail @ ..] = e.as_slice() { match_exp(s, &head.to_ref(), &tail) } else { panic!("todo: add error handling to content() chain. invalid expression {e:?}"); }, }, Self::Err(_error) => { Box::new(())//&format!("EdnView error: {error:?}")) // FIXME: String is not Render }, _ => todo!() } } } fn match_exp <'a, E: Output + 'a, State: EdnViewData> ( s: &'a State, head: &EdnItem<&str>, tail: &'a [EdnItem] ) -> Box + 'a> { match (head, tail) { (Key("align/c"), [a]) => Align::c(s.get_content(a.to_ref())).boxed(), (Key("align/x"), [a]) => Align::x(s.get_content(a.to_ref())).boxed(), (Key("align/y"), [a]) => Align::y(s.get_content(a.to_ref())).boxed(), (Key("align/n"), [a]) => Align::n(s.get_content(a.to_ref())).boxed(), (Key("align/s"), [a]) => Align::s(s.get_content(a.to_ref())).boxed(), (Key("align/e"), [a]) => Align::e(s.get_content(a.to_ref())).boxed(), (Key("align/w"), [a]) => Align::w(s.get_content(a.to_ref())).boxed(), (Key("align/nw"), [a]) => Align::nw(s.get_content(a.to_ref())).boxed(), (Key("align/ne"), [a]) => Align::ne(s.get_content(a.to_ref())).boxed(), (Key("align/sw"), [a]) => Align::sw(s.get_content(a.to_ref())).boxed(), (Key("align/se"), [a]) => Align::se(s.get_content(a.to_ref())).boxed(), (Key("bsp/a"), [a, b]) => Bsp::a(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(), (Key("bsp/b"), [a, b]) => Bsp::b(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(), (Key("bsp/e"), [a, b]) => Bsp::e(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(), (Key("bsp/n"), [a, b]) => Bsp::n(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(), (Key("bsp/s"), [a, b]) => Bsp::s(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(), (Key("bsp/w"), [a, b]) => Bsp::w(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(), (Key("fill/x"), [a]) => Fill::x(s.get_content(a.to_ref())).boxed(), (Key("fill/y"), [a]) => Fill::y(s.get_content(a.to_ref())).boxed(), (Key("fill/xy"), [a]) => Fill::xy(s.get_content(a.to_ref())).boxed(), (Key("fixed/x"), [x, a]) => Fixed::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(), (Key("fixed/y"), [y, a]) => Fixed::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(), (Key("fixed/xy"), [x, y, a]) => Fixed::xy(s.get_unit(x.to_ref()), s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(), (Key("max/x"), [x, a]) => Max::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(), (Key("max/y"), [y, a]) => Max::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(), (Key("push/x"), [x, a]) => Push::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(), (Key("push/y"), [y, a]) => Push::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(), (Key("when"), [c, a]) => When(s.get_bool(c.to_ref()), s.get_content(a.to_ref())).boxed(), _ => todo!("{:?} {:?}", &head, &tail) } }