implement TokensIterator::peek

This commit is contained in:
🪞👃🪞 2025-01-18 03:47:18 +01:00
parent a949117017
commit 92fcb0af8f
7 changed files with 196 additions and 198 deletions

View file

@ -13,6 +13,24 @@ macro_rules! transform_xy {
pub fn y (item: T) -> Self { Self::Y(item) }
pub fn xy (item: T) -> Self { Self::XY(item) }
}
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<RenderBox<'a, E>> {
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> {
let head = atoms.next()?;
if head.kind() != TokenKind::Key { return None }
Some(match head.text() {
$x => Self::x(
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
$y => Self::y(
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
$xy => Self::xy(
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
_ => return None
})
}
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
fn content (&self) -> impl Render<E> {
match self {
@ -26,24 +44,6 @@ macro_rules! transform_xy {
$area
}
}
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<RenderBox<'a, E>> {
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> {
let head = atoms.next()?;
if head.kind() != TokenKind::Key { return None }
Some(match head.text() {
$x => Self::x(
state.get_content(atoms.next().expect("no content")).expect("no content")
),
$y => Self::y(
state.get_content(atoms.next().expect("no content")).expect("no content")
),
$xy => Self::xy(
state.get_content(atoms.next().expect("no content")).expect("no content")
),
_ => return None
})
}
}
}
}
/// Defines an enum that parametrically transforms its content
@ -56,16 +56,26 @@ macro_rules! transform_xy_unit {
pub fn y (y: U, item: T) -> Self { Self::Y(y, item) }
pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) }
}
impl<U: Copy + Coordinate, T> $Enum<U, T> {
pub fn dx (&self) -> U {
match self {
Self::X(x, _) => *x, Self::Y(_, _) => 0.into(), Self::XY(x, _, _) => *x,
}
}
pub fn dy (&self) -> U {
match self {
Self::X(_, _) => 0.into(), Self::Y(y, _) => *y, Self::XY(_, y, _) => *y,
}
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<E::Unit, RenderBox<'a, E>> {
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> {
let head = atoms.next()?;
if head.kind() != TokenKind::Key { return None }
Some(match head.text() {
$x => Self::x(
state.get_unit(&atoms.next().expect("no x")).expect("no x"),
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
$y => Self::y(
state.get_unit(&atoms.next().expect("no y")).expect("no y"),
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
$xy => Self::xy(
state.get_unit(&atoms.next().expect("no x")).expect("no x"),
state.get_unit(&atoms.next().expect("no y")).expect("no y"),
state.get_content(&atoms.next().expect("no content")).expect("no content"),
),
_ => return None
})
}
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
@ -80,26 +90,16 @@ macro_rules! transform_xy_unit {
$layout.into()
}
}
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<E::Unit, RenderBox<'a, E>> {
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> {
let head = atoms.next()?;
if head.kind() != TokenKind::Key { return None }
Some(match head.text() {
$x => Self::x(
state.get_unit(atoms.next().expect("no x")).expect("no x"),
state.get_content(atoms.next().expect("no content")).expect("no content")
),
$y => Self::y(
state.get_unit(atoms.next().expect("no y")).expect("no y"),
state.get_content(atoms.next().expect("no content")).expect("no content")
),
$xy => Self::xy(
state.get_unit(atoms.next().expect("no x")).expect("no x"),
state.get_unit(atoms.next().expect("no y")).expect("no y"),
state.get_content(atoms.next().expect("no content")).expect("no content"),
),
_ => return None
})
impl<U: Copy + Coordinate, T> $Enum<U, T> {
pub fn dx (&self) -> U {
match self {
Self::X(x, _) => *x, Self::Y(_, _) => 0.into(), Self::XY(x, _, _) => *x,
}
}
pub fn dy (&self) -> U {
match self {
Self::X(_, _) => 0.into(), Self::Y(y, _) => *y, Self::XY(_, y, _) => *y,
}
}
}
}
@ -111,7 +111,6 @@ transform_xy!("fill/x" "fill/y" "fill/xy" |self: Fill, to|{
X(_) => [x0, y, wmax, h],
Y(_) => [x, y0, w, hmax],
XY(_) => [x0, y0, wmax, hmax],
_ => unreachable!()
}.into()
});
transform_xy_unit!("fixed/x" "fixed/y" "fixed/xy"|self: Fixed, area|{
@ -120,14 +119,12 @@ transform_xy_unit!("fixed/x" "fixed/y" "fixed/xy"|self: Fixed, area|{
Self::X(fw, _) => [x, y, *fw, h],
Self::Y(fh, _) => [x, y, w, *fh],
Self::XY(fw, fh, _) => [x, y, *fw, *fh],
_ => unreachable!(),
};
let [x, y, w, h] = Render::layout(&self.content(), fixed_area.into()).xywh();
let fixed_area = match self {
Self::X(fw, _) => [x, y, *fw, h],
Self::Y(fh, _) => [x, y, w, *fh],
Self::XY(fw, fh, _) => [x, y, *fw, *fh],
_ => unreachable!(),
};
fixed_area
});
@ -137,7 +134,6 @@ transform_xy_unit!("min/x" "min/y" "min/xy"|self: Min, area|{
Self::X(mw, _) => [area.x(), area.y(), area.w().max(*mw), area.h()],
Self::Y(mh, _) => [area.x(), area.y(), area.w(), area.h().max(*mh)],
Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().max(*mw), area.h().max(*mh)],
_ => unreachable!(),
}});
transform_xy_unit!("max/x" "max/y" "max/xy"|self: Max, area|{
let [x, y, w, h] = area.xywh();
@ -145,15 +141,13 @@ transform_xy_unit!("max/x" "max/y" "max/xy"|self: Max, area|{
Self::X(fw, _) => [x, y, *fw, h],
Self::Y(fh, _) => [x, y, w, *fh],
Self::XY(fw, fh, _) => [x, y, *fw, *fh],
_ => unreachable!(),
}.into())});
transform_xy_unit!("shrink/x" "shrink/y" "shrink/xy"|self: Shrink, area|Render::layout(&self.content(), [
area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())
].into()));
transform_xy_unit!("expand/x" "expand/y" "expand/xy"|self: Expand, area|Render::layout(&self.content(), [
area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy()
].into()));
transform_xy_unit!("shrink/x" "shrink/y" "shrink/xy"|self: Shrink, area|Render::layout(
&self.content(),
[area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())].into()));
transform_xy_unit!("expand/x" "expand/y" "expand/xy"|self: Expand, area|Render::layout(
&self.content(),
[area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy()].into()));
transform_xy_unit!("push/x" "push/y" "push/xy"|self: Push, area|{
let area = Render::layout(&self.content(), area);
[area.x() + self.dx(), area.y() + self.dy(), area.w(), area.h()]