heimdall_errors/
lib.rs

1//! Macros for auto impl [From<T>] for errors
2
3/// Implement the [`From`] trait for an struct with kind structure
4///
5/// # Params
6/// ```ignore
7/// implement_in_error_in_struct($struct_error, $error_type, $error_kind);
8/// ```
9/// # Example
10/// ```
11/// use heimdall_errors::implement_error;
12/// use std::env::VarError;
13///
14/// pub enum ErrorKind {
15///     Var,
16/// }
17///
18/// pub struct StructError {
19///     kind: ErrorKind,
20///     message: String,
21/// }
22///
23/// // Implement From<VarError> for StructError.
24/// implement_error!(StructError, VarError, ErrorKind::Var);
25///
26/// ```
27///
28///# Code generated
29/// The code
30/// ```ignore
31/// implement_error!(StructError, VarError, ErrorKind::Var);
32/// ```
33///
34/// generates the next code
35///
36///```ignore
37/// impl From<VarError> for StructError {
38///    fn from(err: VarError) -> Self {
39///        Self {
40///            kind: ErrorKind::Var,
41///            message: err.to_string(),
42///        }
43///     }
44/// }
45/// ```
46#[macro_export]
47macro_rules! implement_error {
48    ($struct_error:ident, $error_type: path, $error_kind: path) => {
49        impl From<$error_type> for $struct_error {
50            fn from(error: $error_type) -> $struct_error {
51                $struct_error {
52                    kind: $error_kind,
53                    message: error.to_string(),
54                }
55            }
56        }
57    };
58}
59
60/// Implement the [`From`] trait for an struct with an specific structure, with ErrorKind,
61/// and message attributes.
62///
63/// # Params
64/// ```ignore
65/// implement_in_error_in_struct($struct_error, $error_type, $error_kind);
66/// ```
67/// # Example
68/// ```
69/// use heimdall_errors::implement_error_with_kind;
70/// use std::io;
71///
72/// pub enum ErrorKind {
73///     IO(io::ErrorKind),
74/// }
75///
76/// pub struct StructError {
77///     kind: ErrorKind,
78///     message: String,
79/// }
80/// // Implement From<io::Error> for StructError.
81/// implement_error_with_kind!(StructError, io::Error, ErrorKind::IO);
82///
83/// ```
84///
85///# Code generated
86/// The code
87/// ```ignore
88/// implement_error_with_kind!(StructError, io::Error, ErrorKind::IO);
89/// ```
90///
91/// generates the next code
92///
93///```ignore
94/// impl From<io::Error> for StructError {
95///    fn from(err: io::Error) -> Self {
96///        Self {
97///            kind: ErrorKind::Io(err.kind()),
98///            message: err.to_string(),
99///        }
100///     }
101/// }
102/// ```
103#[macro_export]
104macro_rules! implement_error_with_kind {
105    ($err:ident, $t: path, $kind: path) => {
106        impl From<$t> for $err {
107            fn from(error: $t) -> $err {
108                $err {
109                    kind: $kind(error.kind().clone()),
110                    message: error.to_string(),
111                }
112            }
113        }
114    };
115}
116
117/// Implement the [`From`] trait for an enum.
118///
119/// **WARNING**: you might prefer to use [thiserror] instead of this macro.
120///
121/// # Params
122/// ```ignore
123///     implement_error_in_enum!($type_, $err_type, $enum_variant);
124/// ```
125///
126/// # Example
127///```
128/// use std::fmt::Debug;
129/// use std::io::Error;
130/// use heimdall_errors::implement_error_in_enum;
131///
132///
133/// pub (crate) enum EnumError {
134///     IO(Error)
135/// }
136///
137///
138/// // Implement From<Error> for StructError.
139/// implement_error_in_enum!(EnumError, Error, EnumError::IO);
140/// ```
141///
142///# Code generated
143/// The code
144/// ```ignore
145/// implement_error_in_enum!(EnumError, Error, EnumError::IO);
146/// ```
147///
148/// generates the next code
149///
150///```ignore
151/// impl From<Error> for EnumError {
152///    fn from(err: Error) -> Self {
153///        EnumError::Io(err)
154///     }
155/// }
156/// ```
157///
158/// [thiserror]:https://crates.io/crates/thiserror
159#[macro_export]
160macro_rules! implement_error_in_enum {
161    ($enum_error:ident, $err_type: path, $enum_variant: path) => {
162        impl From<$err_type> for $enum_error {
163            fn from(error: $err_type) -> $enum_error {
164                $enum_variant(error)
165            }
166        }
167    };
168}
169
170/// Generate the [From<T>] trait implementation for an custom enum error using [ToString] trait.
171/// # Params
172/// ```ignore
173///     implement_error_in_enum!($enum_error, $err_type, $enum_variant);
174/// ```
175/// # Example
176/// ```
177/// use std::fmt::Debug;
178/// use heimdall_errors::implement_string_error_in_enum;
179///
180///
181/// pub (crate) enum EnumError {
182///     IO(String)
183/// }
184///
185///
186/// // Implement From<Error> for StructError.
187/// implement_string_error_in_enum!(EnumError, std::io::Error, EnumError::IO);
188/// ```
189///
190///# Code generated
191/// The code
192/// ```ignore
193/// implement_string_error_in_enum!(EnumError, std::io::Error, EnumError::IO);
194/// ```
195///
196/// generates the next code
197///
198///```ignore
199/// impl From<std::io::Error> for EnumError {
200///    fn from(err: std::io::Error) -> Self {
201///        EnumError::Io(err.to_string())
202///     }
203/// }
204/// ```
205#[macro_export]
206macro_rules! implement_string_error_in_enum {
207    ($enum_error:ident, $err_type: path, $enum_variant: path) => {
208        impl From<$err_type> for $enum_error {
209            fn from(error: $err_type) -> $enum_error {
210                $enum_variant(error.to_string())
211            }
212        }
213    };
214}
215
216/// Implement the [`From`] trait for an struct with an specific structure.
217///
218/// # Params
219/// ```ignore
220/// implement_in_error_in_struct($struct_error, $err_type, $kind);
221/// ```
222/// # Example
223/// ```
224/// use heimdall_errors::implement_in_error_in_struct;
225///
226/// pub enum ErrorKind{
227///     IO,
228/// }
229/// pub struct StructError {
230///     kind: ErrorKind,
231///     message: String,
232///     source: Option<Box<dyn std::error::Error>>
233/// }
234///
235/// // Implement From<std::io::Error> for StructError.
236/// implement_in_error_in_struct!(StructError, std::io::Error, ErrorKind::IO);
237///
238/// ```
239#[macro_export]
240macro_rules! implement_in_error_in_struct {
241    ($struct_error:ident, $err_type: path, $kind: path) => {
242        impl From<$err_type> for $struct_error {
243            fn from(err: $err_type) -> Self {
244                Self {
245                    kind: $kind,
246                    message: err.to_string(),
247                    source: Some(Box::new(err)),
248                }
249            }
250        }
251    };
252}