From 3975837393d005d8db8880198ac5c26e6f17be84 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Tue, 7 Jan 2025 21:48:58 +0100 Subject: [PATCH] extract match_exp --- edn/src/edn_item.rs | 2 +- output/src/edn_view.rs | 89 ++++++++++++++++++---------------- src/groovebox/groovebox_tui.rs | 13 ++--- 3 files changed, 56 insertions(+), 48 deletions(-) diff --git a/edn/src/edn_item.rs b/edn/src/edn_item.rs index 293e7346..d5d900d7 100644 --- a/edn/src/edn_item.rs +++ b/edn/src/edn_item.rs @@ -50,7 +50,7 @@ impl EdnItem<&str> { } } } -impl + PartialEq + Default + Clone + std::fmt::Debug> EdnItem { +impl> EdnItem { pub fn to_ref (&self) -> EdnItem<&str> { match self { Self::Nil => EdnItem::Nil, diff --git a/output/src/edn_view.rs b/output/src/edn_view.rs index cb5aff5e..29052e46 100644 --- a/output/src/edn_view.rs +++ b/output/src/edn_view.rs @@ -46,48 +46,10 @@ impl + Send + Sync> Content for EdnView { 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 + Send + Sync> Content for EdnView { } } +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) + } +} diff --git a/src/groovebox/groovebox_tui.rs b/src/groovebox/groovebox_tui.rs index a5a508fb..296194bc 100644 --- a/src/groovebox/groovebox_tui.rs +++ b/src/groovebox/groovebox_tui.rs @@ -5,12 +5,6 @@ use EdnItem::*; const EDN: &'static str = include_str!("groovebox.edn"); -//impl Content for Groovebox { - //fn content (&self) -> impl Render { - //self.size.of(EdnView::from_source(self, EDN)) - //} -//} - // this works: render!(TuiOut: (self: Groovebox) => self.size.of( Bsp::s(self.toolbar_view(), @@ -19,6 +13,13 @@ render!(TuiOut: (self: Groovebox) => self.size.of( Bsp::n(self.status_view(), Bsp::w(self.pool_view(), Fill::xy(Bsp::e(self.sampler_view(), &self.editor))))))))); +// this almost works: +//impl Content for Groovebox { + //fn content (&self) -> impl Render { + //self.size.of(EdnView::from_source(self, EDN)) + //} +//} + impl Groovebox { fn toolbar_view (&self) -> impl Content + use<'_> { Fill::x(Fixed::y(2, lay!(