proc: builtims
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-25 11:43:35 +03:00
parent 3e1084555b
commit 31e84bf5b3
3 changed files with 55 additions and 68 deletions

View file

@ -1,10 +1,17 @@
use crate::*;
// maybe their names should be switched around?
///// Implement the [Give] trait, which boils down to
///// specifying two types and providing an expression.
#[macro_export] macro_rules! from_dsl {
($Type:ty: |$state:ident:$State:ty, $words:ident|$expr:expr) => {
take! { $Type|$state:$State,$words|$expr }
};
}
/// [Take]s instances of [Type] given [TokenIter].
pub trait Give<'state, Type> {
/// Implement this to be able to [Give] [Type] from the [TokenIter].
/// Advance the stream if returning `Ok<Some<Type>>`.
fn give <'source: 'state> (&self, words: TokenIter<'source>) -> Perhaps<Type>;
/// Return custom error on [None].
fn give_or_fail <'source: 'state, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
@ -59,6 +66,8 @@ pub trait Give<'state, Type> {
}
/// [Give]s instances of [Self] given [TokenIter].
pub trait Take<'state, State>: Sized {
/// Implement this to be able to [Take] [Self] from the [TokenIter].
/// Advance the stream if returning `Ok<Some<Self>>`.
fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self>;
/// Return custom error on [None].
fn take_or_fail <'source: 'state, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
@ -113,40 +122,6 @@ pub trait Take<'state, State>: Sized {
};
}
#[cfg(feature="dsl")]
impl<'state, E: 'state, State: Give<'state, Box<dyn Render<E> + 'state>>>
Take<'state, State> for Box<dyn Render<E> + 'state> {
fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self> {
state.give(words)
}
}
/// 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<'state, State, A: Take<'state, State>> Take<'state, State> for $T {
fn take <'source: 'state> ($state: &State, mut $words:TokenIter<'source>) -> Perhaps<$T> {
$expr
}
}
};
(@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
impl<'state, State, A: Take<'state, State>, B: Take<'state, State>> Take<'state, State> for $T {
fn take <'source: 'state> ($state: &State, mut $words:TokenIter<'source>) -> Perhaps<$T> {
$expr
}
}
};
($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => {
impl<'state> Take<'state, $S> for $T {
fn take <'source: 'state> ($state: &$S, mut $words:TokenIter<'source>) -> Perhaps<$T> {
$expr
}
}
};
}
// auto impl graveyard:
//impl<'state, State: Give<Type>, Type: 'state> Take<'state, State> for Type {
@ -157,8 +132,16 @@ Take<'state, State> for Box<dyn Render<E> + 'state> {
//}
//}
//impl<'state, Type: Take<'state, State>, State> Give<Type> for State {
//fn take <'state> (&self, mut words:TokenIter<'source>) -> Perhaps<Type> {
//Type::take(self, words)
//#[cfg(feature="dsl")]
//impl<'state, E: 'state, State: Give<'state, Box<dyn Render<E> + 'state>>>
//Take<'state, State> for Box<dyn Render<E> + 'state> {
//fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self> {
//state.give(words)
//}
//}
impl<'state, Type: Take<'state, State>, State> Give<'state, Type> for State {
fn give <'source: 'state> (&self, words: TokenIter<'source>) -> Perhaps<Type> {
Type::take(self, words)
}
}