Merge pull request #414 from matthiasbeyer/libimagerror/custom-membersntypes

Libimagerror/custom membersntypes
This commit is contained in:
Matthias Beyer 2016-05-23 20:01:33 +02:00
commit d6f3c90880
2 changed files with 59 additions and 5 deletions

View file

@ -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<Box<Error>>,
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,22 @@ macro_rules! generate_error_types {
}
}
#[macro_export]
macro_rules! generate_error_types {
(
$name: ident,
$kindname: ident,
$($kind:ident => $string:expr),*
) => {
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Copy)]
pub struct SomeNotExistingTypeWithATypeNameNoOneWillEverChoose {}
generate_custom_error_types!($name, $kindname,
SomeNotExistingTypeWithATypeNameNoOneWillEverChoose,
$($kind => $string),*);
}
}
#[cfg(test)]
mod test {
@ -114,6 +133,36 @@ mod test {
TestErrorKindB => "testerrorkind B");
);
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Copy)]
pub struct CustomData {
pub test: i32,
pub othr: i64,
}
generate_error_imports!();
generate_custom_error_types!(CustomTestError, CustomTestErrorKind,
CustomData,
CustomErrorKindA => "customerrorkind a",
CustomErrorKindB => "customerrorkind B");
impl CustomTestError {
pub fn test(&self) -> i32 {
match self.custom_data {
Some(t) => t.test,
None => 0,
}
}
pub fn bar(&self) -> i64 {
match self.custom_data {
Some(t) => t.othr,
None => 0,
}
}
}
#[test]
fn test_a() {
use self::error::{TestError, TestErrorKind};

View file

@ -1,7 +1,10 @@
generate_error_imports!();
use std::convert::From;
generate_error_types!(StoreError, StoreErrorKind,
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Copy)]
pub struct CustomErrorData {}
generate_custom_error_types!(StoreError, StoreErrorKind, CustomErrorData,
ConfigurationError => "Store Configuration Error",
FileError => "File Error",
IoError => "IO Error",
@ -31,7 +34,7 @@ generate_error_types!(StoreError, StoreErrorKind,
EncodingError => "Encoding error"
);
generate_error_types!(ParserError, ParserErrorKind,
generate_custom_error_types!(ParserError, ParserErrorKind, CustomErrorData,
TOMLParserErrors => "Several TOML-Parser-Errors",
MissingMainSection => "Missing main section",
MissingVersionInfo => "Missing version information in main section",
@ -44,6 +47,7 @@ impl From<ParserError> for StoreError {
StoreError {
err_type: StoreErrorKind::MalformedEntry,
cause: Some(Box::new(ps)),
custom_data: None,
}
}
}
@ -53,6 +57,7 @@ impl From<::std::io::Error> for StoreError {
StoreError {
err_type: StoreErrorKind::IoError,
cause: Some(Box::new(ps)),
custom_data: None,
}
}
}