wip: finally, informative type errors from the macro
Some checks failed
/ build (push) Has been cancelled

fixin mixin
This commit is contained in:
🪞👃🪞 2025-05-21 15:54:25 +03:00
parent abc87d3234
commit 583660c330
10 changed files with 152 additions and 205 deletions

View file

@ -1,114 +1,70 @@
use crate::*; use crate::*;
pub trait Dsl<Type> { pub trait Receive<Type> {
fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type>; fn receive <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type>;
fn take_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> ( fn receive_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
&self, words: &mut TokenIter<'source>, error: F &self, words: &mut TokenIter<'source>, error: F
) -> Usually<Type> { ) -> Usually<Type> {
if let Some(value) = Dsl::<Type>::take(self, words)? { if let Some(value) = Receive::<Type>::receive(self, words)? {
Ok(value) Ok(value)
} else { } else {
Result::Err(format!("{}: {:?}", error().into(), words.peek().map(|x|x.value)).into()) Result::Err(format!("receive: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
} }
} }
} }
impl<Type: Namespace<State>, State> Dsl<Type> for State { pub trait Provide<'n, State>: Sized + 'n {
fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> { fn provide <'source> (state: &State, words: &mut TokenIter<'source>)
Namespace::take_from(self, words)
}
}
pub trait Namespace<State>: Sized {
fn take_from <'source> (state: &State, words: &mut TokenIter<'source>)
-> Perhaps<Self>; -> Perhaps<Self>;
fn take_from_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> ( fn provide_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
state: &State, words: &mut TokenIter<'source>, error: F state: &State, words: &mut TokenIter<'source>, error: F
) -> Usually<Self> { ) -> Usually<Self> {
if let Some(value) = Namespace::<State>::take_from(state, words)? { if let Some(value) = Provide::<State>::provide(state, words)? {
Ok(value) Ok(value)
} else { } else {
Result::Err(format!("{}: {:?}", error().into(), words.peek().map(|x|x.value)).into()) Result::Err(format!("provide: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
} }
} }
} }
//impl<State: Dsl<T>, T> Namespace<State> for T { /// Implement the [Receive] trait, which boils down to
//fn take_from <'state, 'source: 'state> (state: &'state State, token: &mut TokenIter<'source>)
//-> Perhaps<Self>
//{
//Dsl::take(state, token)
//}
//}
/// Implement the [Dsl] trait, which boils down to
/// specifying two types and providing an expression. /// specifying two types and providing an expression.
#[macro_export] macro_rules! from_dsl { #[macro_export] macro_rules! from_dsl {
(@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => { (@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
impl<State: Dsl<A>, A> Namespace<State> for $T { impl<'n, State, A: Provide<'n, State>> Provide<'n, State> for $T {
fn take_from <'source> ( fn provide <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
$state: &State,
$words: &mut TokenIter<'source>,
) -> Perhaps<$T> {
$expr $expr
} }
} }
}; };
(@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => { (@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
impl<State: Dsl<A> + Dsl<B>, A, B> Namespace<State> for $T { impl<'n, State, A: Provide<'n, State>, B: Provide<'n, State>> Provide<'n, State> for $T {
fn take_from <'source> ( fn provide <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
$state: &State,
$words: &mut TokenIter<'source>,
) -> Perhaps<$T> {
$expr $expr
} }
} }
}; };
($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => { ($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => {
impl Namespace<$S> for $T { impl<'n> Provide<'n, $S> for $T {
fn take_from <'source> ( fn provide <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
$state: &$S,
$words: &mut TokenIter<'source>,
) -> Perhaps<$T> {
$expr $expr
} }
} }
}; };
} }
///// Maps a sequencer of EDN tokens to parameters of supported types // auto impl graveyard:
///// for a given context.
//pub trait Dsl<Value>: Sized { //impl<'n, State: Receive<Type>, Type: 'n> Provide<'n, State> for Type {
//fn take <'state, 'source> (&'state self, _: &mut TokenIter<'source>) -> Perhaps<Value> { //fn provide <'source> (state: &State, words: &mut TokenIter<'source>)
//unimplemented!() //-> Perhaps<Self>
//} //{
//fn take_or_fail <'state, 'source> ( //state.take(words)
//&'state self,
//token: &mut TokenIter<'source>,
//error: impl Into<Box<dyn std::error::Error>>
//) -> Usually<Value> {
//if let Some(value) = Dsl::<Value>::take(self, token)? {
//Ok(value)
//} else {
//Result::Err(error.into())
//}
//} //}
//} //}
//impl<T: Dsl<U>, U> Dsl<U> for &T { //impl<'n, Type: Provide<'n, State>, State> Receive<Type> for State {
//fn take <'state, 'source> (&'state self, iter: &mut TokenIter<'source>) -> Perhaps<U> { //fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
//(*self).take(iter) //Type::provide(self, words)
//} //}
//} //}
//impl<T: Dsl<U>, U> Dsl<U> for Option<T> {
//fn take <'state, 'source> (&'state self, iter: &mut TokenIter<'source>) -> Perhaps<U> {
//Ok(self.as_ref().map(|s|s.take(iter)).transpose()?.flatten())
//}
//}
//impl<'state, X, Y> Dsl<X> for Y where Y: Dsl<'state, X> {
//}
//impl<T, U: Dsl<T>> Dsl<U> for T {
//}

