[][src]Macro snafu::ensure

macro_rules! ensure {
    ($predicate:expr, $context_selector:expr) => { ... };
}

Ensure a condition is true. If it is not, return from the function with an error.

use snafu::{ensure, Snafu};

#[derive(Debug, Snafu)]
enum Error {
    InvalidUser { user_id: i32 },
}

fn example(user_id: i32) -> Result<(), Error> {
    ensure!(user_id > 0, InvalidUser { user_id });
    // After this point, we know that `user_id` is positive.
    let user_id = user_id as u32;
    Ok(())
}