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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// =============================================================================
// USES
// =============================================================================
// -----------------------------------------------------------------------------
use crate::;
// =============================================================================
// TRAITS
// =============================================================================
/// An error that contains an associated [`ErrorKind`].
///
/// Error types are expected to be fairly simple structs, usually containing
/// only a single field.
///
/// ```
/// use error_kind::{Error, ErrorKind};
///
/// #[derive(Clone, Copy, Debug, ErrorKind)]
/// enum MyErrorKind {
/// Variant,
/// }
///
/// #[derive(Debug, Error)]
/// struct MyError(MyErrorKind);
/// ```
///
/// If you plan on having many different kinds of errors within a crate, you may
/// wish to make your type generic over the kind of error.
///
/// ```
/// use error_kind::{Error, ErrorKind};
///
/// #[derive(Debug, Error)]
/// struct MyError<K: ErrorKind>(K);
/// ```
///
/// Your error kind can also be wrapped in [`envelopes`] to collect additional
/// error information.
///
/// [`envelopes`]: crate::envelopes
///
/// # Traits Implemented
///
/// The derive macro helps us out a lot here, implementing the following traits:
///
/// - [`Display`]
/// - [`ErrorContext`]
/// - [`Error`] (both `core` and `error_kind`)
/// - [`From`] (via `#[error(domains(...))]`)
/// - [`TrivialError`]
///
/// [`Display`]: core::fmt::Display
/// [`TrivialError`]: crate::TrivialError
///
/// You can disable any of these by using the `#[error(disable(...))]` attribute
/// (e.g. `#[error(disable(From, Display))]`).
///
/// You can force-hide certain trait implementations from documentation by using
/// the `#[error(hidden(...))]` attribute (e.g.
/// `#[error(hidden(TrivialError))]`).
///
/// ```rust
/// # use error_kind::{Error, ErrorKind};
/// # #[derive(Clone, Copy, Debug, ErrorKind)]
/// # enum MyErrorKind { Variant }
///
/// #[derive(Debug, Error)]
/// #[error(disable(Display, TrivialError))]
/// #[error(hidden(core::error::Error))]
/// struct NoDisplayExample(MyErrorKind);
///
/// // Though, now you have to implement `Display` yourself.
/// // This is required since `Display` is a supertrait of `Error`.
/// impl std::fmt::Display for NoDisplayExample {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// # unimplemented!()
/// // ...
/// }
/// }
/// ```
///
/// # Error Domains
///
/// If your crate defines any [`ErrorDomain`]s that you would like your error to
/// be able to convert between, register them with `#[error(domains(...))]`.
///
/// [`ErrorDomain`]: crate::ErrorDomain
///
/// **This allows for...**
///
/// - Conversion from `MyError<MyErrorKind>` to `MyError<_>` for any
/// registered [`ErrorDomain`] which contains `MyErrorKind`.
/// - Conversion from `MyError<MyErrorDomain>` to `MyError<_>` for any
/// registered [`ErrorDomain`] which contains a superset of all [`ErrorKind`]s
/// present in `MyErrorDomain`.
///
/// ```rust
/// # use error_kind::{Error, ErrorDomain, ErrorKind};
/// # #[derive(Clone, Copy, Debug, ErrorKind, PartialEq)]
/// # enum MyErrorKind1 { Variant }
/// # #[derive(Clone, Copy, Debug, ErrorKind, PartialEq)]
/// # enum MyErrorKind2 { Variant }
/// # #[derive(Clone, Copy, Debug, ErrorKind, PartialEq)]
/// # enum MyErrorKind3 { Variant }
/// #[derive(Clone, Copy, Debug, ErrorDomain, PartialEq)]
/// enum MyErrorDomain1 {
/// Kind1(MyErrorKind1),
/// Kind2(MyErrorKind2),
/// // ...
/// }
///
/// #[derive(Clone, Copy, Debug, ErrorDomain, PartialEq)]
/// #[error(domains(MyErrorDomain1))] // <- Suggest a subset domain
/// enum MyErrorDomain2 {
/// Kind1(MyErrorKind1),
/// Kind2(MyErrorKind2),
/// Kind3(MyErrorKind3),
/// // ...
/// }
///
/// #[derive(Debug, Error, Eq, PartialEq)]
/// #[error(domains(MyErrorDomain1, MyErrorDomain2))] // Because of this...
/// struct MyError<K: ErrorKind>(K);
///
/// // ... these conversions work as expected!
/// assert_eq!(
/// MyError(MyErrorDomain1::Kind1(MyErrorKind1::Variant)),
/// From::from(MyError(MyErrorKind1::Variant)),
/// );
/// assert_eq!(
/// MyError(MyErrorDomain2::Kind1(MyErrorKind1::Variant)),
/// From::from(MyError(MyErrorKind1::Variant)),
/// );
///
/// // ... and perhaps surprisingly, this also works!
/// assert_eq!(
/// MyError(MyErrorDomain2::Kind1(MyErrorKind1::Variant)),
/// From::from(MyError(MyErrorDomain1::Kind1(MyErrorKind1::Variant))),
/// );
/// ```
///
/// > **Learn More:** While you don't need to understand how this works, if you
/// > are curious to learn, see the [`IsNot`] and [`MatchInto`] traits.
///
/// [`IsNot`]: crate::details::IsNot
/// [`MatchInto`]: crate::details::MatchInto
///
/// # Trivial Errors
///
/// The [`TrivialError`] trait is used to power the [`err!`] macro.
///
/// If the error type only contains one field (the error kind), it's said to be
/// trivial. In these cases, we can easily say how to properly construct it!
///
/// ```
/// use error_kind::{Error, ErrorKind, TrivialError};
///
/// #[derive(Clone, Copy, Debug, ErrorKind)]
/// enum MyErrorKind {
/// Variant,
/// }
///
/// #[derive(Debug, Error)]
/// struct MyError(MyErrorKind);
///
/// // Allowed via the `TrivialError` trait.
/// let _ = MyError::new(MyErrorKind::Variant);
/// ```
///
/// However, if more than one field is present, the derive macro is not certain
/// that taking the `Default` for the other values is the right thing to do.
///
/// As such, it will simply avoid implementing this trait.
///
/// ```compile_fail
/// use error_kind::{Error, ErrorKind, TrivialError};
///
/// #[derive(Clone, Copy, Debug, ErrorKind)]
/// enum MyErrorKind {
/// Variant,
/// }
///
/// #[derive(Debug, Error)]
/// struct MyError(#[error_kind]MyErrorKind, u32);
///
/// // Not allowed, because of multiple fields. :(
/// let _ = MyError::new(MyErrorKind::Variant);
/// ```
///
/// In those cases, if you know better, you can implement [`TrivialError`]
/// manually.
///
/// [`err!`]: crate::err
///
/// # Recommendation: Provide an `error_kind` Function
///
/// Though users can depend on this trait to acquire the underlying error kind,
/// it's more ergonomic to simply provide a function on the error type itself.
///
/// **The `Error` derive macro *will not* implement this for you!**
///
/// ```
/// use error_kind::{Error, ErrorKind};
///
/// #[derive(Copy, Clone, Debug, ErrorKind)]
/// pub enum MyErrorKind {
/// Error1,
/// }
///
/// #[derive(Debug, Error)]
/// pub struct MyError {
/// error_kind: MyErrorKind,
/// }
///
/// impl MyError {
/// // We have to implement this manually, if we want it.
/// pub fn error_kind(&self) -> MyErrorKind {
/// self.error_kind
/// }
/// }
/// ```
///
/// # Examples
///
/// This type supports named and unnamed type constructions.
///
/// ```rust
/// # use error_kind::{Error, ErrorKind};
/// # #[derive(Clone, Copy, Debug, ErrorKind)]
/// # enum MyErrorKind { Variant }
/// #[derive(Debug, Error)]
/// struct NamedExample {
/// error_kind: MyErrorKind,
/// }
///
/// #[derive(Debug, Error)]
/// struct UnnamedExample(MyErrorKind);
/// ```
///
/// If multiple fields are present, one field must be marked as the field which
/// contains our `ErrorKind` (using `#[error_kind]`).
///
/// ```rust
/// # use error_kind::{Error, ErrorKind};
/// # #[derive(Clone, Copy, Debug, ErrorKind)]
/// # enum MyErrorKind { Variant }
/// #[derive(Debug, Error)]
/// struct MyError(u32, #[error_kind]MyErrorKind, i16);
/// ```
///
/// Your error type is allowed to be generic over the `ErrorKind`. It's highly
/// recommended that you add a bounds to the type parameter, however.
///
/// ```rust
/// # use error_kind::{Error, ErrorKind};
/// # #[derive(Clone, Copy, Debug, ErrorKind)]
/// # enum MyErrorKind { Variant }
/// #[derive(Debug, Error)]
/// struct MyError<K: ErrorKind> {
/// field_a: u32,
/// #[error_kind]
/// field_b: K,
/// field_c: i16,
/// }
/// ```