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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! Macros for auto impl [From<T>] for errors

/// Generate the [From<T>] trait implementation for an custom error struct with an specific
/// structure, with ErrorKind, and message attributes;
///
/// * Param 1: the custom error type,
/// * Param 2: the error type you wants use in From trait,
/// * Param 3: the corresponding ErrorKind.
///
/// # Example
/// ```
/// use std::fmt::{Display, Formatter, Result, Debug};
/// use std::io::Error;
/// use std::env::VarError;
/// use heimdall_errors::implement_error;
///
/// // First, create your ErrorKind
///#[derive(Debug, PartialEq, Copy, Clone)]
/// pub (crate) enum ErrorKind {
///     Io,
///     Env
/// }
///
/// impl ToString for ErrorKind {
///     fn to_string(&self) -> String {
///         format!("{:?}", &self)
///     }
/// }
///
/// // Next, create your Error struct
/// #[derive(Debug, PartialEq, Clone)]
/// pub (crate) struct MyErrorType {
///     kind: ErrorKind,
///     message: String
/// }
///
/// // Implement the Display trait
/// impl Display for MyErrorType {
///     fn fmt(&self, f: &mut Formatter<'_>) -> Result {
///         write!(
///             f,
///             "kind: {} message: {}",
///             self.kind.to_string(),
///             self.message
///         )
///     }
/// }
///
/// // Generate implementations
/// implement_error!(MyErrorType, std::io::Error, ErrorKind::Io);
/// implement_error!(MyErrorType, VarError, ErrorKind::Env);
/// ```
///
///# Code generated
/// The code
/// ```ignore
/// implement_error!(MyErrorType, std::io::Error, ErrorKind::Io);
/// ```
///
/// generates the next code
///
///```ignore
/// impl From<std::io::Error> for MyErrorType {
///    fn from(err: std::io::Error) -> Self {
///        Self {
///            kind: ErrorKind::Io,
///            message: err.to_string(),
///        }
///     }
/// }
/// ```
#[macro_export]
macro_rules! implement_error {
    ($err:ident, $t: path, $kind: path) => {
        impl From<$t> for $err {
            fn from(error: $t) -> $err {
                $err {
                    kind: $kind,
                    message: error.to_string(),
                }
            }
        }
    };
}

/// Generate the [From<T>] trait implementation for an custom error struct with an specific
/// structure, with ErrorKind, and message attributes. Use only if you want recovery the ErrorKind.
///
/// **Warning**: This macro use the `kind()` method. Make sure that the error implemented this method.
///
/// * Param 1: the custom error type,
/// * Param 2: the error type you wants use in From trait,
/// * Param 3: the corresponding ErrorKind.
///
/// # Example
/// ```
/// use std::fmt::{Display, Formatter, Result, Debug};
/// use std::io::Error;
/// use heimdall_errors::implement_error_with_kind;
///
/// // First, create your ErrorKind
///#[derive(Debug, PartialEq, Clone)]
/// pub (crate) enum ErrorKind {
///     Io(std::io::ErrorKind),
/// }
///
/// impl ToString for ErrorKind {
///     fn to_string(&self) -> String {
///         format!("{:?}", &self)
///     }
/// }
///
/// // Next, create your Error struct
/// #[derive(Debug, PartialEq, Clone)]
/// pub (crate) struct MyErrorType {
///     kind: ErrorKind,
///     message: String
/// }
///
/// // Implement the Display trait
/// impl Display for MyErrorType {
///     fn fmt(&self, f: &mut Formatter<'_>) -> Result {
///         write!(
///             f,
///             "kind: {} message: {}",
///             self.kind.to_string(),
///             self.message
///         )
///     }
/// }
///
/// // Generate implementation
/// implement_error_with_kind!(MyErrorType, std::io::Error, ErrorKind::Io);
/// ```
///
///# Code generated
/// The code
/// ```ignore
/// implement_error_with_kind!(MyErrorType, std::io::Error, ErrorKind::Io);
/// ```
///
/// generates the next code
///
///```ignore
/// impl From<std::io::Error> for MyErrorType {
///    fn from(err: std::io::Error) -> Self {
///        Self {
///            kind: ErrorKind::Io(err.kind()),
///            message: err.to_string(),
///        }
///     }
/// }
/// ```
#[macro_export]
macro_rules! implement_error_with_kind {
    ($err:ident, $t: path, $kind: path) => {
        impl From<$t> for $err {
            fn from(error: $t) -> $err {
                $err {
                    kind: $kind(error.kind().clone()),
                    message: error.to_string(),
                }
            }
        }
    };
}