mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
wip: dsl, output, input, proc, tui: sorting out give and take
Some checks are pending
/ build (push) Waiting to run
Some checks are pending
/ build (push) Waiting to run
This commit is contained in:
parent
5a2177cc77
commit
3e1084555b
10 changed files with 273 additions and 301 deletions
|
|
@ -31,32 +31,38 @@ use crate::*;
|
|||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
|
||||
pub struct Align<A>(Alignment, A);
|
||||
#[cfg(feature = "dsl")]take!(Align<A>, A|state: Give<A>, words|Ok(Some(match words.peek() {
|
||||
Some(Token { value: Value::Key(key), .. }) => match key {
|
||||
"align/c"|"align/x"|"align/y"|
|
||||
"align/n"|"align/s"|"align/e"|"al;qign/w"|
|
||||
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
|
||||
let _ = words.next().unwrap();
|
||||
let content = state.give_or_fail(&mut words.clone(), ||"expected content")?;
|
||||
match key {
|
||||
"align/c" => Self::c(content),
|
||||
"align/x" => Self::x(content),
|
||||
"align/y" => Self::y(content),
|
||||
"align/n" => Self::n(content),
|
||||
"align/s" => Self::s(content),
|
||||
"align/e" => Self::e(content),
|
||||
"align/w" => Self::w(content),
|
||||
"align/nw" => Self::nw(content),
|
||||
"align/ne" => Self::ne(content),
|
||||
"align/sw" => Self::sw(content),
|
||||
"align/se" => Self::se(content),
|
||||
_ => unreachable!()
|
||||
}
|
||||
},
|
||||
_ => return Ok(None)
|
||||
},
|
||||
_ => return Ok(None)
|
||||
})));
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'state, State: Give<'state, A>, A: 'state> Take<'state, State> for Align<A> {
|
||||
fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
//give!(Align<A>, A|state, words|Ok(Some(match words.peek() {
|
||||
//Some(Token { value: Value::Key(key), .. }) => match key {
|
||||
//"align/c"|"align/x"|"align/y"|
|
||||
//"align/n"|"align/s"|"align/e"|"al;qign/w"|
|
||||
//"align/nw"|"align/sw"|"align/ne"|"align/se" => {
|
||||
//let _ = words.next().unwrap();
|
||||
//let content = Take::take_or_fail(state, &mut words.clone(), ||"expected content")?;
|
||||
//match key {
|
||||
//"align/c" => Self::c(content),
|
||||
//"align/x" => Self::x(content),
|
||||
//"align/y" => Self::y(content),
|
||||
//"align/n" => Self::n(content),
|
||||
//"align/s" => Self::s(content),
|
||||
//"align/e" => Self::e(content),
|
||||
//"align/w" => Self::w(content),
|
||||
//"align/nw" => Self::nw(content),
|
||||
//"align/ne" => Self::ne(content),
|
||||
//"align/sw" => Self::sw(content),
|
||||
//"align/se" => Self::se(content),
|
||||
//_ => unreachable!()
|
||||
//}
|
||||
//},
|
||||
//_ => return Ok(None)
|
||||
//},
|
||||
//_ => return Ok(None)
|
||||
//})));
|
||||
|
||||
impl<A> Align<A> {
|
||||
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }
|
||||
|
|
|
|||
|
|
@ -20,34 +20,29 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
|
|||
}
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'n, State: Give<A> + Give<B>, A: 'n, B: 'n> Take<'n, State> for Bsp<A, B> {
|
||||
fn take <'source> (state: &State, words: &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"),
|
||||
..
|
||||
}) = words.peek() {
|
||||
if let Value::Key(key) = words.next().unwrap().value() {
|
||||
let base = words.clone();
|
||||
let a: A = state.give_or_fail(words, ||"bsp: expected content 1")?;
|
||||
let b: B = state.give_or_fail(words, ||"bsp: expected content 2")?;
|
||||
return Ok(Some(match key {
|
||||
"bsp/n" => Self::n(a, b),
|
||||
"bsp/s" => Self::s(a, b),
|
||||
"bsp/e" => Self::e(a, b),
|
||||
"bsp/w" => Self::w(a, b),
|
||||
"bsp/a" => Self::a(a, b),
|
||||
"bsp/b" => Self::b(a, b),
|
||||
_ => unreachable!(),
|
||||
}))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
} else {
|
||||
None
|
||||
})
|
||||
#[cfg(feature = "dsl")] take!(Bsp<A, B>, A, B|state, words|Ok(if let Some(Token {
|
||||
value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"),
|
||||
..
|
||||
}) = words.peek() {
|
||||
if let Value::Key(key) = words.next().unwrap().value() {
|
||||
let base = words.clone();
|
||||
let a: A = state.give_or_fail(words, ||"bsp: expected content 1")?;
|
||||
let b: B = state.give_or_fail(words, ||"bsp: expected content 2")?;
|
||||
return Ok(Some(match key {
|
||||
"bsp/n" => Self::n(a, b),
|
||||
"bsp/s" => Self::s(a, b),
|
||||
"bsp/e" => Self::e(a, b),
|
||||
"bsp/w" => Self::w(a, b),
|
||||
"bsp/a" => Self::a(a, b),
|
||||
"bsp/b" => Self::b(a, b),
|
||||
_ => unreachable!(),
|
||||
}))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}));
|
||||
impl<A, B> Bsp<A, B> {
|
||||
#[inline] pub const fn n (a: A, b: B) -> Self { Self(North, a, b) }
|
||||
#[inline] pub const fn s (a: A, b: B) -> Self { Self(South, a, b) }
|
||||
|
|
|
|||
|
|
@ -8,21 +8,18 @@ impl<A, B> Either<A, B> {
|
|||
Self(c, a, b)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'n, State: Give<bool> + Give<A> + Give<B>, A: 'n, B: 'n> Take<'n, State> for Either<A, B> {
|
||||
fn take <'source> (state: &State, 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.give_or_fail(token, ||"either: no condition")?,
|
||||
state.give_or_fail(token, ||"either: no content 1")?,
|
||||
state.give_or_fail(token, ||"either: no content 2")?,
|
||||
)))
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")] take!(Either<A, B>, A, B|state, words|Ok(
|
||||
if let Some(Token { value: Value::Key("either"), .. }) = words.peek() {
|
||||
let base = words.clone();
|
||||
let _ = words.next().unwrap();
|
||||
return Ok(Some(Self(
|
||||
state.give_or_fail(words, ||"either: no condition")?,
|
||||
state.give_or_fail(words, ||"either: no content 1")?,
|
||||
state.give_or_fail(words, ||"either: no content 2")?,
|
||||
)))
|
||||
} else {
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -32,26 +32,19 @@ macro_rules! transform_xy {
|
|||
#[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<'n, A: 'n, T: Give<A>> Take<'n, T> for $Enum<A> {
|
||||
fn take <'source> (state: &T, token: &mut TokenIter<'source>)
|
||||
-> Perhaps<Self>
|
||||
{
|
||||
if let Some(Token { value: Value::Key(k), .. }) = token.peek() {
|
||||
let mut base = token.clone();
|
||||
return Ok(Some(match token.next() {
|
||||
Some(Token{value:Value::Key($x),..}) =>
|
||||
Self::x(state.give_or_fail(token, ||"x: no content")?),
|
||||
Some(Token{value:Value::Key($y),..}) =>
|
||||
Self::y(state.give_or_fail(token, ||"y: no content")?),
|
||||
Some(Token{value:Value::Key($xy),..}) =>
|
||||
Self::xy(state.give_or_fail(token, ||"xy: no content")?),
|
||||
_ => unreachable!()
|
||||
}))
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")] take!($Enum<A>, A|state, words|Ok(
|
||||
if let Some(Token { value: Value::Key(k), .. }) = words.peek() {
|
||||
let mut base = words.clone();
|
||||
let content = state.give_or_fail(words, ||format!("{k}: no content"))?;
|
||||
return Ok(Some(match words.next() {
|
||||
Some(Token{value: Value::Key($x),..}) => Self::x(content),
|
||||
Some(Token{value: Value::Key($y),..}) => Self::y(content),
|
||||
Some(Token{value: Value::Key($xy),..}) => Self::xy(content),
|
||||
_ => unreachable!()
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}));
|
||||
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
|
||||
fn content (&self) -> impl Render<E> + '_ {
|
||||
match self {
|
||||
|
|
@ -78,56 +71,45 @@ macro_rules! transform_xy_unit {
|
|||
#[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<'n, A: 'n, U: Coordinate + 'n, T: Give<A> + Give<U>> Take<'n, T> for $Enum<U, A> {
|
||||
fn take <'source> (
|
||||
state: &T, token: &mut TokenIter<'source>
|
||||
) -> Perhaps<Self> {
|
||||
Ok(if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = token.peek() {
|
||||
let mut base = token.clone();
|
||||
Some(match token.next() {
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::x(
|
||||
state.give_or_fail(token, ||"x: no unit")?,
|
||||
state.give_or_fail(token, ||"x: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($y), .. }) => Self::y(
|
||||
state.give_or_fail(token, ||"y: no unit")?,
|
||||
state.give_or_fail(token, ||"y: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
||||
state.give_or_fail(token, ||"xy: no unit x")?,
|
||||
state.give_or_fail(token, ||"xy: no unit y")?,
|
||||
state.give_or_fail(token, ||"xy: no content")?
|
||||
),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
#[cfg(feature = "dsl")] take!($Enum<U, A>, U, A|state, words|Ok(
|
||||
if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = words.peek() {
|
||||
let mut base = words.clone();
|
||||
Some(match words.next() {
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::x(
|
||||
state.give_or_fail(words, ||"x: no unit")?,
|
||||
state.give_or_fail(words, ||"x: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($y), .. }) => Self::y(
|
||||
state.give_or_fail(words, ||"y: no unit")?,
|
||||
state.give_or_fail(words, ||"y: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
||||
state.give_or_fail(words, ||"xy: no unit x")?,
|
||||
state.give_or_fail(words, ||"xy: no unit y")?,
|
||||
state.give_or_fail(words, ||"xy: no content")?
|
||||
),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}));
|
||||
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
|
||||
fn content (&self) -> impl Render<E> + '_ {
|
||||
Some(match self {
|
||||
Self::X(_, content) => content,
|
||||
Self::Y(_, content) => content,
|
||||
Self::XY(_, _, content) => content,
|
||||
})
|
||||
}
|
||||
fn layout (&$self, $to: E::Area) -> E::Area {
|
||||
$layout.into()
|
||||
}
|
||||
fn content (&self) -> impl Render<E> + '_ {
|
||||
use $Enum::*;
|
||||
Some(match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, })
|
||||
}
|
||||
}
|
||||
impl<U: Copy + Coordinate, T> $Enum<U, T> {
|
||||
impl<U: Coordinate, T> $Enum<U, T> {
|
||||
#[inline] pub fn dx (&self) -> U {
|
||||
match self {
|
||||
Self::X(x, _) => *x, Self::Y(_, _) => 0.into(), Self::XY(x, _, _) => *x,
|
||||
}
|
||||
use $Enum::*;
|
||||
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
|
||||
}
|
||||
#[inline] pub fn dy (&self) -> U {
|
||||
match self {
|
||||
Self::X(_, _) => 0.into(), Self::Y(y, _) => *y, Self::XY(_, y, _) => *y,
|
||||
}
|
||||
use $Enum::*;
|
||||
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,33 +1,22 @@
|
|||
use crate::*;
|
||||
|
||||
/// Show an item only when a condition is true.
|
||||
pub struct When<A>(pub bool, pub A);
|
||||
|
||||
impl<A> When<A> {
|
||||
/// Create a binary condition.
|
||||
pub const fn new (c: bool, a: A) -> Self {
|
||||
Self(c, a)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'n, State: Give<bool>, A: Take<'n, State>> Take<'n, State> for When<A> {
|
||||
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
Ok(if let Some(Token {
|
||||
value: Value::Key("when"),
|
||||
..
|
||||
}) = words.peek() {
|
||||
let _ = words.next();
|
||||
let base = words.clone();
|
||||
return Ok(Some(Self(
|
||||
state.give_or_fail(words, ||"cond: no condition")?,
|
||||
A::take_or_fail(state, words, ||"cond: no content")?,
|
||||
)))
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")]take!(When<A>, A|state, words|Ok(Some(match words.peek() {
|
||||
Some(Token { value: Value::Key("when"), .. }) => {
|
||||
let _ = words.next();
|
||||
let base = words.clone();
|
||||
let cond = state.give_or_fail(words, ||"cond: no condition")?;
|
||||
let cont = state.give_or_fail(words, ||"cond: no content")?;
|
||||
Self(cond, cont)
|
||||
},
|
||||
_ => return Ok(None)
|
||||
})));
|
||||
|
||||
impl<E: Output, A: Render<E>> Content<E> for When<A> {
|
||||
fn layout (&self, to: E::Area) -> E::Area {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue