script::python::error

Macro ensure

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

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

ยงExamples

use snafu::prelude::*;

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

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