pub(crate) use std::error::Error; /// Standard result type. pub type Usually = Result>; /// Standard optional result type. pub type Perhaps = Result, Box>; /// Implement the `From` trait. #[macro_export] macro_rules! from { ($(<$($lt:lifetime),+>)?|$state:ident:$Source:ty|$Target:ty=$cb:expr) => { impl $(<$($lt),+>)? From<$Source> for $Target { fn from ($state:$Source) -> Self { $cb } } }; } pub trait Has: Send + Sync { fn get (&self) -> &T; fn get_mut (&mut self) -> &mut T; } #[macro_export] macro_rules! has { ($T:ty: |$self:ident : $S:ty| $x:expr) => { impl Has<$T> for $S { fn get (&$self) -> &$T { &$x } fn get_mut (&mut $self) -> &mut $T { &mut $x } } }; } pub trait MaybeHas: Send + Sync { fn get (&self) -> Option<&T>; fn get_mut (&mut self) -> Option<&mut T>; } #[macro_export] macro_rules! maybe_has { ($T:ty: |$self:ident : $S:ty| $x:block; $y:block $(;)?) => { impl MaybeHas<$T> for $S { fn get (&$self) -> Option<&$T> $x fn get_mut (&mut $self) -> Option<&mut $T> $y } }; } /// May compute a `RetVal` from `Args`. pub trait Eval { /// A custom operation on [Args] that may return [Result::Err] or [Option::None]. fn try_eval (&self, args: &Args) -> Perhaps; /// Invoke a custom operation, converting a `None` result to a custom `Box`. fn eval >> (&self, args: &Args, error: impl Fn()->E) -> Usually { match self.try_eval(args)? { Some(value) => Ok(value), _ => Result::Err(format!("Eval: {}", error().into()).into()) } } } //impl, I, O> Eval for &S { //fn try_eval (&self, input: I) -> Perhaps { //(*self).try_eval(input) //} //} //impl, I: Ast, O: Dsl> Eval for S { //fn try_eval (&self, input: I) -> Perhaps { //Dsl::try_provide(self, input) //} //}