#[macro_export]
macro_rules! guard_unwrap {
($($input:tt)*) => {
$crate::guard!(
$($input)* else {
panic!(
"called `guard_unwrap!({})` on a mismatching value`",
stringify!($($input)*),
)
}
);
};
}
#[cfg(test)]
mod test {
#[test]
fn should_match() {
let val: Option<()> = None;
guard_unwrap!(let Option::None = val);
}
#[test]
fn should_bind() {
let val = Some(42);
guard_unwrap!(let Some(n) = val);
assert_eq!(n, 42);
}
#[test]
#[should_panic]
fn should_panic() {
let val: Option<()> = None;
guard_unwrap!(let Some(_) = val);
}
#[test]
#[should_panic(expected = "Some(_)")]
fn panic_message_should_include_pattern() {
let val: Option<()> = None;
guard_unwrap!(let Some(_) = val);
}
#[test]
#[should_panic(expected = "val")]
fn panic_message_should_include_matched_expression() {
let val: Option<()> = None;
guard_unwrap!(let Some(_) = val);
}
#[test]
#[should_panic(
expected = "called `guard_unwrap!(let Some(_) = foo(bar))` on a mismatching value"
)]
fn should_have_nice_panic_message() {
let bar = true;
fn foo(_: bool) -> Option<()> {
None
}
guard_unwrap!(let Some(_) = foo(bar));
}
}