Skip to content

Commit 4a650db

Browse files
committed
feat: added macro to automatically match input values for a specific pattern
1 parent 7050c89 commit 4a650db

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

taurus/src/context/macros.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/// Pulls typed parameters from a slice of `Argument` using your `TryFromArgument`
2+
/// impls. Fails early with your `Signal::Failure(RuntimeError::simple(...))`.
3+
macro_rules! args {
4+
($args_ident:ident => $( $name:ident : $ty:ty ),+ $(,)?) => {
5+
// Arity check
6+
let __expected: usize = 0usize $(+ { let _ = ::core::any::type_name::<$ty>(); 1usize })*;
7+
if $args_ident.len() != __expected {
8+
return $crate::context::signal::Signal::Failure(
9+
$crate::error::RuntimeError::simple(
10+
"InvalidArgumentRuntimeError",
11+
format!("Expected {__expected} args but received {}", $args_ident.len()),
12+
)
13+
);
14+
}
15+
16+
// Typed extraction
17+
let mut __i: usize = 0;
18+
$(
19+
let $name: $ty = match <
20+
$ty as $crate::context::argument::TryFromArgument
21+
>::try_from_argument(& $args_ident[__i]) {
22+
Ok(v) => v,
23+
Err(sig) => return sig,
24+
};
25+
__i += 1;
26+
)+
27+
};
28+
}
29+
30+
/// Asserts there are no arguments.
31+
macro_rules! no_args {
32+
($args_ident:ident) => {
33+
if !$args_ident.is_empty() {
34+
return $crate::context::signal::Signal::Failure(
35+
$crate::error::RuntimeError::simple(
36+
"InvalidArgumentRuntimeError",
37+
format!("Expected 0 args but received {}", $args_ident.len()),
38+
)
39+
);
40+
}
41+
};
42+
}
43+
44+
pub(crate) use args;
45+
pub(crate) use no_args;

0 commit comments

Comments
 (0)