1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! 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");
/// ```
/// 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)
/// }
/// ```
/// 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(())
/// }
/// ```