mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
wip: finally, informative type errors from the macro
Some checks failed
/ build (push) Has been cancelled
Some checks failed
/ build (push) Has been cancelled
fixin mixin
This commit is contained in:
parent
abc87d3234
commit
583660c330
10 changed files with 152 additions and 205 deletions
|
|
@ -1,114 +1,70 @@
|
|||
use crate::*;
|
||||
|
||||
pub trait Dsl<Type> {
|
||||
fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type>;
|
||||
fn take_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||
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> (
|
||||
&self, words: &mut TokenIter<'source>, error: F
|
||||
) -> Usually<Type> {
|
||||
if let Some(value) = Dsl::<Type>::take(self, words)? {
|
||||
if let Some(value) = Receive::<Type>::receive(self, words)? {
|
||||
Ok(value)
|
||||
} 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 {
|
||||
fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
|
||||
Namespace::take_from(self, words)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Namespace<State>: Sized {
|
||||
fn take_from <'source> (state: &State, words: &mut TokenIter<'source>)
|
||||
pub trait Provide<'n, State>: Sized + 'n {
|
||||
fn provide <'source> (state: &State, words: &mut TokenIter<'source>)
|
||||
-> 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
|
||||
) -> Usually<Self> {
|
||||
if let Some(value) = Namespace::<State>::take_from(state, words)? {
|
||||
if let Some(value) = Provide::<State>::provide(state, words)? {
|
||||
Ok(value)
|
||||
} 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 {
|
||||
//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
|
||||
/// Implement the [Receive] 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<State: Dsl<A>, A> Namespace<State> for $T {
|
||||
fn take_from <'source> (
|
||||
$state: &State,
|
||||
$words: &mut TokenIter<'source>,
|
||||
) -> Perhaps<$T> {
|
||||
impl<'n, State, A: Provide<'n, State>> Provide<'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<State: Dsl<A> + Dsl<B>, A, B> Namespace<State> for $T {
|
||||
fn take_from <'source> (
|
||||
$state: &State,
|
||||
$words: &mut TokenIter<'source>,
|
||||
) -> Perhaps<$T> {
|
||||
impl<'n, State, A: Provide<'n, State>, B: Provide<'n, State>> Provide<'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 Namespace<$S> for $T {
|
||||
fn take_from <'source> (
|
||||
$state: &$S,
|
||||
$words: &mut TokenIter<'source>,
|
||||
) -> Perhaps<$T> {
|
||||
impl<'n> Provide<'n, $S> for $T {
|
||||
fn provide <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||
$expr
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
///// Maps a sequencer of EDN tokens to parameters of supported types
|
||||
///// for a given context.
|
||||
//pub trait Dsl<Value>: Sized {
|
||||
//fn take <'state, 'source> (&'state self, _: &mut TokenIter<'source>) -> Perhaps<Value> {
|
||||
//unimplemented!()
|
||||
//}
|
||||
//fn take_or_fail <'state, 'source> (
|
||||
//&'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())
|
||||
//}
|
||||
// auto impl graveyard:
|
||||
|
||||
//impl<'n, State: Receive<Type>, Type: 'n> Provide<'n, State> for Type {
|
||||
//fn provide <'source> (state: &State, words: &mut TokenIter<'source>)
|
||||
//-> Perhaps<Self>
|
||||
//{
|
||||
//state.take(words)
|
||||
//}
|
||||
//}
|
||||
|
||||
//impl<T: Dsl<U>, U> Dsl<U> for &T {
|
||||
//fn take <'state, 'source> (&'state self, iter: &mut TokenIter<'source>) -> Perhaps<U> {
|
||||
//(*self).take(iter)
|
||||
//impl<'n, Type: Provide<'n, State>, State> Receive<Type> for State {
|
||||
//fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
|
||||
//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 {
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -5,15 +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<'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.
|
||||
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C>;
|
||||
}
|
||||
|
||||
/// A [SourceIter] can be a [KeyMap].
|
||||
impl<'source, S, C: Namespace<S> + Command<S>, I: DslInput>
|
||||
KeyMap<'source, S, C, I>
|
||||
for SourceIter<'source> {
|
||||
impl<'k, S, C: Provide<'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() {
|
||||
|
|
@ -24,7 +22,7 @@ for SourceIter<'source> {
|
|||
match exp_iter.next() {
|
||||
Some(Token { value: Value::Sym(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))
|
||||
}
|
||||
}
|
||||
|
|
@ -40,9 +38,7 @@ for SourceIter<'source> {
|
|||
}
|
||||
|
||||
/// A [TokenIter] can be a [KeyMap].
|
||||
impl<'source, S, C: Namespace<S> + Command<S>, I: DslInput>
|
||||
KeyMap<'source, S, C, I>
|
||||
for TokenIter<'source> {
|
||||
impl<'k, S, C: Provide<'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() {
|
||||
|
|
@ -52,7 +48,7 @@ for TokenIter<'source> {
|
|||
match e.next() {
|
||||
Some(Token { value: Value::Sym(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))
|
||||
}
|
||||
}
|
||||
|
|
@ -67,38 +63,33 @@ for TokenIter<'source> {
|
|||
}
|
||||
}
|
||||
|
||||
pub type InputCondition<'source, S> =
|
||||
Box<dyn Fn(&S)->Usually<bool> + Send + Sync + 'source>;
|
||||
pub type InputCondition<'k, S> = Box<dyn Fn(&S)->Usually<bool> + Send + Sync + 'k>;
|
||||
|
||||
/// A collection of pre-configured mappings of input events to commands,
|
||||
/// which may be made available subject to given conditions.
|
||||
pub struct InputMap<'source,
|
||||
pub struct InputMap<'k,
|
||||
S,
|
||||
C: Command<S> + Namespace<S>,
|
||||
C: Command<S> + Provide<'k, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'source, S, C, I>
|
||||
M: KeyMap<'k, S, C, I>
|
||||
> {
|
||||
__: PhantomData<&'source (S, C, I)>,
|
||||
pub layers: Vec<(InputCondition<'source, S>, M)>,
|
||||
__: PhantomData<&'k (S, C, I)>,
|
||||
pub layers: Vec<(InputCondition<'k, S>, M)>,
|
||||
}
|
||||
|
||||
impl<'source,
|
||||
impl<'k,
|
||||
S,
|
||||
C: Command<S> + Namespace<S>,
|
||||
C: Command<S> + Provide<'k, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'source, S, C, I>
|
||||
> Default for InputMap<'source, S, C, I, M>{
|
||||
M: KeyMap<'k, S, C, I>
|
||||
> Default for InputMap<'k, S, C, I, M>{
|
||||
fn default () -> Self {
|
||||
Self { __: PhantomData, layers: vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source,
|
||||
S,
|
||||
C: Command<S> + Namespace<S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'source, S, C, I>
|
||||
> InputMap<'source, S, C, I, M> {
|
||||
impl<'k, S, C: Command<S> + Provide<'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)
|
||||
}
|
||||
|
|
@ -110,34 +101,30 @@ impl<'source,
|
|||
self.add_layer_if(Box::new(|_|Ok(true)), keymap);
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source,
|
||||
S,
|
||||
C: Command<S> + Namespace<S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'source, S, C, I>
|
||||
> std::fmt::Debug for InputMap<'source, S, C, I, M> {
|
||||
impl<'k, S, C: Command<S> + Provide<'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())
|
||||
}
|
||||
}
|
||||
|
||||
/// An [InputMap] can be a [KeyMap].
|
||||
impl<'source,
|
||||
impl<'k,
|
||||
S,
|
||||
C: Command<S> + Namespace<S>,
|
||||
C: Command<S> + Provide<'k, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'source, S, C, I>
|
||||
> KeyMap<'source, S, C, I> for InputMap<'source, S, C, I, M> {
|
||||
M: KeyMap<'k, S, C, I>
|
||||
> KeyMap<'k, S, C, I> for InputMap<'k, S, C, I, M> {
|
||||
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
|
||||
for (condition, keymap) in self.layers.iter() {
|
||||
if !condition(state)? {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
#[cfg(feature = "dsl")]
|
||||
from_dsl!(@a: Align<A>: |state, iter|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 = state.take_or_fail(&mut iter.clone(), ||"expected content")?;
|
||||
return Ok(Some(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)
|
||||
impl<'n, State: Receive<A>, A: 'n> Provide<'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 {
|
||||
"align/c"|"align/x"|"align/y"|
|
||||
"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")?;
|
||||
return Ok(Some(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!()
|
||||
}))
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
});
|
||||
}
|
||||
|
||||
impl<A> Align<A> {
|
||||
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }
|
||||
|
|
|
|||
|
|
@ -21,24 +21,28 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
|
|||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<State: Dsl<A> + Dsl<B> + std::fmt::Debug, A, B> Namespace<State> for Bsp<A, B> {
|
||||
fn take_from <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
impl<'n, State: Receive<A> + Receive<B>, A: 'n, B: 'n> Provide<'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"),
|
||||
..
|
||||
}) = words.peek() {
|
||||
let base = words.clone();
|
||||
let a: A = state.take_or_fail(words, ||"expected content 1")?;
|
||||
let b: B = state.take_or_fail(words, ||"expected content 2")?;
|
||||
return Ok(Some(match words.next() {
|
||||
Some(Token { value: Value::Key("bsp/n"), .. }) => Self::n(a, b),
|
||||
Some(Token { value: Value::Key("bsp/s"), .. }) => Self::s(a, b),
|
||||
Some(Token { value: Value::Key("bsp/e"), .. }) => Self::e(a, b),
|
||||
Some(Token { value: Value::Key("bsp/w"), .. }) => Self::w(a, b),
|
||||
Some(Token { value: Value::Key("bsp/a"), .. }) => Self::a(a, b),
|
||||
Some(Token { value: Value::Key("bsp/b"), .. }) => Self::b(a, b),
|
||||
_ => unreachable!(),
|
||||
}))
|
||||
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")?;
|
||||
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
|
||||
})
|
||||
|
|
|
|||
|
|
@ -9,18 +9,15 @@ impl<A, B> Either<A, B> {
|
|||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<A, B, T: Dsl<bool> + Dsl<A> + Dsl<B>> Namespace<T> for Either<A, B> {
|
||||
fn take_from <'source> (
|
||||
state: &T,
|
||||
token: &mut TokenIter<'source>
|
||||
) -> Perhaps<Self> {
|
||||
impl<'n, State: Receive<bool> + Receive<A> + Receive<B>, A: 'n, B: 'n> Provide<'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.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")?,
|
||||
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")?,
|
||||
)))
|
||||
}
|
||||
Ok(None)
|
||||
|
|
|
|||
|
|
@ -33,19 +33,19 @@ macro_rules! transform_xy {
|
|||
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<A, T: Dsl<A>> Namespace<T> for $Enum<A> {
|
||||
fn take_from <'source> (
|
||||
state: &T, token: &mut TokenIter<'source>
|
||||
) -> Perhaps<Self> {
|
||||
impl<'n, A: 'n, T: Receive<A>> Provide<'n, T> for $Enum<A> {
|
||||
fn provide <'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.take_or_fail(token, ||"x: no content")?),
|
||||
Self::x(state.receive_or_fail(token, ||"x: no content")?),
|
||||
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),..}) =>
|
||||
Self::xy(state.take_or_fail(token, ||"xy: no content")?),
|
||||
Self::xy(state.receive_or_fail(token, ||"xy: no content")?),
|
||||
_ => 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) }
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<A, U: Coordinate, T: Dsl<A> + Dsl<U>> Namespace<T> for $Enum<U, A> {
|
||||
fn take_from <'source> (
|
||||
impl<'n, A: 'n, U: Coordinate + 'n, T: Receive<A> + Receive<U>> Provide<'n, T> for $Enum<U, A> {
|
||||
fn provide <'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.take_or_fail(token, ||"x: no unit")?,
|
||||
state.take_or_fail(token, ||"x: no content")?,
|
||||
state.receive_or_fail(token, ||"x: no unit")?,
|
||||
state.receive_or_fail(token, ||"x: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($y), .. }) => Self::y(
|
||||
state.take_or_fail(token, ||"y: no unit")?,
|
||||
state.take_or_fail(token, ||"y: no content")?,
|
||||
state.receive_or_fail(token, ||"y: no unit")?,
|
||||
state.receive_or_fail(token, ||"y: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
||||
state.take_or_fail(token, ||"xy: no unit x")?,
|
||||
state.take_or_fail(token, ||"xy: no unit y")?,
|
||||
state.take_or_fail(token, ||"xy: no content")?
|
||||
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")?
|
||||
),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -9,19 +9,17 @@ impl<A> When<A> {
|
|||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<A, T: Dsl<bool> + Dsl<A>> Namespace<T> for When<A> {
|
||||
fn take_from <'source> (
|
||||
state: &T,
|
||||
token: &mut TokenIter<'source>
|
||||
) -> Perhaps<Self> {
|
||||
impl<'n, State: Receive<bool> + Receive<A>, A: 'n> Provide<'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"),
|
||||
..
|
||||
}) = token.peek() {
|
||||
let base = token.clone();
|
||||
}) = words.peek() {
|
||||
let _ = words.next();
|
||||
let base = words.clone();
|
||||
return Ok(Some(Self(
|
||||
state.take_or_fail(token, ||"cond: no condition")?,
|
||||
state.take_or_fail(token, ||"cond: no content")?,
|
||||
state.receive_or_fail(words, ||"cond: no condition")?,
|
||||
state.receive_or_fail(words, ||"cond: no content")?,
|
||||
)))
|
||||
} else {
|
||||
None
|
||||
|
|
|
|||
|
|
@ -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: Namespace::take_from_or_fail(self, words)?,
|
||||
#arg: Provide::provide_or_fail(self, words)?,
|
||||
});
|
||||
}
|
||||
out
|
||||
|
|
@ -149,8 +149,8 @@ impl ToTokens for CommandDef {
|
|||
}
|
||||
}
|
||||
/// Generated by [tengri_proc::command].
|
||||
impl ::tengri::dsl::Namespace<#state> for #command_enum {
|
||||
fn take_from <'source> (
|
||||
impl<'n> ::tengri::dsl::Provide<'n, #state> for #command_enum {
|
||||
fn provide <'source> (
|
||||
state: &#state,
|
||||
words: &mut ::tengri::dsl::TokenIter<'source>
|
||||
) -> Perhaps<Self> {
|
||||
|
|
|
|||
|
|
@ -85,8 +85,8 @@ impl ToTokens for ExposeImpl {
|
|||
let values = variants.iter().map(ExposeArm::from);
|
||||
write_quote_to(out, quote! {
|
||||
/// Generated by [tengri_proc::expose].
|
||||
impl ::tengri::dsl::Namespace<#state> for #t {
|
||||
fn take_from <'source> (
|
||||
impl<'n> ::tengri::dsl::Provide<'n, #state> for #t {
|
||||
fn provide <'source> (
|
||||
state: &#state,
|
||||
words: &mut ::tengri::dsl::TokenIter<'source>
|
||||
) -> Perhaps<Self> {
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ impl ToTokens for ViewDef {
|
|||
let builtins: Vec<_> = builtins_with_types()
|
||||
.iter()
|
||||
.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 {
|
||||
return Ok(Some(value.boxed()))
|
||||
}
|
||||
|
|
@ -55,30 +55,20 @@ impl ToTokens for ViewDef {
|
|||
let exposed: Vec<_> = exposed
|
||||
.iter()
|
||||
.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();
|
||||
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].
|
||||
///
|
||||
/// 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::Namespace<#self_ty> for Box<dyn Render<#output> + '_> {
|
||||
fn take_from <'source> (
|
||||
state: &#self_ty,
|
||||
words: &mut ::tengri::dsl::TokenIter<'source>
|
||||
) -> Perhaps<Self> {
|
||||
impl ::tengri::dsl::Receive<Box<dyn Render<#output>>> for #self_ty {
|
||||
fn receive <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>)
|
||||
-> Perhaps<Box<dyn Render<#output>>>
|
||||
{
|
||||
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
|
||||
match value {
|
||||
// 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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue