#![doc(html_root_uri = "https://xion.github.io/rust-error-derive")]
#[macro_use] mod util;
#[macro_export]
macro_rules! ErrorFrom {
(@expand $name:ident ($($var_names:ident($var_tys:ty),)*)) => {
$(
impl ::std::convert::From<$var_tys> for $name {
fn from(v: $var_tys) -> $name {
$name::$var_names(v)
}
}
)*
};
(() $(pub)* enum $name:ident { $($body:tt)* }) => {
enum_derive_util! {
@collect_unary_variants
(ErrorFrom { @expand $name }),
($($body)*,) -> ()
}
};
}
#[macro_export]
macro_rules! ErrorDisplay {
(@expand $name:ident ($($var_names:ident($var_tys:ty),)*)) => {
impl ::std::fmt::Display for $name {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
try!(write!(fmt, "{}", ::std::error::Error::description(self)));
if let Some(cause) = ::std::error::Error::cause(self) {
try!(write!(fmt, "\n-- caused by {}", cause));
}
Ok(())
}
}
};
(() $(pub)* enum $name:ident { $($body:tt)* }) => {
enum_derive_util! {
@collect_unary_variants
(ErrorDisplay { @expand $name }),
($($body)*,) -> ()
}
};
}
#[macro_export]
macro_rules! Error {
(@expand $desc:expr, $name:ident ($($var_names:ident($var_tys:ty),)*)) => {
impl ::std::error::Error for $name {
fn description(&self) -> &str {
$desc
}
fn cause(&self) -> Option<&::std::error::Error> {
match *self {
$(
$name::$var_names(ref c) => Some(c),
)*
}
}
}
};
(($desc:expr) $(pub)* enum $name:ident { $($body:tt)* }) => {
enum_derive_util! {
@collect_unary_variants
(Error { @expand $desc, $name }),
($($body)*,) -> ()
}
};
}