Macro simple_error::bail

source ·
macro_rules! bail {
    ($fmt:literal) => { ... };
    ($e:expr) => { ... };
    ($fmt:literal, $($arg:tt)+) => { ... };
}
Expand description

Helper macro for returning a SimpleError constructed from either a Into<SimpleError>, a string slice, or a formatted string.

Examples

use self::simple_error::SimpleError;
use std::error::Error;
// Use with a `Into<SimpleError>`

struct ErrorSeed;

impl From<ErrorSeed> for SimpleError {
    fn from(_: ErrorSeed) -> SimpleError {
        SimpleError::new(".")
    }
}

fn bail_block_into(es: ErrorSeed) -> Result<(), SimpleError> {
    bail!(es);
}

// Use with a string slice
fn bail_block_str(s: &str) -> Result<(), SimpleError> {
    bail!(s);
}

// Use with a formatted string
fn bail_block_format(s: &str) -> Result<(), SimpleError> {
    bail!("reason: {}", s);
}

// Use with a formatted string to a boxed error
fn bail_block_format_to_box_error(s: &str) -> Result<(), Box<dyn Error>> {
    bail!("reason: {}", s);
}