forked from Orchid/orchid
First steps for the macro system
This commit is contained in:
@@ -158,6 +158,40 @@ impl fmt::Display for OrcErrv {
|
||||
|
||||
pub type OrcRes<T> = Result<T, OrcErrv>;
|
||||
|
||||
pub fn join_ok<T, U>(left: OrcRes<T>, right: OrcRes<U>) -> OrcRes<(T, U)> {
|
||||
match (left, right) {
|
||||
(Ok(t), Ok(u)) => Ok((t, u)),
|
||||
(Err(e), Ok(_)) | (Ok(_), Err(e)) => Err(e),
|
||||
(Err(e1), Err(e2)) => Err(e1 + e2),
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! join_ok {
|
||||
($($names:ident $(: $tys:ty)? = $vals:expr;)*) => {
|
||||
let $crate::join_ok!(@NAMES $($names $(: $tys)? = $vals;)*)
|
||||
:
|
||||
$crate::join_ok!(@TYPES $($names $(: $tys)? = $vals;)*)
|
||||
=
|
||||
$crate::join_ok!(@VALUES $($names $(: $tys)? = $vals;)*)?;
|
||||
};
|
||||
(@NAMES $name:ident $(: $ty:ty)? = $val:expr ; $($names:ident $(: $tys:ty)? = $vals:expr;)*) => {
|
||||
($name, $crate::join_ok!(@NAMES $($names $(: $tys)? = $vals;)*))
|
||||
};
|
||||
(@NAMES) => { _ };
|
||||
(@TYPES $name:ident : $ty:ty = $val:expr ; $($names:ident $(: $tys:ty)? = $vals:expr;)*) => {
|
||||
($ty, $crate::join_ok!(@TYPES $($names $(: $tys)? = $vals;)*))
|
||||
};
|
||||
(@TYPES $name:ident = $val:expr ; $($names:ident $(: $tys:ty)? = $vals:expr;)*) => {
|
||||
(_, $crate::join_ok!(@TYPES $($names $(: $tys)? = $vals;)*))
|
||||
};
|
||||
(@TYPES) => { () };
|
||||
(@VALUES $name:ident $(: $ty:ty)? = $val:expr ; $($names:ident $(: $tys:ty)? = $vals:expr;)*) => {
|
||||
$crate::error::join_ok($val, $crate::join_ok!(@VALUES $($names $(: $tys)? = $vals;)*))
|
||||
};
|
||||
(@VALUES) => { Ok(()) };
|
||||
}
|
||||
|
||||
pub fn mk_err(
|
||||
description: Tok<String>,
|
||||
message: impl AsRef<str>,
|
||||
|
||||
Reference in New Issue
Block a user