extract match_exp

This commit is contained in:
🪞👃🪞 2025-01-07 21:48:58 +01:00
parent 9a70fbc416
commit 3975837393
3 changed files with 56 additions and 48 deletions

View file

@ -46,48 +46,10 @@ impl<E: Output, T: EdnViewData<E> + Send + Sync> Content<E> for EdnView<E, T> {
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() {
let head = &head.to_ref();
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)
}
match_exp(s, &head.to_ref(), &tail)
} else {
panic!("todo: add error handling to content() chain. invalid expression {e:?}")
panic!("todo: add error handling to content() chain. invalid expression {e:?}");
().boxed()
},
},
Self::Err(error) => {
@ -99,3 +61,48 @@ impl<E: Output, T: EdnViewData<E> + Send + Sync> Content<E> for EdnView<E, T> {
}
}
fn match_exp <'a, E: Output + 'a, State: EdnViewData<E>> (
s: &'a State,
head: &EdnItem<&str>,
tail: &'a [EdnItem<String>]
) -> Box<dyn Render<E> + '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)
}
}