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

@ -1,25 +1,31 @@
use crate::*;
pub trait Receive<Type> {
fn receive <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type>;
fn receive_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
// maybe their names should be switched around?
/// [Take]s instances of [Type] given [TokenIter].
pub trait Give<Type> {
/// Implement this to be able to [Give] [Type] from the [TokenIter].
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type>;
/// Return custom error on [None].
fn give_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
&self, words: &mut TokenIter<'source>, error: F
) -> Usually<Type> {
if let Some(value) = Receive::<Type>::receive(self, words)? {
if let Some(value) = Give::<Type>::give(self, words)? {
Ok(value)
} else {
Result::Err(format!("receive: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
Result::Err(format!("give: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
}
}
}
pub trait Provide<'n, State>: Sized + 'n {
fn provide <'source> (state: &State, words: &mut TokenIter<'source>)
-> Perhaps<Self>;
/// [Give]s instances of [Self] given [TokenIter].
pub trait Take<'n, State>: Sized + 'n {
fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self>;
/// Return custom error on [None].
fn provide_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
state: &State, words: &mut TokenIter<'source>, error: F
) -> Usually<Self> {
if let Some(value) = Provide::<State>::provide(state, words)? {
if let Some(value) = Take::<State>::provide(state, words)? {
Ok(value)
} else {
Result::Err(format!("provide: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
@ -27,25 +33,25 @@ pub trait Provide<'n, State>: Sized + 'n {
}
}
/// Implement the [Receive] trait, which boils down to
/// Implement the [Give] trait, which boils down to
/// specifying two types and providing an expression.
#[macro_export] macro_rules! from_dsl {
(@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
impl<'n, State, A: Provide<'n, State>> Provide<'n, State> for $T {
impl<'n, State, A: Take<'n, State>> Take<'n, State> for $T {
fn provide <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
$expr
}
}
};
(@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
impl<'n, State, A: Provide<'n, State>, B: Provide<'n, State>> Provide<'n, State> for $T {
impl<'n, State, A: Take<'n, State>, B: Take<'n, State>> Take<'n, State> for $T {
fn provide <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
$expr
}
}
};
($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => {
impl<'n> Provide<'n, $S> for $T {
impl<'n> Take<'n, $S> for $T {
fn provide <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
$expr
}
@ -55,7 +61,7 @@ pub trait Provide<'n, State>: Sized + 'n {
// auto impl graveyard:
//impl<'n, State: Receive<Type>, Type: 'n> Provide<'n, State> for Type {
//impl<'n, State: Give<Type>, Type: 'n> Take<'n, State> for Type {
//fn provide <'source> (state: &State, words: &mut TokenIter<'source>)
//-> Perhaps<Self>
//{
@ -63,7 +69,7 @@ pub trait Provide<'n, State>: Sized + 'n {
//}
//}
//impl<'n, Type: Provide<'n, State>, State> Receive<Type> for State {
//impl<'n, Type: Take<'n, State>, State> Give<Type> for State {
//fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
//Type::provide(self, words)
//}

View file

@ -5,13 +5,13 @@ use std::marker::PhantomData;
pub trait DslInput: Input { fn matches_dsl (&self, token: &str) -> bool; }
/// A pre-configured mapping of input events to commands.
pub trait KeyMap<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> {
pub trait KeyMap<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> {
/// Try to find a command that matches the current input event.
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C>;
}
/// A [SourceIter] can be a [KeyMap].
impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for SourceIter<'k> {
impl<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for SourceIter<'k> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
let mut iter = self.clone();
while let Some((token, rest)) = iter.next() {
@ -22,7 +22,7 @@ impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for
match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = Provide::provide(state, &mut exp_iter)? {
if let Some(command) = Take::provide(state, &mut exp_iter)? {
return Ok(Some(command))
}
}
@ -38,7 +38,7 @@ impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for
}
/// A [TokenIter] can be a [KeyMap].
impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for TokenIter<'k> {
impl<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for TokenIter<'k> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
let mut iter = self.clone();
while let Some(next) = iter.next() {
@ -48,7 +48,7 @@ impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for
match e.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = Provide::provide(state, &mut e)? {
if let Some(command) = Take::provide(state, &mut e)? {
return Ok(Some(command))
}
}
@ -69,7 +69,7 @@ pub type InputCondition<'k, S> = Box<dyn Fn(&S)->Usually<bool> + Send + Sync + '
/// which may be made available subject to given conditions.
pub struct InputMap<'k,
S,
C: Command<S> + Provide<'k, S>,
C: Command<S> + Take<'k, S>,
I: DslInput,
M: KeyMap<'k, S, C, I>
> {
@ -79,7 +79,7 @@ pub struct InputMap<'k,
impl<'k,
S,
C: Command<S> + Provide<'k, S>,
C: Command<S> + Take<'k, S>,
I: DslInput,
M: KeyMap<'k, S, C, I>
> Default for InputMap<'k, S, C, I, M>{
@ -88,7 +88,7 @@ impl<'k,
}
}
impl<'k, S, C: Command<S> + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
impl<'k, S, C: Command<S> + Take<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
InputMap<'k, S, C, I, M> {
pub fn new (keymap: M) -> Self {
Self::default().layer(keymap)
@ -111,7 +111,7 @@ InputMap<'k, S, C, I, M> {
}
}
impl<'k, S, C: Command<S> + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
impl<'k, S, C: Command<S> + Take<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
std::fmt::Debug for InputMap<'k, S, C, I, M> {
fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "[InputMap: {} layer(s)]", self.layers.len())
@ -121,7 +121,7 @@ std::fmt::Debug for InputMap<'k, S, C, I, M> {
/// An [InputMap] can be a [KeyMap].
impl<'k,
S,
C: Command<S> + Provide<'k, S>,
C: Command<S> + Take<'k, S>,
I: DslInput,
M: KeyMap<'k, S, C, I>
> KeyMap<'k, S, C, I> for InputMap<'k, S, C, I, M> {

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;

View file

@ -84,7 +84,7 @@ impl ToTokens for CommandDef {
let mut out = TokenStream2::new();
for (arg, ty) in arm.args() {
write_quote_to(&mut out, quote! {
#arg: Provide::provide_or_fail(self, words)?,
#arg: Take::provide_or_fail(self, words)?,
});
}
out
@ -149,7 +149,7 @@ impl ToTokens for CommandDef {
}
}
/// Generated by [tengri_proc::command].
impl<'n> ::tengri::dsl::Provide<'n, #state> for #command_enum {
impl<'n> ::tengri::dsl::Take<'n, #state> for #command_enum {
fn provide <'source> (
state: &#state,
words: &mut ::tengri::dsl::TokenIter<'source>

View file

@ -85,7 +85,7 @@ impl ToTokens for ExposeImpl {
let values = variants.iter().map(ExposeArm::from);
write_quote_to(out, quote! {
/// Generated by [tengri_proc::expose].
impl<'n> ::tengri::dsl::Provide<'n, #state> for #t {
impl<'n> ::tengri::dsl::Take<'n, #state> for #t {
fn provide <'source> (
state: &#state,
words: &mut ::tengri::dsl::TokenIter<'source>

View file

@ -46,7 +46,7 @@ impl ToTokens for ViewDef {
let builtins: Vec<_> = builtins_with_types()
.iter()
.map(|ty|write_quote(quote! {
let value: Option<#ty> = Receive::<#ty>::receive(self, &mut exp.clone())?;
let value: Option<#ty> = Give::<#ty>::give(self, &mut exp.clone())?;
if let Some(value) = value {
return Ok(Some(value.boxed()))
}
@ -65,8 +65,8 @@ impl ToTokens for ViewDef {
/// Gives [#self_ty] the ability to construct the [Render]able
/// which might corresponds to a given [TokenStream],
/// while taking [#self_ty]'s state into consideration.
impl ::tengri::dsl::Receive<Box<dyn Render<#output>>> for #self_ty {
fn receive <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>)
impl ::tengri::dsl::Give<Box<dyn Render<#output>>> for #self_ty {
fn give <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>)
-> Perhaps<Box<dyn Render<#output>>>
{
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {