mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
dsl: add DslExpNs::from_exp; return Perhaps rather than Usually
Some checks failed
/ build (push) Has been cancelled
Some checks failed
/ build (push) Has been cancelled
This commit is contained in:
parent
3298d6b6e1
commit
a1190a24a1
1 changed files with 15 additions and 5 deletions
|
|
@ -6,15 +6,15 @@ pub struct DslNs<'t, T: 't>(pub &'t [(&'t str, T)]);
|
||||||
/// Namespace where keys are symbols.
|
/// Namespace where keys are symbols.
|
||||||
pub trait DslSymNs<'t, T: 't>: 't {
|
pub trait DslSymNs<'t, T: 't>: 't {
|
||||||
const SYMS: DslNs<'t, fn (&'t Self)->T>;
|
const SYMS: DslNs<'t, fn (&'t Self)->T>;
|
||||||
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Usually<T> {
|
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
if let Some(dsl) = dsl.sym()? {
|
if let Some(dsl) = dsl.sym()? {
|
||||||
for (sym, get) in Self::SYMS.0 {
|
for (sym, get) in Self::SYMS.0 {
|
||||||
if dsl == *sym {
|
if dsl == *sym {
|
||||||
return Ok(get(self))
|
return Ok(Some(get(self)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Err(format!("not found: sym: {dsl:?}").into())
|
return Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -26,11 +26,21 @@ pub trait DslSymNs<'t, T: 't>: 't {
|
||||||
|
|
||||||
pub trait DslExpNs<'t, T: 't>: 't {
|
pub trait DslExpNs<'t, T: 't>: 't {
|
||||||
const EXPS: DslNs<'t, fn (&'t Self, &str)->T>;
|
const EXPS: DslNs<'t, fn (&'t Self, &str)->T>;
|
||||||
|
fn from_exp <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
|
if let Some(exp) = dsl.exp()? {
|
||||||
|
for (key, value) in Self::EXPS.0.iter() {
|
||||||
|
if exp.head() == Ok(Some(key)) {
|
||||||
|
return Ok(Some(value(self, exp.tail()?.unwrap_or(""))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export] macro_rules! dsl_exp (
|
#[macro_export] macro_rules! dsl_exp (
|
||||||
(|$state:ident:$State:ty|->$type:ty { $(
|
(|$state:ident:$State:ty$(, $tail:ident)?|->$type:ty { $(
|
||||||
[$key:literal $(/ $sub:ident: $Sub:ty)? $(, $arg:ident $(?)? :$argtype:ty)*] => $body:expr
|
($key:literal $(/ $sub:ident: $Sub:ty)? $(, $arg:ident $(?)? :$argtype:ty)*) => $body:expr
|
||||||
),* $(,)? }) => {
|
),* $(,)? }) => {
|
||||||
impl<'t> DslExpNs<'t, $type> for $State {
|
impl<'t> DslExpNs<'t, $type> for $State {
|
||||||
const EXPS: DslNs<'t, fn (&'t $State, &str)->$type> =
|
const EXPS: DslNs<'t, fn (&'t $State, &str)->$type> =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue