use std::{error, fmt};
#[doc(hidden)]
#[derive(Debug)]
pub struct Err {
pub desc: &'static str,
pub fmt: String,
}
impl fmt::Display for Err {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.fmt)
}
}
impl error::Error for Err {
fn description(&self) -> &str {
self.desc
}
}
#[macro_export]
macro_rules! err {
($kind:ident, $desc:expr, $($rest:tt)*) => {
::std::io::Error::new(::std::io::ErrorKind::$kind, $crate::Err {
desc: $desc,
fmt: format!($($rest)*),
})
};
($kind:ident, $desc:expr) => {
err!($kind, $desc, "{}", $desc)
};
}
#[cfg(test)]
mod tests {
#[test]
fn test() {
let _ = err!(NotFound, "test");
let _ = err!(NotFound, "test", "x {} y", 2 + 2);
}
}