dsl: Provide -> Take, Receive -> Give (swap + shorten)

This commit is contained in:
🪞👃🪞 2025-05-23 21:39:29 +03:00
parent 583660c330
commit ddf0c05d5f
10 changed files with 64 additions and 55 deletions

View file

@ -36,7 +36,7 @@ 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<'n, State: Receive<A>, A: 'n> Provide<'n, State> for Align<A> {
impl<'n, State: Give<A>, A: 'n> Take<'n, State> for Align<A> {
fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
if let Some(Token { value: Value::Key(key), .. }) = words.peek() {
match key {
@ -44,7 +44,7 @@ impl<'n, State: Receive<A>, A: 'n> Provide<'n, State> for Align<A> {
"align/n"|"align/s"|"align/e"|"align/w"|
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
let _ = words.next().unwrap();
let content = state.receive_or_fail(&mut words.clone(), ||"expected content")?;
let content = state.give_or_fail(&mut words.clone(), ||"expected content")?;
return Ok(Some(match key {
"align/c" => Self::c(content),
"align/x" => Self::x(content),

View file

@ -21,7 +21,7 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
}
}
#[cfg(feature = "dsl")]
impl<'n, State: Receive<A> + Receive<B>, A: 'n, B: 'n> Provide<'n, State> for Bsp<A, B> {
impl<'n, State: Give<A> + Give<B>, A: 'n, B: 'n> Take<'n, State> for Bsp<A, B> {
fn provide <'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"),
@ -29,8 +29,8 @@ impl<'n, State: Receive<A> + Receive<B>, A: 'n, B: 'n> Provide<'n, State> for Bs
}) = words.peek() {
if let Value::Key(key) = words.next().unwrap().value() {
let base = words.clone();
let a: A = state.receive_or_fail(words, ||"bsp: expected content 1")?;
let b: B = state.receive_or_fail(words, ||"bsp: expected content 2")?;
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),

View file

@ -9,15 +9,15 @@ impl<A, B> Either<A, B> {
}
}
#[cfg(feature = "dsl")]
impl<'n, State: Receive<bool> + Receive<A> + Receive<B>, A: 'n, B: 'n> Provide<'n, State> for Either<A, B> {
impl<'n, State: Give<bool> + Give<A> + Give<B>, A: 'n, B: 'n> Take<'n, State> for Either<A, B> {
fn provide <'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.receive_or_fail(token, ||"either: no condition")?,
state.receive_or_fail(token, ||"either: no content 1")?,
state.receive_or_fail(token, ||"either: no content 2")?,
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)

View file

@ -33,7 +33,7 @@ macro_rules! transform_xy {
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
}
#[cfg(feature = "dsl")]
impl<'n, A: 'n, T: Receive<A>> Provide<'n, T> for $Enum<A> {
impl<'n, A: 'n, T: Give<A>> Take<'n, T> for $Enum<A> {
fn provide <'source> (state: &T, token: &mut TokenIter<'source>)
-> Perhaps<Self>
{
@ -41,11 +41,11 @@ macro_rules! transform_xy {
let mut base = token.clone();
return Ok(Some(match token.next() {
Some(Token{value:Value::Key($x),..}) =>
Self::x(state.receive_or_fail(token, ||"x: no content")?),
Self::x(state.give_or_fail(token, ||"x: no content")?),
Some(Token{value:Value::Key($y),..}) =>
Self::y(state.receive_or_fail(token, ||"y: no content")?),
Self::y(state.give_or_fail(token, ||"y: no content")?),
Some(Token{value:Value::Key($xy),..}) =>
Self::xy(state.receive_or_fail(token, ||"xy: no content")?),
Self::xy(state.give_or_fail(token, ||"xy: no content")?),
_ => unreachable!()
}))
}
@ -79,7 +79,7 @@ macro_rules! transform_xy_unit {
#[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: Receive<A> + Receive<U>> Provide<'n, T> for $Enum<U, A> {
impl<'n, A: 'n, U: Coordinate + 'n, T: Give<A> + Give<U>> Take<'n, T> for $Enum<U, A> {
fn provide <'source> (
state: &T, token: &mut TokenIter<'source>
) -> Perhaps<Self> {
@ -87,17 +87,17 @@ macro_rules! transform_xy_unit {
let mut base = token.clone();
Some(match token.next() {
Some(Token { value: Value::Key($x), .. }) => Self::x(
state.receive_or_fail(token, ||"x: no unit")?,
state.receive_or_fail(token, ||"x: no content")?,
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.receive_or_fail(token, ||"y: no unit")?,
state.receive_or_fail(token, ||"y: no content")?,
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.receive_or_fail(token, ||"xy: no unit x")?,
state.receive_or_fail(token, ||"xy: no unit y")?,
state.receive_or_fail(token, ||"xy: no content")?
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!(),
})

View file

@ -2,14 +2,16 @@ 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: Receive<bool> + Receive<A>, A: 'n> Provide<'n, State> for When<A> {
impl<'n, State: Give<bool>, A: Take<'n, State>> Take<'n, State> for When<A> {
fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
Ok(if let Some(Token {
value: Value::Key("when"),
@ -18,14 +20,15 @@ impl<'n, State: Receive<bool> + Receive<A>, A: 'n> Provide<'n, State> for When<A
let _ = words.next();
let base = words.clone();
return Ok(Some(Self(
state.receive_or_fail(words, ||"cond: no condition")?,
state.receive_or_fail(words, ||"cond: no content")?,
state.give_or_fail(words, ||"cond: no condition")?,
A::provide_or_fail(state, words, ||"cond: no content")?,
)))
} else {
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;