mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
dsl: Provide -> Take, Receive -> Give (swap + shorten)
This commit is contained in:
parent
583660c330
commit
ddf0c05d5f
10 changed files with 64 additions and 55 deletions
|
|
@ -1,25 +1,31 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
pub trait Receive<Type> {
|
// maybe their names should be switched around?
|
||||||
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> (
|
/// [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
|
&self, words: &mut TokenIter<'source>, error: F
|
||||||
) -> Usually<Type> {
|
) -> Usually<Type> {
|
||||||
if let Some(value) = Receive::<Type>::receive(self, words)? {
|
if let Some(value) = Give::<Type>::give(self, words)? {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
} else {
|
} 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 {
|
/// [Give]s instances of [Self] given [TokenIter].
|
||||||
fn provide <'source> (state: &State, words: &mut TokenIter<'source>)
|
pub trait Take<'n, State>: Sized + 'n {
|
||||||
-> Perhaps<Self>;
|
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> (
|
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) = Provide::<State>::provide(state, words)? {
|
if let Some(value) = Take::<State>::provide(state, words)? {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
} else {
|
} else {
|
||||||
Result::Err(format!("provide: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
|
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.
|
/// 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<'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> {
|
fn provide <'source> ($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<'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> {
|
fn provide <'source> ($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<'n> Provide<'n, $S> for $T {
|
impl<'n> Take<'n, $S> for $T {
|
||||||
fn provide <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
fn provide <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||||
$expr
|
$expr
|
||||||
}
|
}
|
||||||
|
|
@ -55,7 +61,7 @@ pub trait Provide<'n, State>: Sized + 'n {
|
||||||
|
|
||||||
// auto impl graveyard:
|
// 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>)
|
//fn provide <'source> (state: &State, words: &mut TokenIter<'source>)
|
||||||
//-> Perhaps<Self>
|
//-> 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> {
|
//fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
|
||||||
//Type::provide(self, words)
|
//Type::provide(self, words)
|
||||||
//}
|
//}
|
||||||
|
|
|
||||||
|
|
@ -5,13 +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<'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.
|
/// 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<'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> {
|
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() {
|
||||||
|
|
@ -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() {
|
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) = Provide::provide(state, &mut exp_iter)? {
|
if let Some(command) = Take::provide(state, &mut exp_iter)? {
|
||||||
return Ok(Some(command))
|
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].
|
/// 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> {
|
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() {
|
||||||
|
|
@ -48,7 +48,7 @@ impl<'k, S, C: Provide<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for
|
||||||
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) = Provide::provide(state, &mut e)? {
|
if let Some(command) = Take::provide(state, &mut e)? {
|
||||||
return Ok(Some(command))
|
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.
|
/// which may be made available subject to given conditions.
|
||||||
pub struct InputMap<'k,
|
pub struct InputMap<'k,
|
||||||
S,
|
S,
|
||||||
C: Command<S> + Provide<'k, S>,
|
C: Command<S> + Take<'k, S>,
|
||||||
I: DslInput,
|
I: DslInput,
|
||||||
M: KeyMap<'k, S, C, I>
|
M: KeyMap<'k, S, C, I>
|
||||||
> {
|
> {
|
||||||
|
|
@ -79,7 +79,7 @@ pub struct InputMap<'k,
|
||||||
|
|
||||||
impl<'k,
|
impl<'k,
|
||||||
S,
|
S,
|
||||||
C: Command<S> + Provide<'k, S>,
|
C: Command<S> + Take<'k, S>,
|
||||||
I: DslInput,
|
I: DslInput,
|
||||||
M: KeyMap<'k, S, C, I>
|
M: KeyMap<'k, S, C, I>
|
||||||
> Default for InputMap<'k, S, C, I, M>{
|
> 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> {
|
InputMap<'k, S, C, I, M> {
|
||||||
pub fn new (keymap: M) -> Self {
|
pub fn new (keymap: M) -> Self {
|
||||||
Self::default().layer(keymap)
|
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> {
|
std::fmt::Debug for InputMap<'k, 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())
|
||||||
|
|
@ -121,7 +121,7 @@ std::fmt::Debug for InputMap<'k, S, C, I, M> {
|
||||||
/// An [InputMap] can be a [KeyMap].
|
/// An [InputMap] can be a [KeyMap].
|
||||||
impl<'k,
|
impl<'k,
|
||||||
S,
|
S,
|
||||||
C: Command<S> + Provide<'k, S>,
|
C: Command<S> + Take<'k, S>,
|
||||||
I: DslInput,
|
I: DslInput,
|
||||||
M: KeyMap<'k, S, C, I>
|
M: KeyMap<'k, S, C, I>
|
||||||
> KeyMap<'k, S, C, I> for InputMap<'k, S, C, I, M> {
|
> KeyMap<'k, S, C, I> for InputMap<'k, S, C, I, M> {
|
||||||
|
|
|
||||||
|
|
@ -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);
|
pub struct Align<A>(Alignment, A);
|
||||||
|
|
||||||
#[cfg(feature = "dsl")]
|
#[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> {
|
fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||||
if let Some(Token { value: Value::Key(key), .. }) = words.peek() {
|
if let Some(Token { value: Value::Key(key), .. }) = words.peek() {
|
||||||
match key {
|
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/n"|"align/s"|"align/e"|"align/w"|
|
||||||
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
|
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
|
||||||
let _ = words.next().unwrap();
|
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 {
|
return Ok(Some(match key {
|
||||||
"align/c" => Self::c(content),
|
"align/c" => Self::c(content),
|
||||||
"align/x" => Self::x(content),
|
"align/x" => Self::x(content),
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature = "dsl")]
|
#[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> {
|
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"),
|
||||||
|
|
@ -29,8 +29,8 @@ impl<'n, State: Receive<A> + Receive<B>, A: 'n, B: 'n> Provide<'n, State> for Bs
|
||||||
}) = words.peek() {
|
}) = words.peek() {
|
||||||
if let Value::Key(key) = words.next().unwrap().value() {
|
if let Value::Key(key) = words.next().unwrap().value() {
|
||||||
let base = words.clone();
|
let base = words.clone();
|
||||||
let a: A = state.receive_or_fail(words, ||"bsp: expected content 1")?;
|
let a: A = state.give_or_fail(words, ||"bsp: expected content 1")?;
|
||||||
let b: B = state.receive_or_fail(words, ||"bsp: expected content 2")?;
|
let b: B = state.give_or_fail(words, ||"bsp: expected content 2")?;
|
||||||
return Ok(Some(match key {
|
return Ok(Some(match key {
|
||||||
"bsp/n" => Self::n(a, b),
|
"bsp/n" => Self::n(a, b),
|
||||||
"bsp/s" => Self::s(a, b),
|
"bsp/s" => Self::s(a, b),
|
||||||
|
|
|
||||||
|
|
@ -9,15 +9,15 @@ impl<A, B> Either<A, B> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature = "dsl")]
|
#[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> {
|
fn provide <'source> (state: &State, 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.receive_or_fail(token, ||"either: no condition")?,
|
state.give_or_fail(token, ||"either: no condition")?,
|
||||||
state.receive_or_fail(token, ||"either: no content 1")?,
|
state.give_or_fail(token, ||"either: no content 1")?,
|
||||||
state.receive_or_fail(token, ||"either: no content 2")?,
|
state.give_or_fail(token, ||"either: no content 2")?,
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ 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<'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>)
|
fn provide <'source> (state: &T, token: &mut TokenIter<'source>)
|
||||||
-> Perhaps<Self>
|
-> Perhaps<Self>
|
||||||
{
|
{
|
||||||
|
|
@ -41,11 +41,11 @@ macro_rules! transform_xy {
|
||||||
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.receive_or_fail(token, ||"x: no content")?),
|
Self::x(state.give_or_fail(token, ||"x: no content")?),
|
||||||
Some(Token{value:Value::Key($y),..}) =>
|
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),..}) =>
|
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!()
|
_ => 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) }
|
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
|
||||||
}
|
}
|
||||||
#[cfg(feature = "dsl")]
|
#[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> (
|
fn provide <'source> (
|
||||||
state: &T, token: &mut TokenIter<'source>
|
state: &T, token: &mut TokenIter<'source>
|
||||||
) -> Perhaps<Self> {
|
) -> Perhaps<Self> {
|
||||||
|
|
@ -87,17 +87,17 @@ macro_rules! transform_xy_unit {
|
||||||
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.receive_or_fail(token, ||"x: no unit")?,
|
state.give_or_fail(token, ||"x: no unit")?,
|
||||||
state.receive_or_fail(token, ||"x: no content")?,
|
state.give_or_fail(token, ||"x: no content")?,
|
||||||
),
|
),
|
||||||
Some(Token { value: Value::Key($y), .. }) => Self::y(
|
Some(Token { value: Value::Key($y), .. }) => Self::y(
|
||||||
state.receive_or_fail(token, ||"y: no unit")?,
|
state.give_or_fail(token, ||"y: no unit")?,
|
||||||
state.receive_or_fail(token, ||"y: no content")?,
|
state.give_or_fail(token, ||"y: no content")?,
|
||||||
),
|
),
|
||||||
Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
||||||
state.receive_or_fail(token, ||"xy: no unit x")?,
|
state.give_or_fail(token, ||"xy: no unit x")?,
|
||||||
state.receive_or_fail(token, ||"xy: no unit y")?,
|
state.give_or_fail(token, ||"xy: no unit y")?,
|
||||||
state.receive_or_fail(token, ||"xy: no content")?
|
state.give_or_fail(token, ||"xy: no content")?
|
||||||
),
|
),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,16 @@ use crate::*;
|
||||||
|
|
||||||
/// Show an item only when a condition is true.
|
/// Show an item only when a condition is true.
|
||||||
pub struct When<A>(pub bool, pub A);
|
pub struct When<A>(pub bool, pub A);
|
||||||
|
|
||||||
impl<A> When<A> {
|
impl<A> When<A> {
|
||||||
/// Create a binary condition.
|
/// Create a binary condition.
|
||||||
pub const fn new (c: bool, a: A) -> Self {
|
pub const fn new (c: bool, a: A) -> Self {
|
||||||
Self(c, a)
|
Self(c, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "dsl")]
|
#[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> {
|
fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||||
Ok(if let Some(Token {
|
Ok(if let Some(Token {
|
||||||
value: Value::Key("when"),
|
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 _ = words.next();
|
||||||
let base = words.clone();
|
let base = words.clone();
|
||||||
return Ok(Some(Self(
|
return Ok(Some(Self(
|
||||||
state.receive_or_fail(words, ||"cond: no condition")?,
|
state.give_or_fail(words, ||"cond: no condition")?,
|
||||||
state.receive_or_fail(words, ||"cond: no content")?,
|
A::provide_or_fail(state, words, ||"cond: no content")?,
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Output, A: Render<E>> Content<E> for When<A> {
|
impl<E: Output, A: Render<E>> Content<E> for When<A> {
|
||||||
fn layout (&self, to: E::Area) -> E::Area {
|
fn layout (&self, to: E::Area) -> E::Area {
|
||||||
let Self(cond, item) = self;
|
let Self(cond, item) = self;
|
||||||
|
|
|
||||||
|
|
@ -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: Provide::provide_or_fail(self, words)?,
|
#arg: Take::provide_or_fail(self, words)?,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
|
|
@ -149,7 +149,7 @@ impl ToTokens for CommandDef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Generated by [tengri_proc::command].
|
/// 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> (
|
fn provide <'source> (
|
||||||
state: &#state,
|
state: &#state,
|
||||||
words: &mut ::tengri::dsl::TokenIter<'source>
|
words: &mut ::tengri::dsl::TokenIter<'source>
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ 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<'n> ::tengri::dsl::Provide<'n, #state> for #t {
|
impl<'n> ::tengri::dsl::Take<'n, #state> for #t {
|
||||||
fn provide <'source> (
|
fn provide <'source> (
|
||||||
state: &#state,
|
state: &#state,
|
||||||
words: &mut ::tengri::dsl::TokenIter<'source>
|
words: &mut ::tengri::dsl::TokenIter<'source>
|
||||||
|
|
|
||||||
|
|
@ -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> = Receive::<#ty>::receive(self, &mut exp.clone())?;
|
let value: Option<#ty> = Give::<#ty>::give(self, &mut exp.clone())?;
|
||||||
if let Some(value) = value {
|
if let Some(value) = value {
|
||||||
return Ok(Some(value.boxed()))
|
return Ok(Some(value.boxed()))
|
||||||
}
|
}
|
||||||
|
|
@ -65,8 +65,8 @@ impl ToTokens for ViewDef {
|
||||||
/// 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::Receive<Box<dyn Render<#output>>> for #self_ty {
|
impl ::tengri::dsl::Give<Box<dyn Render<#output>>> for #self_ty {
|
||||||
fn receive <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>)
|
fn give <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>)
|
||||||
-> Perhaps<Box<dyn Render<#output>>>
|
-> Perhaps<Box<dyn Render<#output>>>
|
||||||
{
|
{
|
||||||
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
|
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue