implement TryFromEdn for Fill

This commit is contained in:
🪞👃🪞 2025-01-13 23:44:45 +01:00
parent 811e341cd5
commit 8eecd75592
3 changed files with 31 additions and 17 deletions

View file

@ -58,18 +58,12 @@ pub trait EdnViewData<'a, E: Output>:
return builtin.boxed() return builtin.boxed()
} }
if let Some(builtin) = Fill::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
return builtin.boxed()
}
panic!("{item:?}"); panic!("{item:?}");
match (head, tail) { 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/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/y"), [y, a]) => Fixed::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),

View file

@ -65,7 +65,7 @@ impl<E: Output> Measure<E> {
y: Arc::new(0.into()), y: Arc::new(0.into()),
} }
} }
pub fn of <T: Content<E>> (&self, item: T) -> Bsp<E, Fill<&Self>, T> { pub fn of <T: Content<E>> (&self, item: T) -> Bsp<E, Fill<E, &Self>, T> {
Bsp::b(Fill::xy(self), item) Bsp::b(Fill::xy(self), item)
} }
} }

View file

@ -6,19 +6,20 @@ use crate::*;
/// The `_Unused` variant wraps the `Output` type /// The `_Unused` variant wraps the `Output` type
/// using `PhantomData` to permit the double generic. /// using `PhantomData` to permit the double generic.
macro_rules! transform_xy { macro_rules! transform_xy {
($self:ident : $Enum:ident |$to:ident|$area:expr) => { ($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$area:expr) => {
pub enum $Enum<T> { X(T), Y(T), XY(T) } pub enum $Enum<E, T> { _Unused(PhantomData<E>), X(T), Y(T), XY(T) }
impl<T> $Enum<T> { impl<E, T> $Enum<E, T> {
pub fn x (item: T) -> Self { Self::X(item) } pub fn x (item: T) -> Self { Self::X(item) }
pub fn y (item: T) -> Self { Self::Y(item) } pub fn y (item: T) -> Self { Self::Y(item) }
pub fn xy (item: T) -> Self { Self::XY(item) } pub fn xy (item: T) -> Self { Self::XY(item) }
} }
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> { impl<E: Output, T: Content<E>> Content<E> for $Enum<E, T> {
fn content (&self) -> impl Render<E> { fn content (&self) -> impl Render<E> {
match self { match self {
Self::X(item) => item, Self::X(item) => item,
Self::Y(item) => item, Self::Y(item) => item,
Self::XY(item) => item Self::XY(item) => item,
_ => unreachable!(),
} }
} }
fn layout (&$self, $to: <E as Output>::Area) -> <E as Output>::Area { fn layout (&$self, $to: <E as Output>::Area) -> <E as Output>::Area {
@ -26,15 +27,34 @@ macro_rules! transform_xy {
$area $area
} }
} }
impl<'a, T, E, A> TryFromEdn<'a, T> for $Enum<E, A>
where
T: EdnProvide<'a, bool> + EdnProvide<'a, A> + 'a,
E: Output,
A: Render<E> + 'a
{
fn try_from_edn (
state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]
) -> Option<Self> {
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 [x0, y0, wmax, hmax] = to.xywh();
let [x, y, w, h] = self.content().layout(to).xywh(); let [x, y, w, h] = self.content().layout(to).xywh();
match self { match self {
X(_) => [x0, y, wmax, h], X(_) => [x0, y, wmax, h],
Y(_) => [x, y0, w, hmax], Y(_) => [x, y0, w, hmax],
XY(_) => [x0, y0, wmax, hmax], XY(_) => [x0, y0, wmax, hmax],
_ => unreachable!()
}.into() }.into()
}); });