dsl, output: error handlers
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-21 14:17:27 +03:00
parent a4a1066f18
commit abc87d3234
7 changed files with 25 additions and 197 deletions

View file

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