mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-09-28 05:21:34 +00:00
Merge pull request #3 from dtolnay/literal
Accept string literal in bail macro
This commit is contained in:
commit
dcb6b794cc
@ -283,6 +283,9 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! bail {
|
||||
($msg:literal $(,)?) => {
|
||||
return std::result::Result::Err($crate::anyhow!($msg));
|
||||
};
|
||||
($err:expr $(,)?) => {
|
||||
return std::result::Result::Err(std::convert::From::from($err));
|
||||
};
|
||||
|
28
tests/macros.rs
Normal file
28
tests/macros.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use anyhow::{bail, Result};
|
||||
use std::io;
|
||||
|
||||
fn bail_literal() -> Result<()> {
|
||||
bail!("oh no!");
|
||||
}
|
||||
|
||||
fn bail_fmt() -> Result<()> {
|
||||
bail!("{} {}!", "oh", "no");
|
||||
}
|
||||
|
||||
fn bail_error() -> Result<()> {
|
||||
bail!(io::Error::new(io::ErrorKind::Other, "oh no!"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_messages() {
|
||||
assert_eq!("oh no!", bail_literal().unwrap_err().to_string());
|
||||
assert_eq!("oh no!", bail_fmt().unwrap_err().to_string());
|
||||
assert_eq!("oh no!", bail_error().unwrap_err().to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_downcast() {
|
||||
assert_eq!("oh no!", bail_literal().unwrap_err().downcast::<&str>().unwrap());
|
||||
assert_eq!("oh no!", bail_fmt().unwrap_err().downcast::<String>().unwrap());
|
||||
assert_eq!("oh no!", bail_error().unwrap_err().downcast::<io::Error>().unwrap().to_string());
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user