From 8eecd755923ea126516397be301231ccfc5aeb56 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Mon, 13 Jan 2025 23:44:45 +0100 Subject: [PATCH] implement TryFromEdn for Fill --- output/src/edn_view.rs | 14 ++++---------- output/src/measure.rs | 2 +- output/src/transform_xy.rs | 32 ++++++++++++++++++++++++++------ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/output/src/edn_view.rs b/output/src/edn_view.rs index 8a4844c3..7098e71a 100644 --- a/output/src/edn_view.rs +++ b/output/src/edn_view.rs @@ -58,18 +58,12 @@ pub trait EdnViewData<'a, E: Output>: return builtin.boxed() } + if let Some(builtin) = Fill::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) { + return builtin.boxed() + } + panic!("{item:?}"); match (head, tail) { - (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.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.get_content(a)).boxed(), (Key("fixed/y"), [y, a]) => Fixed::y(self.arg(y).unwrap(), self.get_content(a)).boxed(), diff --git a/output/src/measure.rs b/output/src/measure.rs index 76b6d0ed..7542cc75 100644 --- a/output/src/measure.rs +++ b/output/src/measure.rs @@ -65,7 +65,7 @@ impl Measure { y: Arc::new(0.into()), } } - pub fn of > (&self, item: T) -> Bsp, T> { + pub fn of > (&self, item: T) -> Bsp, T> { Bsp::b(Fill::xy(self), item) } } diff --git a/output/src/transform_xy.rs b/output/src/transform_xy.rs index d6863218..2e448ad8 100644 --- a/output/src/transform_xy.rs +++ b/output/src/transform_xy.rs @@ -6,19 +6,20 @@ use crate::*; /// The `_Unused` variant wraps the `Output` type /// using `PhantomData` to permit the double generic. macro_rules! transform_xy { - ($self:ident : $Enum:ident |$to:ident|$area:expr) => { - pub enum $Enum { X(T), Y(T), XY(T) } - impl $Enum { + ($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$area:expr) => { + pub enum $Enum { _Unused(PhantomData), X(T), Y(T), XY(T) } + impl $Enum { pub fn x (item: T) -> Self { Self::X(item) } pub fn y (item: T) -> Self { Self::Y(item) } pub fn xy (item: T) -> Self { Self::XY(item) } } - impl> Content for $Enum { + impl> Content for $Enum { fn content (&self) -> impl Render { match self { Self::X(item) => item, Self::Y(item) => item, - Self::XY(item) => item + Self::XY(item) => item, + _ => unreachable!(), } } fn layout (&$self, $to: ::Area) -> ::Area { @@ -26,15 +27,34 @@ macro_rules! transform_xy { $area } } + impl<'a, T, E, A> TryFromEdn<'a, T> for $Enum + where + T: EdnProvide<'a, bool> + EdnProvide<'a, A> + 'a, + E: Output, + A: Render + 'a + { + fn try_from_edn ( + state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>] + ) -> Option { + use EdnItem::*; + Some(match (head, tail) { + (Key($x), [a]) => Self::x(state.get(a).expect("no content")), + (Key($y), [a]) => Self::y(state.get(a).expect("no content")), + (Key($xy), [a]) => Self::xy(state.get(a).expect("no content")), + _ => return None + }) + } + } } } -transform_xy!(self: Fill |to|{ +transform_xy!("fill/x" "fill/y" "fill/xy" |self: Fill, to|{ let [x0, y0, wmax, hmax] = to.xywh(); let [x, y, w, h] = self.content().layout(to).xywh(); match self { X(_) => [x0, y, wmax, h], Y(_) => [x, y0, w, hmax], XY(_) => [x0, y0, wmax, hmax], + _ => unreachable!() }.into() });