#[macro_export]
macro_rules! match_method {
($method:expr, {$($body:tt)*}) => {
match_method!{@match $method, {}, $($body)*}
};
(@match $method:expr, {$($body:tt)*}, $(,)*) => {
match $method {
$($body)*
}
};
(@match $method:expr, {$($body:tt)*}, $p:literal => $e:expr, $($tail:tt)*) => {
match_method! {
@match
$method,
{
$($body)*
$crate::method_hash!($p) => $e,
},
$($tail)*
}
};
(@match $method:expr, {$($body:tt)*}, $p:literal => $e:block $($tail:tt)*) => {
match_method! {
@match
$method,
{
$($body)*
$crate::method_hash!($p) => $e,
},
$($tail)*
}
};
(@match $method:expr, {$($body:tt)*}, _ => $e:expr, $($tail:tt)*) => {
match_method! {
@match
$method,
{
$($body)*
_ => $e,
},
$($tail)*
}
};
(@match $method:expr, {$($body:tt)*}, _ => $e:expr) => {
match_method! {
@match
$method,
{
$($body)*
_ => $e,
},
}
};
}
#[cfg(test)]
mod tests {
#[test]
fn handle_constructor() {
let method_num = 1u64; let ret = match_method!(method_num, {
"Constructor" => Some(1),
_ => None,
});
assert_eq!(ret, Some(1));
}
#[test]
fn handle_unknown_method() {
let method_num = 12345u64; let ret = match_method!(method_num, {
"Constructor" => Some(1),
_ => None,
});
assert_eq!(ret, None);
}
#[test]
fn handle_user_method() {
let method_num = crate::method_hash!("TokensReceived");
let ret = match_method!(method_num, {
"Constructor" => Some(1),
"TokensReceived" => Some(2),
_ => None,
});
assert_eq!(ret, Some(2));
}
#[test]
fn handle_optional_commas() {
let method_num = crate::method_hash!("TokensReceived");
let ret = match_method!(method_num, {
"Constructor" => Some(1),
"TokensReceived" => {
Some(2)
}
_ => {
None
}
});
assert_eq!(ret, Some(2));
}
}