Macro snafu::ensure_whatever

source ·
macro_rules! ensure_whatever {
    ($predicate:expr, $fmt:literal$(, $($arg:expr),* $(,)?)?) => { ... };
}
Expand description

Ensure a condition is true. If it is not, return a stringly-typed error message.

This can be used with the provided Whatever type or with a custom error type that uses snafu(whatever).

§Examples

use snafu::prelude::*;

#[derive(Debug, Snafu)]
#[snafu(whatever, display("Error was: {message}"))]
struct Error {
    message: String,
}
type Result<T, E = Error> = std::result::Result<T, E>;

fn get_bank_account_balance(account_id: &str) -> Result<u8> {
    ensure_whatever!(
        moon_is_rising(),
        "We are recalibrating the dynamos for account {}, sorry",
        account_id,
    );

    Ok(100)
}