dsl: spiffier notfounds

This commit is contained in:
🪞👃🪞 2025-05-21 14:10:40 +03:00
parent 7516517078
commit daef8dfa9e

View file

@ -1,34 +1,34 @@
use crate::*;
pub trait Dsl<Type> {
fn take <'source> (&self, token: &mut TokenIter<'source>) -> Perhaps<Type>;
fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type>;
fn take_or_fail <'source> (
&self, token: &mut TokenIter<'source>, error: impl Into<Box<dyn std::error::Error>>
&self, words: &mut TokenIter<'source>, error: impl Into<Box<dyn std::error::Error>>
) -> Usually<Type> {
if let Some(value) = Dsl::<Type>::take(self, token)? {
if let Some(value) = Dsl::<Type>::take(self, words)? {
Ok(value)
} else {
Result::Err(error.into())
Result::Err(format!("{}: {:?}", error.into(), words.peek().map(|x|x.value)).into())
}
}
}
impl<Type: Namespace<State>, State> Dsl<Type> for State {
fn take <'source> (&self, token: &mut TokenIter<'source>) -> Perhaps<Type> {
Namespace::take_from(self, token)
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, token: &mut TokenIter<'source>)
fn take_from <'source> (state: &State, words: &mut TokenIter<'source>)
-> Perhaps<Self>;
fn take_from_or_fail <'source> (
state: &State, token: &mut TokenIter<'source>, error: impl Into<Box<dyn std::error::Error>>
state: &State, words: &mut TokenIter<'source>, error: impl Into<Box<dyn std::error::Error>>
) -> Usually<Self> {
if let Some(value) = Namespace::<State>::take_from(state, token)? {
if let Some(value) = Namespace::<State>::take_from(state, words)? {
Ok(value)
} else {
Result::Err(error.into())
Result::Err(format!("{}: {:?}", error.into(), words.peek().map(|x|x.value)).into())
}
}
}