Add macro to generate error types with custom fields and functions

This commit is contained in:
Matthias Beyer 2016-05-17 00:14:00 +02:00
parent 1442b370bb
commit 603b4de593

View file

@ -22,12 +22,13 @@ macro_rules! generate_error_module {
} }
#[macro_export] #[macro_export]
macro_rules! generate_error_types { macro_rules! generate_custom_error_types {
( {
$name: ident, $name: ident,
$kindname: ident, $kindname: ident,
$customMemberTypeName: ident,
$($kind:ident => $string:expr),* $($kind:ident => $string:expr),*
) => { } => {
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub enum $kindname { pub enum $kindname {
$( $kind ),* $( $kind ),*
@ -62,6 +63,7 @@ macro_rules! generate_error_types {
pub struct $name { pub struct $name {
err_type: $kindname, err_type: $kindname,
cause: Option<Box<Error>>, cause: Option<Box<Error>>,
custom_data: Option<$customMemberTypeName>,
} }
impl $name { impl $name {
@ -70,6 +72,7 @@ macro_rules! generate_error_types {
$name { $name {
err_type: errtype, err_type: errtype,
cause: cause, cause: cause,
custom_data: None,
} }
} }
@ -105,6 +108,18 @@ macro_rules! generate_error_types {
} }
} }
#[macro_export]
macro_rules! generate_error_types {
(
$name: ident,
$kindname: ident,
$($kind:ident => $string:expr),*
) => {
generate_custom_error_types!($name, $kindname, members = {}, functions = {}, $($kind),*);
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {