mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
wip: directionalize!
can't fit all into 1 trait because of directionality of trait implementation rules and constraints :(
This commit is contained in:
parent
f797a7143d
commit
7c1cddc759
10 changed files with 221 additions and 222 deletions
|
|
@ -36,20 +36,17 @@ pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
|
|||
pub struct Align<A>(Alignment, A);
|
||||
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'state, E: Output + 'state, T: ViewContext<'state, E>>
|
||||
Dsl<T> for Align<RenderBox<'state, E>> {
|
||||
fn take_from <'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
impl<State: Dsl<A>, A> FromDsl<State> for Align<A> {
|
||||
fn take_from <'state, 'source: 'state> (state: &'state State, iter: &mut TokenIter<'source>)
|
||||
-> Perhaps<Self>
|
||||
{
|
||||
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
|
||||
match key {
|
||||
"align/c"|"align/x"|"align/y"|
|
||||
"align/n"|"align/s"|"align/e"|"align/w"|
|
||||
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
|
||||
let _ = iter.next().unwrap();
|
||||
let content = if let Some(content) = state.get_content(&mut iter.clone())? {
|
||||
content
|
||||
} else {
|
||||
panic!("no content corresponding to {:?}", &iter);
|
||||
};
|
||||
let content = state.take_or_fail(&mut iter.clone(), "expected content")?;
|
||||
return Ok(Some(match key {
|
||||
"align/c" => Self::c(content),
|
||||
"align/x" => Self::x(content),
|
||||
|
|
|
|||
|
|
@ -21,9 +21,8 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
|
|||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'state, E: Output + 'state, T: ViewContext<'state, E>>
|
||||
Dsl<T> for Bsp<RenderBox<'state, E>, RenderBox<'state, E>> {
|
||||
fn take_from <'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
impl<A, B, T: Dsl<A> + Dsl<B>> FromDsl<T> for Bsp<A, B> {
|
||||
fn take_from <'state, 'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
Ok(if let Some(Token {
|
||||
value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"),
|
||||
..
|
||||
|
|
@ -31,23 +30,23 @@ Dsl<T> for Bsp<RenderBox<'state, E>, RenderBox<'state, E>> {
|
|||
let base = iter.clone();
|
||||
return Ok(Some(match iter.next() {
|
||||
Some(Token { value: Value::Key("bsp/n"), .. }) =>
|
||||
Self::n(state.get_content_or_fail(iter)?,
|
||||
state.get_content_or_fail(iter)?),
|
||||
Self::n(state.take_or_fail(iter, "expected content 1")?,
|
||||
state.take_or_fail(iter, "expected content 2")?),
|
||||
Some(Token { value: Value::Key("bsp/s"), .. }) =>
|
||||
Self::s(state.get_content_or_fail(iter)?,
|
||||
state.get_content_or_fail(iter)?),
|
||||
Self::s(state.take_or_fail(iter, "expected content 1")?,
|
||||
state.take_or_fail(iter, "expected content 2")?),
|
||||
Some(Token { value: Value::Key("bsp/e"), .. }) =>
|
||||
Self::e(state.get_content_or_fail(iter)?,
|
||||
state.get_content_or_fail(iter)?),
|
||||
Self::e(state.take_or_fail(iter, "expected content 1")?,
|
||||
state.take_or_fail(iter, "expected content 2")?),
|
||||
Some(Token { value: Value::Key("bsp/w"), .. }) =>
|
||||
Self::w(state.get_content_or_fail(iter)?,
|
||||
state.get_content_or_fail(iter)?),
|
||||
Self::w(state.take_or_fail(iter, "expected content 1")?,
|
||||
state.take_or_fail(iter, "expected content 2")?),
|
||||
Some(Token { value: Value::Key("bsp/a"), .. }) =>
|
||||
Self::a(state.get_content_or_fail(iter)?,
|
||||
state.get_content_or_fail(iter)?),
|
||||
Self::a(state.take_or_fail(iter, "expected content 1")?,
|
||||
state.take_or_fail(iter, "expected content 2")?),
|
||||
Some(Token { value: Value::Key("bsp/b"), .. }) =>
|
||||
Self::b(state.get_content_or_fail(iter)?,
|
||||
state.get_content_or_fail(iter)?),
|
||||
Self::b(state.take_or_fail(iter, "expected content 1")?,
|
||||
state.take_or_fail(iter, "expected content 2")?),
|
||||
_ => unreachable!(),
|
||||
}))
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -17,44 +17,26 @@ impl<A, B> Either<A, B> {
|
|||
Self(c, a, b)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'state, E: Output + 'state, T: ViewContext<'state, E>>
|
||||
Dsl<T> for When<RenderBox<'state, E>> {
|
||||
fn take_from <'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
impl<A, T: Dsl<bool> + Dsl<A>> FromDsl<T> for When<A> {
|
||||
fn take_from <'state, 'source: 'state> (
|
||||
state: &'state T,
|
||||
token: &mut TokenIter<'source>
|
||||
) -> Perhaps<Self> {
|
||||
Ok(if let Some(Token {
|
||||
value: Value::Key("when"),
|
||||
..
|
||||
}) = iter.peek() {
|
||||
let base = iter.clone();
|
||||
}) = token.peek() {
|
||||
let base = token.clone();
|
||||
return Ok(Some(Self(
|
||||
state.take(iter)?.unwrap_or_else(||panic!("cond: no condition: {base:?}")),
|
||||
state.get_content_or_fail(iter)?
|
||||
state.take_or_fail(token, "cond: no condition")?,
|
||||
state.take_or_fail(token, "cond: no content")?,
|
||||
)))
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'state, E: Output + 'state, T: ViewContext<'state, E>>
|
||||
Dsl<T> for Either<RenderBox<'state, E>, RenderBox<'state, E>> {
|
||||
fn take_from <'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
if let Some(Token { value: Value::Key("either"), .. }) = iter.peek() {
|
||||
let base = iter.clone();
|
||||
let _ = iter.next().unwrap();
|
||||
//panic!("{iter:?}");
|
||||
return Ok(Some(Self(
|
||||
state.take(iter)?.unwrap_or_else(||panic!("either: no condition: {base:?}")),
|
||||
state.get_content_or_fail(iter)?,
|
||||
state.get_content_or_fail(iter)?,
|
||||
)))
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Output, A: Render<E>> Content<E> for When<A> {
|
||||
fn layout (&self, to: E::Area) -> E::Area {
|
||||
let Self(cond, item) = self;
|
||||
|
|
@ -73,7 +55,24 @@ impl<E: Output, A: Render<E>> Content<E> for When<A> {
|
|||
if *cond { item.render(to) }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<A, B, T: Dsl<bool> + Dsl<A> + Dsl<B>> FromDsl<T> for Either<A, B> {
|
||||
fn take_from <'state, 'source: 'state> (
|
||||
state: &'state T,
|
||||
token: &mut TokenIter<'source>
|
||||
) -> Perhaps<Self> {
|
||||
if let Some(Token { value: Value::Key("either"), .. }) = token.peek() {
|
||||
let base = token.clone();
|
||||
let _ = token.next().unwrap();
|
||||
return Ok(Some(Self(
|
||||
state.take_or_fail(token, "either: no condition")?,
|
||||
state.take_or_fail(token, "either: no content 1")?,
|
||||
state.take_or_fail(token, "either: no content 2")?,
|
||||
)))
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
|
||||
fn layout (&self, to: E::Area) -> E::Area {
|
||||
let Self(cond, a, b) = self;
|
||||
|
|
|
|||
|
|
@ -26,31 +26,24 @@ use crate::*;
|
|||
/// along either the X axis, the Y axis, or both.
|
||||
macro_rules! transform_xy {
|
||||
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$area:expr) => {
|
||||
pub enum $Enum<T> { X(T), Y(T), XY(T) }
|
||||
impl<T> $Enum<T> {
|
||||
#[inline] pub const fn x (item: T) -> Self {
|
||||
Self::X(item)
|
||||
}
|
||||
#[inline] pub const fn y (item: T) -> Self {
|
||||
Self::Y(item)
|
||||
}
|
||||
#[inline] pub const fn xy (item: T) -> Self {
|
||||
Self::XY(item)
|
||||
}
|
||||
pub enum $Enum<A> { X(A), Y(A), XY(A) }
|
||||
impl<A> $Enum<A> {
|
||||
#[inline] pub const fn x (item: A) -> Self { Self::X(item) }
|
||||
#[inline] pub const fn y (item: A) -> Self { Self::Y(item) }
|
||||
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'state, E: Output + 'state, T: ViewContext<'state, E>>
|
||||
Dsl<T> for $Enum<RenderBox<'state, E>> {
|
||||
fn take_from <'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
impl<A, T: Dsl<A>> FromDsl<T> for $Enum<A> {
|
||||
fn take_from <'state, 'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
if let Some(Token { value: Value::Key(k), .. }) = iter.peek() {
|
||||
let mut base = iter.clone();
|
||||
return Ok(Some(match iter.next() {
|
||||
Some(Token{value:Value::Key($x),..}) =>
|
||||
Self::x(state.get_content_or_fail(iter)?),
|
||||
Self::x(state.take_or_fail(iter, "x: no content")?),
|
||||
Some(Token{value:Value::Key($y),..}) =>
|
||||
Self::y(state.get_content_or_fail(iter)?),
|
||||
Self::y(state.take_or_fail(iter, "y: no content")?),
|
||||
Some(Token{value:Value::Key($xy),..}) =>
|
||||
Self::xy(state.get_content_or_fail(iter)?),
|
||||
Self::xy(state.take_or_fail(iter, "xy: no content")?),
|
||||
_ => unreachable!()
|
||||
}))
|
||||
}
|
||||
|
|
@ -77,31 +70,30 @@ macro_rules! transform_xy {
|
|||
/// along either the X axis, the Y axis, or both.
|
||||
macro_rules! transform_xy_unit {
|
||||
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$layout:expr) => {
|
||||
pub enum $Enum<U, T> { X(U, T), Y(U, T), XY(U, U, T), }
|
||||
impl<U, T> $Enum<U, T> {
|
||||
#[inline] pub const fn x (x: U, item: T) -> Self { Self::X(x, item) }
|
||||
#[inline] pub const fn y (y: U, item: T) -> Self { Self::Y(y, item) }
|
||||
#[inline] pub const fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) }
|
||||
pub enum $Enum<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
||||
impl<U, A> $Enum<U, A> {
|
||||
#[inline] pub const fn x (x: U, item: A) -> Self { Self::X(x, item) }
|
||||
#[inline] pub const fn y (y: U, item: A) -> Self { Self::Y(y, item) }
|
||||
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'state, E: Output + 'state, T: ViewContext<'state, E>>
|
||||
Dsl<T> for $Enum<E::Unit, RenderBox<'state, E>> {
|
||||
fn take_from <'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
impl<A, U: Coordinate, T: Dsl<A> + Dsl<U>> FromDsl<T> for $Enum<U, A> {
|
||||
fn take_from <'state, 'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
Ok(if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = iter.peek() {
|
||||
let mut base = iter.clone();
|
||||
Some(match iter.next() {
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::x(
|
||||
state.take_or_fail(iter, "no unit specified")?,
|
||||
state.get_content_or_fail(iter)?,
|
||||
state.take_or_fail(iter, "x: no unit")?,
|
||||
state.take_or_fail(iter, "x: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($y), .. }) => Self::y(
|
||||
state.take_or_fail(iter, "no unit specified")?,
|
||||
state.get_content_or_fail(iter)?,
|
||||
state.take_or_fail(iter, "y: no unit")?,
|
||||
state.take_or_fail(iter, "y: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
||||
state.take_or_fail(iter, "no unit specified")?,
|
||||
state.take_or_fail(iter, "no unit specified")?,
|
||||
state.get_content_or_fail(iter)?
|
||||
state.take_or_fail(iter, "xy: no unit x")?,
|
||||
state.take_or_fail(iter, "xy: no unit y")?,
|
||||
state.take_or_fail(iter, "xy: no content")?
|
||||
),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,63 +1,54 @@
|
|||
use crate::*;
|
||||
|
||||
#[cfg(feature = "dsl")]
|
||||
#[macro_export] macro_rules! try_delegate {
|
||||
($s:ident, $dsl:expr, $T:ty) => {
|
||||
let value: Option<$T> = Dsl::take_from($s, $dsl)?;
|
||||
if let Some(value) = value {
|
||||
return Ok(Some(value.boxed()))
|
||||
}
|
||||
}
|
||||
}
|
||||
//#[cfg(feature = "dsl")]
|
||||
//#[macro_export] macro_rules! try_delegate {
|
||||
//($s:ident, $dsl:expr, $T:ty) => {
|
||||
//let value: Option<$T> = Dsl::take_from($s, $dsl)?;
|
||||
//if let Some(value) = value {
|
||||
//return Ok(Some(value.boxed()))
|
||||
//}
|
||||
//}
|
||||
//}
|
||||
|
||||
// Provides components to the view.
|
||||
#[cfg(feature = "dsl")]
|
||||
pub trait ViewContext<'state, E: Output + 'state>: Send + Sync where
|
||||
bool: Dsl<Self>,
|
||||
usize: Dsl<Self>,
|
||||
E::Unit: Dsl<Self>,
|
||||
{
|
||||
fn get_content_or_fail <'source: 'state> (&'state self, iter: &mut TokenIter<'source>)
|
||||
-> Usually<RenderBox<'state, E>>
|
||||
{
|
||||
let base = iter.clone();
|
||||
if let Some(content) = self.get_content(iter)? {
|
||||
Ok(content)
|
||||
} else {
|
||||
Err(format!("not found: {iter:?}").into())
|
||||
}
|
||||
}
|
||||
fn get_content <'source: 'state> (&'state self, iter: &mut TokenIter<'source>)
|
||||
-> Perhaps<RenderBox<'state, E>>
|
||||
{
|
||||
match iter.peek() {
|
||||
Some(Token { value: Value::Sym(_), .. }) =>
|
||||
self.get_content_sym(iter),
|
||||
Some(Token { value: Value::Exp(_, _), .. }) =>
|
||||
self.get_content_exp(iter),
|
||||
None => Ok(None),
|
||||
_ => panic!("only :symbols and (expressions) accepted here")
|
||||
}
|
||||
}
|
||||
fn get_content_sym <'source: 'state> (&'state self, iter: &mut TokenIter<'source>)
|
||||
-> Perhaps<RenderBox<'state, E>>;
|
||||
fn get_content_exp <'source: 'state> (&'state self, iter: &mut TokenIter<'source>)
|
||||
-> Perhaps<RenderBox<'state, E>>
|
||||
{
|
||||
try_delegate!(self, iter, When::<RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Either::<RenderBox<'state, E>, RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Align::<RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Bsp::<RenderBox<'state, E>, RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Fill::<RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Fixed::<_, RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Min::<_, RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Max::<_, RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Shrink::<_, RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Expand::<_, RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Push::<_, RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Pull::<_, RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Margin::<_, RenderBox<'state, E>>);
|
||||
try_delegate!(self, iter, Padding::<_, RenderBox<'state, E>>);
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
//// Provides components to the view.
|
||||
//#[cfg(feature = "dsl")]
|
||||
//pub trait ViewContext<'state, E: Output + 'state>:
|
||||
//FromDsl<bool> + FromDsl<usize> + FromDsl<E::Unit> + Send + Sync
|
||||
//{
|
||||
//fn get_content_sym <'source: 'state> (&'state self, iter: &mut TokenIter<'source>)
|
||||
//-> Perhaps<RenderBox<'state, E>>;
|
||||
//fn get_content_exp <'source: 'state> (&'state self, iter: &mut TokenIter<'source>)
|
||||
//-> Perhaps<RenderBox<'state, E>>
|
||||
//{
|
||||
//try_delegate!(self, iter, When::<RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Either::<RenderBox<'state, E>, RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Align::<RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Bsp::<RenderBox<'state, E>, RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Fill::<RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Fixed::<_, RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Min::<_, RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Max::<_, RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Shrink::<_, RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Expand::<_, RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Push::<_, RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Pull::<_, RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Margin::<_, RenderBox<'state, E>>);
|
||||
//try_delegate!(self, iter, Padding::<_, RenderBox<'state, E>>);
|
||||
//Ok(None)
|
||||
//}
|
||||
//}
|
||||
|
||||
//#[cfg(feature = "dsl")]
|
||||
//impl<'context, O: Output + 'context, T: ViewContext<'context, O>> FromDsl<T> for RenderBox<'context, O> {
|
||||
//fn take_from <'state, 'source: 'state> (state: &'state T, token: &mut TokenIter<'source>)
|
||||
//-> Perhaps<RenderBox<'context, O>>
|
||||
//{
|
||||
//Ok(if let Some(content) = state.get_content_sym(token)? {
|
||||
//Some(content)
|
||||
//} else if let Some(content) = state.get_content_exp(token)? {
|
||||
//Some(content)
|
||||
//} else {
|
||||
//None
|
||||
//})
|
||||
//}
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue