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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/// Asserts that a boolean expression is `true` at runtime, returning a
/// stackable error otherwise.
///
/// Has `return Err(...)` with a [stacked_errors::Error](crate::Error) and
/// attached location if the expression is false. An custom message can be
/// attached that is used as a [StackableErr](crate::StackableErr) argument.
///
/// ```
/// use stacked_errors::{ensure, Result, StackableErr};
///
/// fn ex(val0: bool, val1: bool) -> Result<()> {
///     ensure!(true);
///
///     ensure!(val0);
///
///     ensure!(val1, format!("val1 was {}", val1));
///
///     Ok(())
/// }
///
/// ex(true, true).unwrap();
///
/// assert_eq!(
///     format!("{}", ex(false, true).unwrap_err()),
///     r#"Error { stack: [
/// Location { file: "src/ensure.rs", line: 10, col: 5 },
/// ensure(val0) -> assertion failed
/// ] }"#
/// );
///
/// assert_eq!(
///     format!("{}", ex(true, false).unwrap_err()),
///     r#"Error { stack: [
/// Location { file: "src/ensure.rs", line: 12, col: 5 },
/// val1 was false
/// ] }"#
/// );
/// ```
#[macro_export]
macro_rules! ensure {
    ($expr:expr) => {
        if !$expr {
            return Err($crate::Error::from_kind($crate::__private::concat!(
                "ensure(",
                $crate::__private::stringify!($expr),
                ") -> assertion failed"
            )))
        }
    };
    ($expr:expr, $msg:expr) => {
        if !$expr {
            return Err($crate::Error::from_kind($msg))
        }
    };
}
/// Asserts that two expressions are equal to each other (with [PartialEq]),
/// returning a stackable error if they are not.
///
/// Has `return Err(...)` with a [stacked_errors::Error](crate::Error) and
/// attached location if the expressions are unequal. A custom message can be
/// attached that is used as a [StackableErr](crate::StackableErr) argument.
///
/// ```
/// use stacked_errors::{ensure_eq, Result, StackableErr};
///
/// fn ex(val0: u8, val1: &str) -> Result<()> {
///     ensure_eq!(42, 42);
///
///     ensure_eq!(8, val0);
///
///     ensure_eq!("test", val1, format!("val1 was \"{}\"", val1));
///
///     Ok(())
/// }
///
/// ex(8, "test").unwrap();
///
/// assert_eq!(
///     format!("{}", ex(0, "test").unwrap_err()),
///     r#"Error { stack: [
/// Location { file: "src/ensure.rs", line: 10, col: 5 },
/// ensure_eq(
///  lhs: 8
///  rhs: 0
/// ) -> equality assertion failed
/// ] }"#
/// );
///
/// assert_eq!(
///     format!("{}", ex(8, "other").unwrap_err()),
///     r#"Error { stack: [
/// Location { file: "src/ensure.rs", line: 12, col: 5 },
/// val1 was "other"
/// ] }"#
/// );
/// ```
#[macro_export]
macro_rules! ensure_eq {
    ($lhs:expr, $rhs:expr) => {
        if $lhs != $rhs {
            return Err($crate::Error::from_kind($crate::__private::format!(
                "ensure_eq(\n lhs: {:?}\n rhs: {:?}\n) -> equality assertion failed",
                $lhs,
                $rhs,
            )))
        }
    };
    ($lhs:expr, $rhs:expr, $msg:expr) => {
        if $lhs != $rhs {
            return Err($crate::Error::from_kind($msg))
        }
    };
}
/// Asserts that two expressions are not equal to each other (with [PartialEq]),
/// returning a stackable error if they are equal.
///
/// Has `return Err(...)` with a [stacked_errors::Error](crate::Error) and
/// attached location if the expressions are equal. A custom message can be
/// attached that is used as a [StackableErr](crate::StackableErr) argument.
///
/// ```
/// use stacked_errors::{ensure_ne, Result, StackableErr};
///
/// fn ex(val0: u8, val1: &str) -> Result<()> {
///     ensure_ne!(42, 8);
///
///     ensure_ne!(8, val0);
///
///     ensure_ne!("test", val1, format!("val1 was \"{}\"", val1));
///
///     Ok(())
/// }
///
/// ex(0, "other").unwrap();
///
/// assert_eq!(
///     format!("{}", ex(8, "other").unwrap_err()),
///     r#"Error { stack: [
/// Location { file: "src/ensure.rs", line: 10, col: 5 },
/// ensure_ne(
///  lhs: 8
///  rhs: 8
/// ) -> inequality assertion failed
/// ] }"#
/// );
///
/// assert_eq!(
///     format!("{}", ex(0, "test").unwrap_err()),
///     r#"Error { stack: [
/// Location { file: "src/ensure.rs", line: 12, col: 5 },
/// val1 was "test"
/// ] }"#
/// );
/// ```
#[macro_export]
macro_rules! ensure_ne {
    ($lhs:expr, $rhs:expr) => {
        if $lhs == $rhs {
            return Err($crate::Error::from_kind($crate::__private::format!(
                "ensure_ne(\n lhs: {:?}\n rhs: {:?}\n) -> inequality assertion failed",
                $lhs,
                $rhs,
            )))
        }
    };
    ($lhs:expr, $rhs:expr, $msg:expr) => {
        if $lhs == $rhs {
            return Err($crate::Error::from_kind($msg))
        }
    };
}