mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
implement TokensIterator::peek
This commit is contained in:
parent
a949117017
commit
92fcb0af8f
7 changed files with 196 additions and 198 deletions
|
|
@ -5,17 +5,17 @@ try_from_atoms!(<'a, E>: Align<RenderBox<'a, E>>: |state, atoms| {
|
|||
let head = atoms.next()?;
|
||||
if head.kind() != TokenKind::Key { return None }
|
||||
match head.text() {
|
||||
"align/c" => return Some(Self::c(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/x" => return Some(Self::x(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/y" => return Some(Self::y(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/n" => return Some(Self::n(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/s" => return Some(Self::s(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/e" => return Some(Self::e(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/w" => return Some(Self::w(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/nw" => return Some(Self::nw(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/ne" => return Some(Self::ne(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/sw" => return Some(Self::sw(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/se" => return Some(Self::se(state.get_content(atoms.next()?).expect("no content"))),
|
||||
"align/c" => return Some(Self::c(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
"align/x" => return Some(Self::x(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
"align/y" => return Some(Self::y(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
"align/n" => return Some(Self::n(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
"align/s" => return Some(Self::s(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
"align/e" => return Some(Self::e(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
"align/w" => return Some(Self::w(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
"align/nw" => return Some(Self::nw(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
"align/ne" => return Some(Self::ne(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
"align/sw" => return Some(Self::sw(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
"align/se" => return Some(Self::se(state.get_content(&atoms.next()?).expect("no content"))),
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
use crate::*;
|
||||
use RefAtom::*;
|
||||
pub use self::Direction::*;
|
||||
/// A cardinal direction.
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
|
|
@ -37,28 +36,28 @@ try_from_atoms!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, atoms|
|
|||
if head.kind() != TokenKind::Key { return None }
|
||||
match head.text() {
|
||||
"bsp/n" => return Some(Self::n(
|
||||
state.get_content(atoms.next()?).expect("no south"),
|
||||
state.get_content(atoms.next()?).expect("no north")
|
||||
state.get_content(&atoms.next()?).expect("no south"),
|
||||
state.get_content(&atoms.next()?).expect("no north")
|
||||
)),
|
||||
"bsp/s" => return Some(Self::s(
|
||||
state.get_content(atoms.next()?).expect("no north"),
|
||||
state.get_content(atoms.next()?).expect("no south")
|
||||
state.get_content(&atoms.next()?).expect("no north"),
|
||||
state.get_content(&atoms.next()?).expect("no south")
|
||||
)),
|
||||
"bsp/e" => return Some(Self::e(
|
||||
state.get_content(atoms.next()?).expect("no west"),
|
||||
state.get_content(atoms.next()?).expect("no east")
|
||||
state.get_content(&atoms.next()?).expect("no west"),
|
||||
state.get_content(&atoms.next()?).expect("no east")
|
||||
)),
|
||||
"bsp/w" => return Some(Self::w(
|
||||
state.get_content(atoms.next()?).expect("no east"),
|
||||
state.get_content(atoms.next()?).expect("no west")
|
||||
state.get_content(&atoms.next()?).expect("no east"),
|
||||
state.get_content(&atoms.next()?).expect("no west")
|
||||
)),
|
||||
"bsp/a" => return Some(Self::a(
|
||||
state.get_content(atoms.next()?).expect("no above"),
|
||||
state.get_content(atoms.next()?).expect("no below")
|
||||
state.get_content(&atoms.next()?).expect("no above"),
|
||||
state.get_content(&atoms.next()?).expect("no below")
|
||||
)),
|
||||
"bsp/b" => return Some(Self::b(
|
||||
state.get_content(atoms.next()?).expect("no above"),
|
||||
state.get_content(atoms.next()?).expect("no below")
|
||||
state.get_content(&atoms.next()?).expect("no above"),
|
||||
state.get_content(&atoms.next()?).expect("no below")
|
||||
)),
|
||||
_ => {}
|
||||
}
|
||||
|
|
@ -124,22 +123,18 @@ impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<A, B> {
|
|||
fn direction (&self) -> Direction { self.0 }
|
||||
fn contents (&self) -> (&A, &B) { (&self.1, &self.2) }
|
||||
}
|
||||
|
||||
/// Renders multiple things on top of each other,
|
||||
#[macro_export] macro_rules! lay {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::b(bsp, $expr);)*; bsp }}
|
||||
}
|
||||
|
||||
/// Stack southward.
|
||||
#[macro_export] macro_rules! col {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::s(bsp, $expr);)*; bsp }};
|
||||
}
|
||||
|
||||
/// Stack northward.
|
||||
#[macro_export] macro_rules! col_up {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::n(bsp, $expr);)*; bsp }}
|
||||
}
|
||||
|
||||
/// Stack eastward.
|
||||
#[macro_export] macro_rules! row {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::e(bsp, $expr);)*; bsp }};
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@ try_from_atoms!(<'a, E>: When<RenderBox<'a, E>>: |state, atoms| {
|
|||
let head = atoms.next()?;
|
||||
if (head.kind(), head.text()) == (TokenKind::Key, "when") {
|
||||
let condition = atoms.next();
|
||||
if let Some(condition) = condition {
|
||||
if let Some(ref condition) = condition {
|
||||
let condition = state.get_bool(condition).expect("no condition");
|
||||
if let Some(content) = atoms.next() {
|
||||
if let Some(ref content) = atoms.next() {
|
||||
let content = state.get_content(content).expect("no atom");
|
||||
return Some(Self(condition, content))
|
||||
}
|
||||
|
|
@ -22,11 +22,11 @@ try_from_atoms!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, ato
|
|||
let head = atoms.next()?;
|
||||
if (head.kind(), head.text()) == (TokenKind::Key, "either") {
|
||||
let condition = atoms.next();
|
||||
if let Some(condition) = condition {
|
||||
if let Some(ref condition) = condition {
|
||||
let condition = state.get_bool(condition).expect("no condition");
|
||||
if let Some(content1) = atoms.next() {
|
||||
if let Some(ref content1) = atoms.next() {
|
||||
let content1 = state.get_content(content1).expect("no content1");
|
||||
if let Some(content2) = atoms.next() {
|
||||
if let Some(ref content2) = atoms.next() {
|
||||
let content2 = state.get_content(content2).expect("no content2");
|
||||
return Some(Self(condition, content1, content2))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::*;
|
||||
use std::{sync::Arc, marker::PhantomData};
|
||||
use std::{sync::Arc, fmt::Debug};
|
||||
use TokenKind::*;
|
||||
/// Define an EDN-backed view.
|
||||
///
|
||||
|
|
@ -46,104 +46,112 @@ use TokenKind::*;
|
|||
}
|
||||
}
|
||||
/// Renders from EDN source and context.
|
||||
#[derive(Default)] pub enum AtomView<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> {
|
||||
///
|
||||
/// Generic over:
|
||||
///
|
||||
/// * `O` - Output target.
|
||||
/// * `S` - State provider.
|
||||
/// * `A` - Atom storage type.
|
||||
#[derive(Default, Clone)] pub enum AtomView<'a, O, S, A> {
|
||||
_Unused(std::marker::PhantomData<&'a O>),
|
||||
#[default] Inert,
|
||||
Ok(T, Atom<Arc<str>>),
|
||||
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
|
||||
Err(String),
|
||||
_Unused(PhantomData<&'a E>),
|
||||
Src(S, &'a str),
|
||||
Ref(S, &'a A),
|
||||
Own(S, A),
|
||||
Err(Arc<str>)
|
||||
}
|
||||
impl<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> std::fmt::Debug for AtomView<'a, E, T> {
|
||||
impl<'a, O, S, A> Debug for AtomView<'a, O, S, A>
|
||||
where S: Debug, A: Debug {
|
||||
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
match self {
|
||||
Self::Inert | Self::_Unused(_) =>
|
||||
Self::Inert =>
|
||||
write!(f, "AtomView::Inert"),
|
||||
Self::Ok(state, view) =>
|
||||
write!(f, "AtomView::Ok(state={state:?} view={view:?}"),
|
||||
Self::Src(state, view) =>
|
||||
write!(f, "AtomView::Src(state={state:?} view={view:?}"),
|
||||
Self::Ref(state, view) =>
|
||||
write!(f, "AtomView::Ref(state={state:?} view={view:?}"),
|
||||
Self::Own(state, view) =>
|
||||
write!(f, "AtomView::Arc(state={state:?} view={view:?}"),
|
||||
Self::Err(error) =>
|
||||
write!(f, "AtomView::Err({error})"),
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output, T> ViewContext<'a, E> for T where T:
|
||||
Context<'a, bool> +
|
||||
Context<'a, usize> +
|
||||
Context<'a, E::Unit> +
|
||||
Context<'a, Box<dyn Render<E> + 'a>>
|
||||
{}
|
||||
impl<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> AtomView<'a, E, T> {
|
||||
pub fn from_source (state: T, source: &'a str) -> Self {
|
||||
match RefAtom::read_one(&source) {
|
||||
Ok((layout, _)) => Self::Ok(state, layout),
|
||||
Err(error) => Self::Err(format!("{error} in {source}"))
|
||||
}
|
||||
}
|
||||
pub fn from_atoms (state: T, atoms: Vec<impl Atom>) -> Self {
|
||||
Self::Ok(state, Atom::Exp(atoms).to_arc())
|
||||
}
|
||||
}
|
||||
impl<E: Output, T: for<'a>ViewContext<'a, E> + Send + Sync + std::fmt::Debug> Content<E> for AtomView<'_, E, T> {
|
||||
fn content (&self) -> impl Render<E> {
|
||||
impl<'a, O, S, A> Content<O> for AtomView<'a, O, S, A>
|
||||
where O: Output, S: ViewContext<'a, O>, A: Atom {
|
||||
fn content (&self) -> impl Render<O> {
|
||||
match self {
|
||||
Self::Ok(state, layout) => state.get_content(layout),
|
||||
Self::Err(_error) => {
|
||||
panic!("{self:?}");
|
||||
//TODO:&format!("AtomView error: {error:?}")) // FIXME: String is not Render
|
||||
},
|
||||
_ => todo!()
|
||||
Self::Inert => { panic!("inert rendered") },
|
||||
Self::Err(e) => { panic!("render error: {e}") },
|
||||
Self::Src(state, source) => {},
|
||||
Self::Ref(state, atom) => {},
|
||||
Self::Own(state, atom) => {},
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output, T> ViewContext<'a, E> for T where T: Sized + Send + Sync
|
||||
+ Context<bool>
|
||||
+ Context<usize>
|
||||
+ Context<E::Unit>
|
||||
+ Context<Box<dyn Render<E> + 'a>> {}
|
||||
/// Provides values to the template
|
||||
pub trait ViewContext<'a, E: Output>:
|
||||
Context<'a, bool> +
|
||||
Context<'a, usize> +
|
||||
Context<'a, E::Unit> +
|
||||
Context<'a, Box<dyn Render<E> + 'a>>
|
||||
pub trait ViewContext<'a, E: Output>: Sized + Send + Sync
|
||||
+ Context<bool>
|
||||
+ Context<usize>
|
||||
+ Context<E::Unit>
|
||||
+ Context<Box<dyn Render<E> + 'a>>
|
||||
{
|
||||
fn get_bool (&'a self, atom: RefAtom<'a>) -> Option<bool> {
|
||||
fn get_bool (&self, atom: &impl Atom) -> Option<bool> {
|
||||
Some(match atom.kind() {
|
||||
Sym => match atom.text().as_ref() {
|
||||
":false" | ":f" => false,
|
||||
":true" | ":t" => true,
|
||||
Num => match atom.num() { 0 => false, _ => true },
|
||||
Sym => match atom.text() {
|
||||
":false" | ":f" => false, ":true" | ":t" => true,
|
||||
_ => return Context::get(self, atom)
|
||||
},
|
||||
Num => match atom.num() {
|
||||
0 => false,
|
||||
_ => true
|
||||
},
|
||||
_ => return Context::get(self, atom)
|
||||
})
|
||||
}
|
||||
fn get_usize (&'a self, atom: RefAtom<'a>) -> Option<usize> {
|
||||
fn get_usize (&self, atom: &impl Atom) -> Option<usize> {
|
||||
Some(match atom.kind() {
|
||||
Num => atom.num(),
|
||||
_ => return Context::get(self, atom)
|
||||
})
|
||||
}
|
||||
fn get_unit (&'a self, atom: RefAtom<'a>) -> Option<E::Unit> {
|
||||
fn get_unit (&self, atom: &impl Atom) -> Option<E::Unit> {
|
||||
Some(match atom.kind() {
|
||||
Num => E::Unit::from(atom.num() as u16),
|
||||
_ => return Context::get(self, atom)
|
||||
})
|
||||
}
|
||||
fn get_content (&'a self, atom: RefAtom<'a>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
try_delegate!(self, atom, When::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Either::<_, RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Align::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Bsp::<_, RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Fill::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Fixed::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Min::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Max::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Shrink::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Expand::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Push::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Pull::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Margin::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Padding::<_, _, RenderBox<'a, E>>);
|
||||
Context::get_or_fail(self, atom)
|
||||
fn get_content (&self, atom: &impl Atom) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
try_delegate!(self, atom, When::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Either::<RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Align::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Bsp::<RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Fill::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Fixed::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Min::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Max::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Shrink::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Expand::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Push::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Pull::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Margin::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Padding::<_, RenderBox<'a, E>>);
|
||||
Some(Context::get_or_fail(self, atom))
|
||||
}
|
||||
}
|
||||
#[macro_export] macro_rules! try_delegate {
|
||||
($s:ident, $atom:expr, $T:ty) => {
|
||||
if $atom.kind() == TokenKind::Exp {
|
||||
if let [head, tail @ ..] = $atom.as_slice() {
|
||||
if let Some(value) = <$T>::try_from_atoms($s, head, tail) {
|
||||
return Some(value.boxed())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// A function that returns a `RenderBox.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue