toobad 0.1.0

A simple, intuitive error handling library
Documentation
//! Convenience macros for error creation and early return.
//!
//! This module provides three macros that simplify common error handling patterns.
//! The [`toobad!`] macro creates errors from formatted strings, [`bail!`] provides
//! early return with an error, and [`ensure!`] conditionally bails on validation failures.
//!
//! These macros integrate with the crate's [`Error`] type and work seamlessly with
//! the `?` operator and [`Context`] trait.

/// Create an error from a formatted message.
///
/// This macro accepts format string arguments and any other values that implement
/// the `Display` trait, constructing a message that becomes the error content.
///
/// # Example
///
/// ```rust
/// let err = toobad!("connection failed to {}", "localhost");
/// ```
#[macro_export]
macro_rules! toobad {
    ($($arg:tt)*) => {
        $crate::Error::msg(format!($($arg)*))
    };
}

/// Return early with an error from the current function.
///
/// This macro expands to `return Err(toobad!(...))`, providing a convenient way
/// to bail out of a function when an error condition is detected.
///
/// # Example
///
/// ```rust
/// fn divide(a: i32, b: i32) -> toobad::Result<i32> {
///     if b == 0 {
///         bail!("division by zero");
///     }
///     Ok(a / b)
/// }
/// ```
#[macro_export]
macro_rules! bail {
    ($($arg:tt)*) => {
        return Err($crate::toobad!($($arg)*))
    };
}

/// Bail if a condition is false.
///
/// This macro evaluates a boolean expression and returns an error early if the
/// condition evaluates to false. This is useful for validation checks.
///
/// # Example
///
/// ```rust
/// fn process(name: &str) -> toobad::Result<()> {
///     ensure!(!name.is_empty(), "name cannot be empty");
///     Ok(())
/// }
/// ```
#[macro_export]
macro_rules! ensure {
    ($cond:expr, $($arg:tt)*) => {
        if !$cond {
            $crate::bail!($($arg)*)
        }
    };
}