Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

[WIP] Add derive block for deriving additional traits on error types #163

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Next Next commit
Add support for customizing traits derived (broken currently)
pengowen123 committed Oct 18, 2017
commit de82983e60991822e4195eb6d4a3001d2d6ffc62
43 changes: 32 additions & 11 deletions src/error_chain.rs
Original file line number Diff line number Diff line change
@@ -2,15 +2,20 @@
#[doc(hidden)]
#[macro_export]
macro_rules! impl_error_chain_processed {
// Default values for `types`.
// Default values for `types` and `derive`.
(
// FIXME does not work if either types or derive isn't empty
types {}
derive {}
$( $rest: tt )*
) => {
impl_error_chain_processed! {
types {
Error, ErrorKind, ResultExt, Result;
}
derive {
Debug;
}
$( $rest )*
}
};
@@ -22,6 +27,7 @@ macro_rules! impl_error_chain_processed {
}
$( $rest: tt )*
) => {
log_syntax!(rest: $($rest)*);
impl_error_chain_processed! {
types {
$error_name, $error_kind_name,
@@ -40,6 +46,10 @@ macro_rules! impl_error_chain_processed {
$result_ext_name:ident;
}

derive {
$($trait:ident),*;
}

links {
$( $link_variant:ident ( $link_error_path:path, $link_kind_path:path )
$( #[$meta_links:meta] )*; ) *
@@ -350,48 +360,59 @@ macro_rules! impl_error_chain_processed {
#[macro_export]
macro_rules! error_chain_processing {
(
({}, $b:tt, $c:tt, $d:tt)
({}, $b:tt, $c:tt, $d:tt, $e:tt)
types $content:tt
$( $tail:tt )*
) => {
error_chain_processing! {
($content, $b, $c, $d)
($content, $b, $c, $d, $e)
$($tail)*
}
};
(
($a:tt, {}, $c:tt, $d:tt, $e:tt)
derive $content:tt
$( $tail:tt )*
) => {
error_chain_processing! {
($a, $content, $c, $d, $e)
$($tail)*
}
};
(
($a:tt, {}, $c:tt, $d:tt)
($a:tt, $b:tt, {}, $d:tt, $e:tt)
links $content:tt
$( $tail:tt )*
) => {
error_chain_processing! {
($a, $content, $c, $d)
($a, $b, $content, $d, $e)
$($tail)*
}
};
(
($a:tt, $b:tt, {}, $d:tt)
($a:tt, $b:tt, $c:tt, {}, $e:tt)
foreign_links $content:tt
$( $tail:tt )*
) => {
error_chain_processing! {
($a, $b, $content, $d)
($a, $b, $c, $content, $e)
$($tail)*
}
};
(
($a:tt, $b:tt, $c:tt, {})
($a:tt, $b:tt, $c:tt, $d:tt, {})
errors $content:tt
$( $tail:tt )*
) => {
error_chain_processing! {
($a, $b, $c, $content)
($a, $b, $c, $d, $content)
$($tail)*
}
};
( ($a:tt, $b:tt, $c:tt, $d:tt) ) => {
( ($a:tt, $b:tt, $c:tt, $d:tt, $e:tt) ) => {
impl_error_chain_processed! {
types $a
derive $e
links $b
foreign_links $c
errors $d
@@ -404,7 +425,7 @@ macro_rules! error_chain_processing {
macro_rules! error_chain {
( $( $block_name:ident { $( $block_content:tt )* } )* ) => {
error_chain_processing! {
({}, {}, {}, {})
({}, {}, {}, {}, {})
$($block_name { $( $block_content )* })*
}
};