From 603b4de5939e376660fdef556b77168d868e4f79 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 17 May 2016 00:14:00 +0200 Subject: [PATCH] Add macro to generate error types with custom fields and functions --- libimagerror/src/error_gen.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/libimagerror/src/error_gen.rs b/libimagerror/src/error_gen.rs index e88da514..e32feedb 100644 --- a/libimagerror/src/error_gen.rs +++ b/libimagerror/src/error_gen.rs @@ -22,12 +22,13 @@ macro_rules! generate_error_module { } #[macro_export] -macro_rules! generate_error_types { - ( +macro_rules! generate_custom_error_types { + { $name: ident, $kindname: ident, + $customMemberTypeName: ident, $($kind:ident => $string:expr),* - ) => { + } => { #[derive(Clone, Copy, Debug, PartialEq)] pub enum $kindname { $( $kind ),* @@ -62,6 +63,7 @@ macro_rules! generate_error_types { pub struct $name { err_type: $kindname, cause: Option>, + custom_data: Option<$customMemberTypeName>, } impl $name { @@ -70,6 +72,7 @@ macro_rules! generate_error_types { $name { err_type: errtype, 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)] mod test {