View file

@ -5,15 +5,13 @@ use std::marker::PhantomData;
pub trait DslInput: Input { fn matches_dsl (&self, token: &str) -> bool; } pub trait DslInput: Input { fn matches_dsl (&self, token: &str) -> bool; }
/// A pre-configured mapping of input events to commands. /// A pre-configured mapping of input events to commands.
pub trait KeyMap<'source, S, C: Namespace<S> + Command<S>, I: DslInput> { pub trait KeyMap<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> {
/// Try to find a command that matches the current input event. /// Try to find a command that matches the current input event.
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C>; fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C>;
} }
/// A [SourceIter] can be a [KeyMap]. /// A [SourceIter] can be a [KeyMap].
impl<'source, S, C: Namespace<S> + Command<S>, I: DslInput> impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for SourceIter<'k> {
KeyMap<'source, S, C, I>
for SourceIter<'source> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> { fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
let mut iter = self.clone(); let mut iter = self.clone();
while let Some((token, rest)) = iter.next() { while let Some((token, rest)) = iter.next() {
@ -24,7 +22,7 @@ for SourceIter<'source> {
match exp_iter.next() { match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => { Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) { if input.matches_dsl(binding) {
if let Some(command) = Namespace::take_from(state, &mut exp_iter)? { if let Some(command) = Provide::provide(state, &mut exp_iter)? {
return Ok(Some(command)) return Ok(Some(command))
} }
} }
@ -40,9 +38,7 @@ for SourceIter<'source> {
} }
/// A [TokenIter] can be a [KeyMap]. /// A [TokenIter] can be a [KeyMap].
impl<'source, S, C: Namespace<S> + Command<S>, I: DslInput> impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for TokenIter<'k> {
KeyMap<'source, S, C, I>
for TokenIter<'source> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> { fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
let mut iter = self.clone(); let mut iter = self.clone();
while let Some(next) = iter.next() { while let Some(next) = iter.next() {
@ -52,7 +48,7 @@ for TokenIter<'source> {
match e.next() { match e.next() {
Some(Token { value: Value::Sym(binding), .. }) => { Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) { if input.matches_dsl(binding) {
if let Some(command) = Namespace::take_from(state, &mut e)? { if let Some(command) = Provide::provide(state, &mut e)? {
return Ok(Some(command)) return Ok(Some(command))
} }
} }
@ -67,38 +63,33 @@ for TokenIter<'source> {
} }
} }
pub type InputCondition<'source, S> = pub type InputCondition<'k, S> = Box<dyn Fn(&S)->Usually<bool> + Send + Sync + 'k>;
Box<dyn Fn(&S)->Usually<bool> + Send + Sync + 'source>;
/// A collection of pre-configured mappings of input events to commands, /// A collection of pre-configured mappings of input events to commands,
/// which may be made available subject to given conditions. /// which may be made available subject to given conditions.
pub struct InputMap<'source, pub struct InputMap<'k,
S, S,
C: Command<S> + Namespace<S>, C: Command<S> + Provide<'k, S>,
I: DslInput, I: DslInput,
M: KeyMap<'source, S, C, I> M: KeyMap<'k, S, C, I>
> { > {
__: PhantomData<&'source (S, C, I)>, __: PhantomData<&'k (S, C, I)>,
pub layers: Vec<(InputCondition<'source, S>, M)>, pub layers: Vec<(InputCondition<'k, S>, M)>,
} }
impl<'source, impl<'k,
S, S,
C: Command<S> + Namespace<S>, C: Command<S> + Provide<'k, S>,
I: DslInput, I: DslInput,
M: KeyMap<'source, S, C, I> M: KeyMap<'k, S, C, I>
> Default for InputMap<'source, S, C, I, M>{ > Default for InputMap<'k, S, C, I, M>{
fn default () -> Self { fn default () -> Self {
Self { __: PhantomData, layers: vec![] } Self { __: PhantomData, layers: vec![] }
} }
} }
impl<'source, impl<'k, S, C: Command<S> + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
S, InputMap<'k, S, C, I, M> {
C: Command<S> + Namespace<S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> InputMap<'source, S, C, I, M> {
pub fn new (keymap: M) -> Self { pub fn new (keymap: M) -> Self {
Self::default().layer(keymap) Self::default().layer(keymap)
} }
@ -110,34 +101,30 @@ impl<'source,
self.add_layer_if(Box::new(|_|Ok(true)), keymap); self.add_layer_if(Box::new(|_|Ok(true)), keymap);
self self
} }
pub fn layer_if (mut self, condition: InputCondition<'source, S>, keymap: M) -> Self { pub fn layer_if (mut self, condition: InputCondition<'k, S>, keymap: M) -> Self {
self.add_layer_if(condition, keymap); self.add_layer_if(condition, keymap);
self self
} }
pub fn add_layer_if (&mut self, condition: InputCondition<'source, S>, keymap: M) -> &mut Self { pub fn add_layer_if (&mut self, condition: InputCondition<'k, S>, keymap: M) -> &mut Self {
self.layers.push((Box::new(condition), keymap)); self.layers.push((Box::new(condition), keymap));
self self
} }
} }
impl<'source, impl<'k, S, C: Command<S> + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
S, std::fmt::Debug for InputMap<'k, S, C, I, M> {
C: Command<S> + Namespace<S>,
I: DslInput,
M: KeyMap<'source, S, C, I>
> std::fmt::Debug for InputMap<'source, S, C, I, M> {
fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "[InputMap: {} layer(s)]", self.layers.len()) write!(f, "[InputMap: {} layer(s)]", self.layers.len())
} }
} }
/// An [InputMap] can be a [KeyMap]. /// An [InputMap] can be a [KeyMap].
impl<'source, impl<'k,
S, S,
C: Command<S> + Namespace<S>, C: Command<S> + Provide<'k, S>,
I: DslInput, I: DslInput,
M: KeyMap<'source, S, C, I> M: KeyMap<'k, S, C, I>
> KeyMap<'source, S, C, I> for InputMap<'source, S, C, I, M> { > KeyMap<'k, S, C, I> for InputMap<'k, S, C, I, M> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> { fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
for (condition, keymap) in self.layers.iter() { for (condition, keymap) in self.layers.iter() {
if !condition(state)? { if !condition(state)? {

View file

@ -36,33 +36,36 @@ pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<A>(Alignment, A); pub struct Align<A>(Alignment, A);
#[cfg(feature = "dsl")] #[cfg(feature = "dsl")]
from_dsl!(@a: Align<A>: |state, iter|if let Some(Token { value: Value::Key(key), .. }) = iter.peek() { impl<'n, State: Receive<A>, A: 'n> Provide<'n, State> for Align<A> {
match key { fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
"align/c"|"align/x"|"align/y"| if let Some(Token { value: Value::Key(key), .. }) = words.peek() {
"align/n"|"align/s"|"align/e"|"align/w"| match key {
"align/nw"|"align/sw"|"align/ne"|"align/se" => { "align/c"|"align/x"|"align/y"|
let _ = iter.next().unwrap(); "align/n"|"align/s"|"align/e"|"align/w"|
let content = state.take_or_fail(&mut iter.clone(), ||"expected content")?; "align/nw"|"align/sw"|"align/ne"|"align/se" => {
return Ok(Some(match key { let _ = words.next().unwrap();
"align/c" => Self::c(content), let content = state.receive_or_fail(&mut words.clone(), ||"expected content")?;
"align/x" => Self::x(content), return Ok(Some(match key {
"align/y" => Self::y(content), "align/c" => Self::c(content),
"align/n" => Self::n(content), "align/x" => Self::x(content),
"align/s" => Self::s(content), "align/y" => Self::y(content),
"align/e" => Self::e(content), "align/n" => Self::n(content),
"align/w" => Self::w(content), "align/s" => Self::s(content),
"align/nw" => Self::nw(content), "align/e" => Self::e(content),
"align/ne" => Self::ne(content), "align/w" => Self::w(content),
"align/sw" => Self::sw(content), "align/nw" => Self::nw(content),
"align/se" => Self::se(content), "align/ne" => Self::ne(content),
_ => unreachable!() "align/sw" => Self::sw(content),
})) "align/se" => Self::se(content),
}, _ => unreachable!()
_ => return Ok(None) }))
},
_ => {}
}
}
Ok(None)
} }
} else { }
Ok(None)
});
impl<A> Align<A> { impl<A> Align<A> {
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) } #[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }

View file

@ -21,24 +21,28 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
} }
} }
#[cfg(feature = "dsl")] #[cfg(feature = "dsl")]
impl<State: Dsl<A> + Dsl<B> + std::fmt::Debug, A, B> Namespace<State> for Bsp<A, B> { impl<'n, State: Receive<A> + Receive<B>, A: 'n, B: 'n> Provide<'n, State> for Bsp<A, B> {
fn take_from <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> { fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
Ok(if let Some(Token { Ok(if let Some(Token {
value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"), value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"),
.. ..
}) = words.peek() { }) = words.peek() {
let base = words.clone(); if let Value::Key(key) = words.next().unwrap().value() {
let a: A = state.take_or_fail(words, ||"expected content 1")?; let base = words.clone();
let b: B = state.take_or_fail(words, ||"expected content 2")?; let a: A = state.receive_or_fail(words, ||"bsp: expected content 1")?;
return Ok(Some(match words.next() { let b: B = state.receive_or_fail(words, ||"bsp: expected content 2")?;
Some(Token { value: Value::Key("bsp/n"), .. }) => Self::n(a, b), return Ok(Some(match key {
Some(Token { value: Value::Key("bsp/s"), .. }) => Self::s(a, b), "bsp/n" => Self::n(a, b),
Some(Token { value: Value::Key("bsp/e"), .. }) => Self::e(a, b), "bsp/s" => Self::s(a, b),
Some(Token { value: Value::Key("bsp/w"), .. }) => Self::w(a, b), "bsp/e" => Self::e(a, b),
Some(Token { value: Value::Key("bsp/a"), .. }) => Self::a(a, b), "bsp/w" => Self::w(a, b),
Some(Token { value: Value::Key("bsp/b"), .. }) => Self::b(a, b), "bsp/a" => Self::a(a, b),
_ => unreachable!(), "bsp/b" => Self::b(a, b),
})) _ => unreachable!(),
}))
} else {
unreachable!()
}
} else { } else {
None None
}) })

View file

@ -9,18 +9,15 @@ impl<A, B> Either<A, B> {
} }
} }
#[cfg(feature = "dsl")] #[cfg(feature = "dsl")]
impl<A, B, T: Dsl<bool> + Dsl<A> + Dsl<B>> Namespace<T> for Either<A, B> { impl<'n, State: Receive<bool> + Receive<A> + Receive<B>, A: 'n, B: 'n> Provide<'n, State> for Either<A, B> {
fn take_from <'source> ( fn provide <'source> (state: &State, token: &mut TokenIter<'source>) -> Perhaps<Self> {
state: &T,
token: &mut TokenIter<'source>
) -> Perhaps<Self> {
if let Some(Token { value: Value::Key("either"), .. }) = token.peek() { if let Some(Token { value: Value::Key("either"), .. }) = token.peek() {
let base = token.clone(); let base = token.clone();
let _ = token.next().unwrap(); let _ = token.next().unwrap();
return Ok(Some(Self( return Ok(Some(Self(
state.take_or_fail(token, ||"either: no condition")?, state.receive_or_fail(token, ||"either: no condition")?,
state.take_or_fail(token, ||"either: no content 1")?, state.receive_or_fail(token, ||"either: no content 1")?,
state.take_or_fail(token, ||"either: no content 2")?, state.receive_or_fail(token, ||"either: no content 2")?,
))) )))
} }
Ok(None) Ok(None)

View file

@ -33,19 +33,19 @@ macro_rules! transform_xy {
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) } #[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
} }
#[cfg(feature = "dsl")] #[cfg(feature = "dsl")]
impl<A, T: Dsl<A>> Namespace<T> for $Enum<A> { impl<'n, A: 'n, T: Receive<A>> Provide<'n, T> for $Enum<A> {
fn take_from <'source> ( fn provide <'source> (state: &T, token: &mut TokenIter<'source>)
state: &T, token: &mut TokenIter<'source> -> Perhaps<Self>
) -> Perhaps<Self> { {
if let Some(Token { value: Value::Key(k), .. }) = token.peek() { if let Some(Token { value: Value::Key(k), .. }) = token.peek() {
let mut base = token.clone(); let mut base = token.clone();
return Ok(Some(match token.next() { return Ok(Some(match token.next() {
Some(Token{value:Value::Key($x),..}) => Some(Token{value:Value::Key($x),..}) =>
Self::x(state.take_or_fail(token, ||"x: no content")?), Self::x(state.receive_or_fail(token, ||"x: no content")?),
Some(Token{value:Value::Key($y),..}) => Some(Token{value:Value::Key($y),..}) =>
Self::y(state.take_or_fail(token, ||"y: no content")?), Self::y(state.receive_or_fail(token, ||"y: no content")?),
Some(Token{value:Value::Key($xy),..}) => Some(Token{value:Value::Key($xy),..}) =>
Self::xy(state.take_or_fail(token, ||"xy: no content")?), Self::xy(state.receive_or_fail(token, ||"xy: no content")?),
_ => unreachable!() _ => unreachable!()
})) }))
} }
@ -79,25 +79,25 @@ macro_rules! transform_xy_unit {
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) } #[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
} }
#[cfg(feature = "dsl")] #[cfg(feature = "dsl")]
impl<A, U: Coordinate, T: Dsl<A> + Dsl<U>> Namespace<T> for $Enum<U, A> { impl<'n, A: 'n, U: Coordinate + 'n, T: Receive<A> + Receive<U>> Provide<'n, T> for $Enum<U, A> {
fn take_from <'source> ( fn provide <'source> (
state: &T, token: &mut TokenIter<'source> state: &T, token: &mut TokenIter<'source>
) -> Perhaps<Self> { ) -> Perhaps<Self> {
Ok(if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = token.peek() { Ok(if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = token.peek() {
let mut base = token.clone(); let mut base = token.clone();
Some(match token.next() { Some(match token.next() {
Some(Token { value: Value::Key($x), .. }) => Self::x( Some(Token { value: Value::Key($x), .. }) => Self::x(
state.take_or_fail(token, ||"x: no unit")?, state.receive_or_fail(token, ||"x: no unit")?,
state.take_or_fail(token, ||"x: no content")?, state.receive_or_fail(token, ||"x: no content")?,
), ),
Some(Token { value: Value::Key($y), .. }) => Self::y( Some(Token { value: Value::Key($y), .. }) => Self::y(
state.take_or_fail(token, ||"y: no unit")?, state.receive_or_fail(token, ||"y: no unit")?,
state.take_or_fail(token, ||"y: no content")?, state.receive_or_fail(token, ||"y: no content")?,
), ),
Some(Token { value: Value::Key($x), .. }) => Self::xy( Some(Token { value: Value::Key($x), .. }) => Self::xy(
state.take_or_fail(token, ||"xy: no unit x")?, state.receive_or_fail(token, ||"xy: no unit x")?,
state.take_or_fail(token, ||"xy: no unit y")?, state.receive_or_fail(token, ||"xy: no unit y")?,
state.take_or_fail(token, ||"xy: no content")? state.receive_or_fail(token, ||"xy: no content")?
), ),
_ => unreachable!(), _ => unreachable!(),
}) })

View file

@ -9,19 +9,17 @@ impl<A> When<A> {
} }
} }
#[cfg(feature = "dsl")] #[cfg(feature = "dsl")]
impl<A, T: Dsl<bool> + Dsl<A>> Namespace<T> for When<A> { impl<'n, State: Receive<bool> + Receive<A>, A: 'n> Provide<'n, State> for When<A> {
fn take_from <'source> ( fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
state: &T,
token: &mut TokenIter<'source>
) -> Perhaps<Self> {
Ok(if let Some(Token { Ok(if let Some(Token {
value: Value::Key("when"), value: Value::Key("when"),
.. ..
}) = token.peek() { }) = words.peek() {
let base = token.clone(); let _ = words.next();
let base = words.clone();
return Ok(Some(Self( return Ok(Some(Self(
state.take_or_fail(token, ||"cond: no condition")?, state.receive_or_fail(words, ||"cond: no condition")?,
state.take_or_fail(token, ||"cond: no content")?, state.receive_or_fail(words, ||"cond: no content")?,
))) )))
} else { } else {
None None

View file

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

View file

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

View file

@ -46,7 +46,7 @@ impl ToTokens for ViewDef {
let builtins: Vec<_> = builtins_with_types() let builtins: Vec<_> = builtins_with_types()
.iter() .iter()
.map(|ty|write_quote(quote! { .map(|ty|write_quote(quote! {
let value: Option<#ty> = Dsl::take(state, &mut exp.clone())?; let value: Option<#ty> = Receive::<#ty>::receive(self, &mut exp.clone())?;
if let Some(value) = value { if let Some(value) = value {
return Ok(Some(value.boxed())) return Ok(Some(value.boxed()))
} }
@ -55,30 +55,20 @@ impl ToTokens for ViewDef {
let exposed: Vec<_> = exposed let exposed: Vec<_> = exposed
.iter() .iter()
.map(|(key, value)|write_quote(quote! { .map(|(key, value)|write_quote(quote! {
#key => Some(#self_ty::#value(state).boxed()), #key => {
Some(Box::new(Thunk::new(move||#self_ty::#value(self))))//Box::new("todo"))
},
})).collect(); })).collect();
write_quote_to(out, quote! { write_quote_to(out, quote! {
#block
/// Generated by [tengri_proc].
///
/// Delegates the rendering of [#self_ty] to the [#self_ty::view} method,
/// which you will need to implement, e.g. passing a [TokenIter]
/// containing a layout and keybindings config from user dirs.
impl ::tengri::output::Content<#output> for #self_ty {
fn content (&self) -> impl Render<#output> {
#self_ty::view(self)
}
}
/// Generated by [tengri_proc]. /// Generated by [tengri_proc].
/// ///
/// Gives [#self_ty] the ability to construct the [Render]able /// Gives [#self_ty] the ability to construct the [Render]able
/// which might corresponds to a given [TokenStream], /// which might corresponds to a given [TokenStream],
/// while taking [#self_ty]'s state into consideration. /// while taking [#self_ty]'s state into consideration.
impl ::tengri::dsl::Namespace<#self_ty> for Box<dyn Render<#output> + '_> { impl ::tengri::dsl::Receive<Box<dyn Render<#output>>> for #self_ty {
fn take_from <'source> ( fn receive <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>)
state: &#self_ty, -> Perhaps<Box<dyn Render<#output>>>
words: &mut ::tengri::dsl::TokenIter<'source> {
) -> Perhaps<Self> {
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() { Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
match value { match value {
// Expressions are handled by built-in functions // Expressions are handled by built-in functions
@ -100,6 +90,18 @@ impl ToTokens for ViewDef {
}) })
} }
} }
/// Generated by [tengri_proc].
///
/// Delegates the rendering of [#self_ty] to the [#self_ty::view} method,
/// which you will need to implement, e.g. passing a [TokenIter]
/// containing a layout and keybindings config from user dirs.
impl ::tengri::output::Content<#output> for #self_ty {
fn content (&self) -> impl Render<#output> + '_ {
#self_ty::view(self)
}
}
// Original user-provided implementation:
#block
}) })
} }
} }