mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
re foreign trait constraints
This commit is contained in:
parent
ddf0c05d5f
commit
cbd28a5934
10 changed files with 30 additions and 24 deletions
|
|
@ -18,17 +18,23 @@ pub trait Give<Type> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'n, T: Take<'n, U>, U> Give<T> for U {
|
||||||
|
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<T> {
|
||||||
|
T::take(self, words)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// [Give]s instances of [Self] given [TokenIter].
|
/// [Give]s instances of [Self] given [TokenIter].
|
||||||
pub trait Take<'n, State>: Sized + 'n {
|
pub trait Take<'n, State>: Sized + 'n {
|
||||||
fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self>;
|
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self>;
|
||||||
/// Return custom error on [None].
|
/// Return custom error on [None].
|
||||||
fn provide_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
fn take_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) = Take::<State>::provide(state, words)? {
|
if let Some(value) = Take::<State>::take(state, words)? {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
} else {
|
} else {
|
||||||
Result::Err(format!("provide: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
|
Result::Err(format!("take: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -38,21 +44,21 @@ pub trait Take<'n, State>: Sized + 'n {
|
||||||
#[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: Take<'n, State>> Take<'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 take <'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: Take<'n, State>, B: Take<'n, State>> Take<'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 take <'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> Take<'n, $S> for $T {
|
impl<'n> Take<'n, $S> for $T {
|
||||||
fn provide <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
fn take <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||||
$expr
|
$expr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +68,7 @@ pub trait Take<'n, State>: Sized + 'n {
|
||||||
// auto impl graveyard:
|
// auto impl graveyard:
|
||||||
|
|
||||||
//impl<'n, State: Give<Type>, Type: 'n> Take<'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 take <'source> (state: &State, words: &mut TokenIter<'source>)
|
||||||
//-> Perhaps<Self>
|
//-> Perhaps<Self>
|
||||||
//{
|
//{
|
||||||
//state.take(words)
|
//state.take(words)
|
||||||
|
|
@ -71,6 +77,6 @@ pub trait Take<'n, State>: Sized + 'n {
|
||||||
|
|
||||||
//impl<'n, Type: Take<'n, State>, State> Give<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::take(self, words)
|
||||||
//}
|
//}
|
||||||
//}
|
//}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ impl<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for So
|
||||||
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) = Take::provide(state, &mut exp_iter)? {
|
if let Some(command) = Take::take(state, &mut exp_iter)? {
|
||||||
return Ok(Some(command))
|
return Ok(Some(command))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -48,7 +48,7 @@ impl<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for To
|
||||||
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) = Take::provide(state, &mut e)? {
|
if let Some(command) = Take::take(state, &mut e)? {
|
||||||
return Ok(Some(command))
|
return Ok(Some(command))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ pub struct Align<A>(Alignment, A);
|
||||||
|
|
||||||
#[cfg(feature = "dsl")]
|
#[cfg(feature = "dsl")]
|
||||||
impl<'n, State: Give<A>, A: 'n> Take<'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 take <'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 {
|
||||||
"align/c"|"align/x"|"align/y"|
|
"align/c"|"align/x"|"align/y"|
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,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: Give<A> + Give<B>, A: 'n, B: 'n> Take<'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 take <'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"),
|
||||||
..
|
..
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ impl<A, B> Either<A, B> {
|
||||||
}
|
}
|
||||||
#[cfg(feature = "dsl")]
|
#[cfg(feature = "dsl")]
|
||||||
impl<'n, State: Give<bool> + Give<A> + Give<B>, A: 'n, B: 'n> Take<'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 take <'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();
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
//! [Content] items that modify the inherent
|
//! [Content] items that modify the inherent
|
||||||
//! dimensions of their inner [Render]ables.
|
//! dimensions of their inner [Render]ables.
|
||||||
//!
|
//!
|
||||||
//! Transform may also react to the [Area] provided.
|
//! Transform may also react to the [Area] taked.
|
||||||
//! ```
|
//! ```
|
||||||
//! use ::tengri::{output::*, tui::*};
|
//! use ::tengri::{output::*, tui::*};
|
||||||
//! let area: [u16;4] = [10, 10, 20, 20];
|
//! let area: [u16;4] = [10, 10, 20, 20];
|
||||||
|
|
@ -34,7 +34,7 @@ macro_rules! transform_xy {
|
||||||
}
|
}
|
||||||
#[cfg(feature = "dsl")]
|
#[cfg(feature = "dsl")]
|
||||||
impl<'n, A: 'n, T: Give<A>> Take<'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 take <'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() {
|
||||||
|
|
@ -80,7 +80,7 @@ macro_rules! transform_xy_unit {
|
||||||
}
|
}
|
||||||
#[cfg(feature = "dsl")]
|
#[cfg(feature = "dsl")]
|
||||||
impl<'n, A: 'n, U: Coordinate + 'n, T: Give<A> + Give<U>> Take<'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 take <'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() {
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ impl<A> When<A> {
|
||||||
|
|
||||||
#[cfg(feature = "dsl")]
|
#[cfg(feature = "dsl")]
|
||||||
impl<'n, State: Give<bool>, A: Take<'n, State>> Take<'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 take <'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"),
|
||||||
..
|
..
|
||||||
|
|
@ -21,7 +21,7 @@ impl<'n, State: Give<bool>, A: Take<'n, State>> Take<'n, State> for When<A> {
|
||||||
let base = words.clone();
|
let base = words.clone();
|
||||||
return Ok(Some(Self(
|
return Ok(Some(Self(
|
||||||
state.give_or_fail(words, ||"cond: no condition")?,
|
state.give_or_fail(words, ||"cond: no condition")?,
|
||||||
A::provide_or_fail(state, words, ||"cond: no content")?,
|
A::take_or_fail(state, words, ||"cond: no content")?,
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
|
||||||
|
|
@ -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: Take::provide_or_fail(self, words)?,
|
#arg: Take::take_or_fail(self, words)?,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
|
|
@ -150,7 +150,7 @@ impl ToTokens for CommandDef {
|
||||||
}
|
}
|
||||||
/// Generated by [tengri_proc::command].
|
/// Generated by [tengri_proc::command].
|
||||||
impl<'n> ::tengri::dsl::Take<'n, #state> for #command_enum {
|
impl<'n> ::tengri::dsl::Take<'n, #state> for #command_enum {
|
||||||
fn provide <'source> (
|
fn take <'source> (
|
||||||
state: &#state,
|
state: &#state,
|
||||||
words: &mut ::tengri::dsl::TokenIter<'source>
|
words: &mut ::tengri::dsl::TokenIter<'source>
|
||||||
) -> Perhaps<Self> {
|
) -> Perhaps<Self> {
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ impl ToTokens for ExposeImpl {
|
||||||
write_quote_to(out, quote! {
|
write_quote_to(out, quote! {
|
||||||
/// Generated by [tengri_proc::expose].
|
/// Generated by [tengri_proc::expose].
|
||||||
impl<'n> ::tengri::dsl::Take<'n, #state> for #t {
|
impl<'n> ::tengri::dsl::Take<'n, #state> for #t {
|
||||||
fn provide <'source> (
|
fn take <'source> (
|
||||||
state: &#state,
|
state: &#state,
|
||||||
words: &mut ::tengri::dsl::TokenIter<'source>
|
words: &mut ::tengri::dsl::TokenIter<'source>
|
||||||
) -> Perhaps<Self> {
|
) -> Perhaps<Self> {
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,7 @@ impl ToTokens for ViewDef {
|
||||||
#(#builtins)*
|
#(#builtins)*
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
// Symbols are handled by user-provided functions
|
// Symbols are handled by user-taked functions
|
||||||
// that take no parameters but `&self`.
|
// that take no parameters but `&self`.
|
||||||
::tengri::dsl::Value::Sym(sym) => match sym {
|
::tengri::dsl::Value::Sym(sym) => match sym {
|
||||||
#(#exposed)*
|
#(#exposed)*
|
||||||
|
|
@ -100,7 +100,7 @@ impl ToTokens for ViewDef {
|
||||||
#self_ty::view(self)
|
#self_ty::view(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Original user-provided implementation:
|
// Original user-taked implementation:
|
||||||
#block
|
#block
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue