1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![allow(unused_imports)]

#[macro_export]
macro_rules! throws {
    ($($(#[$attr:meta])* $name:ident = $($case:ident($error:path)),*);+) => {
        throws!($($(#[$attr])* () $name = $($case($error)),*);+);
    };
    ($($(#[$attr:meta])* pub $name:ident = $($case:ident($error:path)),*);+) => {
        throws!($($(#[$attr])* (pub) $name = $($case($error)),*);+);
    };
    ($($(#[$attr:meta])* pub($($restriction:tt)+) $name:ident = $($case:ident($error:path)),*);+) => {
        throws!($($(#[$attr])* (pub($($restriction)+)) $name = $($case($error)),*);+);
    };
    ($($(#[$attr:meta])* ($($vis:tt)*) $name:ident = $($case:ident($error:path)),*);+) => {
    $(
        $(#[$attr])*
        #[derive(Debug)]
        $($vis)* enum $name { $($case($error)),* }

        impl ::std::fmt::Display for $name {
            fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                match *self {
                    $($name::$case(ref err) => write!(formatter, stringify!($case error: {}), err)),*
                }
            }
        }

        impl ::std::error::Error for $name {
            fn description(&self) -> &str {
                match *self {
                    $($name::$case(ref err) => err.description()),*
                }
            }

            fn cause(&self) -> Option<&::std::error::Error> {
                match *self {
                    $($name::$case(ref err) => Some(err)),*
                }
            }
        }

        $(
        impl ::std::convert::From<$error> for $name {
            fn from(err: $error) -> $name {
                $name::$case(err)
            }
        }
        )*
    )+
    };
}

#[cfg(test)]
mod tests {
    use std::io;
    use std::num::{ParseIntError, ParseFloatError};

    throws!(SingleIoError = Io(io::Error));

    throws!(#[derive(PartialEq, Eq)] SingleParseIntError = Parse(ParseIntError));

    throws!(MultipleError = Io(io::Error), ParseFloat(ParseFloatError), Single(SingleIoError));

    throws! {
        IoError = Io(io::Error);
        ParseError = Parse(ParseIntError)
    }

    mod error {
        use super::*;

        throws!(pub PubError = Io(io::Error), Parse(ParseIntError));

        throws!(pub(crate) PubCrateError = Io(io::Error), Parse(ParseIntError));

        throws!(
            #[derive(PartialEq, Eq, Clone)]
            #[allow(missing_docs)]
            pub(crate) AllFeaturesError = ParseInt(ParseIntError), ParseFloat(ParseFloatError)
        );

        throws! {
            () PrivateIoError = Io(io::Error);
            #[derive(Clone)]
            (pub) PubParseError = Parse(ParseIntError)
        }
    }

    #[test]
    fn create_from_error() {
        let error = From::from(io::Error::from(io::ErrorKind::NotFound));
        assert!(match error {
            SingleIoError::Io(_) => true,
        });

        let error = "not int"
            .parse::<i32>()
            .map_err(SingleParseIntError::from)
            .unwrap_err();
        assert!(match error {
            SingleParseIntError::Parse(_) => true,
        });
        assert_eq!(error, error);

        let error = From::from(io::Error::from(io::ErrorKind::NotFound));
        assert!(match error {
            MultipleError::Io(_) => true,
            _ => false,
        });

        let error = SingleIoError::from(io::Error::from(io::ErrorKind::NotFound));
        let error = From::from(error);
        assert!(match error {
            MultipleError::Single(_) => true,
            _ => false,
        });

        let error = From::from("not float".parse::<f32>().unwrap_err());
        assert!(match error {
            MultipleError::ParseFloat(_) => true,
            _ => false,
        });

        let error = From::from(io::Error::from(io::ErrorKind::NotFound));
        assert!(match error {
            error::PubError::Io(_) => true,
            _ => false,
        });

        let error = From::from("not int".parse::<i32>().unwrap_err());
        assert!(match error {
            error::PubCrateError::Parse(_) => true,
            _ => false,
        });

        let error = From::from("not float".parse::<f32>().unwrap_err());
        assert!(match error {
            error::AllFeaturesError::ParseFloat(_) => true,
            _ => false,
        });
        assert_eq!(error, error.clone());

        let error = From::from(io::Error::from(io::ErrorKind::NotFound));
        assert!(match error {
            IoError::Io(_) => true,
        });
    }
}