#[macro_export]
macro_rules! error {
($value: expr) => {
std::io::Error::new(
std::io::ErrorKind::Other,
$value
)
};
($kind: ident, $value: expr) => {
std::io::Error::new(
std::io::ErrorKind::$kind,
$value
)
};
($value: literal, $($arg:tt)+) => {
std::io::Error::new(
std::io::ErrorKind::Other,
format!($value, $($arg)+)
)
};
($kind: ident, $value: literal, $($arg:tt)+) => {
std::io::Error::new(
std::io::ErrorKind::$kind,
format!($value, $($arg)+)
)
};
(as $kind: ident) => {
<std::io::Error as std::convert::From<std::io::ErrorKind>>::from(std::io::ErrorKind::$kind)
};
() => {
<std::io::Error as std::convert::From<std::io::ErrorKind>>::from(std::io::ErrorKind::Other)
};
}
#[test]
fn test_error() {
let err = error!("test value");
assert_eq!(err.kind(), std::io::ErrorKind::Other);
assert_eq!(err.to_string().as_str(), "test value");
let err = error!(NotConnected, "test value 2");
assert_eq!(err.kind(), std::io::ErrorKind::NotConnected);
assert_eq!(err.to_string().as_str(), "test value 2");
let err = error!(as TimedOut);
assert_eq!(err.kind(), std::io::ErrorKind::TimedOut);
assert_eq!(err.to_string().as_str(), "timed out");
let err = error!();
assert_eq!(err.kind(), std::io::ErrorKind::Other);
assert_eq!(err.to_string().as_str(), "other error");
let err = error!("hello {}", "world");
assert_eq!(err.kind(), std::io::ErrorKind::Other);
assert_eq!(err.to_string().as_str(), "hello world");
let err = error!(TimedOut, "the answer: {}", 42);
assert_eq!(err.kind(), std::io::ErrorKind::TimedOut);
assert_eq!(err.to_string().as_str(), "the answer: 42");
let err = error!("hello {}. value={}", "world", 69);
assert_eq!(err.kind(), std::io::ErrorKind::Other);
assert_eq!(err.to_string().as_str(), "hello world. value=69");
}