Macro manyhow::ensure

source ·
macro_rules! ensure {
    ($cond:expr, $($bail_args:tt)*) => { ... };
    (let $pat:pat = $expr:expr, $($bail_args:tt)*) => { ... };
}
Expand description

Return early with an error, if a condition is not satisfied, matching anyhow::ensure!.

The syntax is identical to bail!, with an additional leading condition.

Additional to a boolean expression, the expression can also be a let ... = ... pattern matching, and will expand to let ... else.

ensure!(true, "an error message"; help = "with attachments");

ensure!(let Some(a) = Some(1), "error");
assert_eq!(a, 1);
let span = Span::call_site();
ensure!(false, span, "error message");
let error = syn::Error::new(Span::call_site(), "an error");
ensure!(false, error);