mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
dsl: spiffier notfounds
This commit is contained in:
parent
7516517078
commit
daef8dfa9e
1 changed files with 10 additions and 10 deletions
|
|
@ -1,34 +1,34 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
pub trait Dsl<Type> {
|
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> (
|
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> {
|
) -> Usually<Type> {
|
||||||
if let Some(value) = Dsl::<Type>::take(self, token)? {
|
if let Some(value) = Dsl::<Type>::take(self, words)? {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
} else {
|
} 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 {
|
impl<Type: Namespace<State>, State> Dsl<Type> for State {
|
||||||
fn take <'source> (&self, token: &mut TokenIter<'source>) -> Perhaps<Type> {
|
fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
|
||||||
Namespace::take_from(self, token)
|
Namespace::take_from(self, words)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Namespace<State>: Sized {
|
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>;
|
-> Perhaps<Self>;
|
||||||
fn take_from_or_fail <'source> (
|
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> {
|
) -> Usually<Self> {
|
||||||
if let Some(value) = Namespace::<State>::take_from(state, token)? {
|
if let Some(value) = Namespace::<State>::take_from(state, words)? {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
} else {
|
} else {
|
||||||
Result::Err(error.into())
|
Result::Err(format!("{}: {:?}", error.into(), words.peek().map(|x|x.value)).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue