Macro stacked_errors::ensure_ne
source · macro_rules! ensure_ne { ($lhs:expr, $rhs:expr) => { ... }; ($lhs:expr, $rhs:expr, $msg:expr) => { ... }; }
Expand description
Asserts that two expressions are not equal to each other (with PartialEq), returning a stackable error if they are equal. Debug is also required if there is no custom message.
Has return Err(...) with a stacked_errors::Error and
attached location if the expressions are equal. A custom message can be
attached that is used as an Error::from_kind
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"
] }"#
);