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
// =============================================================================
// USES
// =============================================================================
// -----------------------------------------------------------------------------
use crateErrorKind;
// =============================================================================
// TRAITS
// =============================================================================
/// A marker trait for a type that denotes a domain of possible error kinds.
///
/// These types are expected to be very simple enums, with the following rules:
///
/// - Each field must contain a single unnamed field.
/// - The unnamed field must contain an [`ErrorKind`] type.
/// - Each type may only be used once within the enum.
///
/// # Display Formatting
///
/// The derive macro for this trait will automatically implement `Display` for
/// you. The default process is to break the variant name into words, lowercase
/// it, and then separate the words by spaces.
///
/// Then, the error domain will print alongside its value: "{domain}: {value}".
///
/// ```rust
/// # use error_kind::{ErrorDomain, ErrorKind};
/// # #[derive(Clone, Copy, Debug, ErrorKind)]
/// # enum IoErrorKind { Variant }
/// # #[derive(Clone, Copy, Debug, ErrorKind)]
/// # enum ParseErrorKind { Variant }
/// #[derive(Clone, Copy, Debug, ErrorDomain)]
/// enum ParseErrorDomain {
/// Io(IoErrorKind), // Prints "io: {0}"
/// Parse(ParseErrorKind), // Prints "parse: {0}"
/// // ...
/// }
/// ```
///
/// If you don't like a particular display format, you can change it with the
/// `#[error(...)]` attribute on a variant.
///
/// ```rust
/// # use error_kind::{ErrorDomain, ErrorKind};
/// # #[derive(Clone, Copy, Debug, ErrorKind)]
/// # enum IoErrorKind { Variant }
/// # #[derive(Clone, Copy, Debug, ErrorKind)]
/// # enum ParseErrorKind { Variant }
/// #[derive(Clone, Copy, Debug, ErrorDomain)]
/// enum ParseErrorDomain {
/// Io(IoErrorKind), // Prints "io: {0}"
/// #[error("custom: {0}")]
/// Parse(ParseErrorKind), // Prints "custom: {0}"
/// // ...
/// }
/// ```
///
/// The `{0}` will expand to the value in the unnamed field.
///
/// # Purpose
///
/// An `ErrorDomain` allows us to compose multiple different error kinds
/// together.
///
/// This allows us to maintain the following design principles:
///
/// 1. Avoid deeply-nested enums to keep error kinds small and efficient.
/// 2. Ensure that subset type can unambiguously convert into any given superset
/// `ErrorDomain`.
///
/// The second point is the *most important*, because it'll allow for very
/// ergonomic usage of your error types. Thanks to this rule, from conversion
/// for `Error<SubsetDomain>` can be implemented for `Error<SupersetDomain>`.
///
/// ```rust
/// # use error_kind::{ErrorDomain, ErrorKind};
/// #[derive(Clone, Copy, Debug, ErrorKind, PartialEq)]
/// enum IoErrorKind {
/// BadFileDescriptor,
/// FileNotFound,
/// // ...
/// }
///
/// #[derive(Clone, Copy, Debug, ErrorKind, PartialEq)]
/// enum ParseErrorKind {
/// InvalidFormat,
/// UnexpectedEndOfData,
/// // ...
/// }
///
/// #[derive(Clone, Copy, Debug, ErrorDomain, PartialEq)]
/// enum ParseErrorDomain {
/// Io(IoErrorKind),
/// Parse(ParseErrorKind),
/// // ...
/// }
///
/// // Thanks to the rules of `ErrorDomain`, this is possible!
/// assert_eq!(
/// ParseErrorDomain::Io(IoErrorKind::FileNotFound),
/// From::from(IoErrorKind::FileNotFound),
/// );
/// ```
///
/// This also works across `ErrorDomain` types as well.
///
/// ```rust
/// # use error_kind::{ErrorDomain, ErrorKind};
/// # #[derive(Clone, Copy, Debug, ErrorKind, PartialEq)]
/// # enum IoErrorKind {
/// # BadFileDescriptor,
/// # FileNotFound,
/// # // ...
/// # }
/// # #[derive(Clone, Copy, Debug, ErrorKind, PartialEq)]
/// # enum ParseErrorKind {
/// # InvalidFormat,
/// # UnexpectedEndOfData,
/// # // ...
/// # }
/// # #[derive(Clone, Copy, Debug, ErrorDomain, PartialEq)]
/// # enum ParseErrorDomain {
/// # Io(IoErrorKind),
/// # Parse(ParseErrorKind),
/// # // ...
/// # }
/// #[derive(Clone, Copy, Debug, ErrorKind, PartialEq)]
/// enum LogicErrorKind {
/// InvalidOperation,
/// TimedOut,
/// // ...
/// }
///
/// #[derive(Clone, Copy, Debug, ErrorDomain, PartialEq)]
/// #[error(domains(ParseErrorDomain))] // <- List subset domains here.
/// enum ProcessingErrorDomain {
/// Io(IoErrorKind),
/// Logic(LogicErrorKind),
/// Parse(ParseErrorKind),
/// // ...
/// }
///
/// // Thanks to the rules of `ErrorDomain`, this is possible!
/// assert_eq!(
/// ProcessingErrorDomain::Io(IoErrorKind::FileNotFound),
/// From::from(ParseErrorDomain::Io(IoErrorKind::FileNotFound)),
/// );
/// ```
///
/// > **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
// =============================================================================
// TRAIT IMPLS
// =============================================================================
// -----------------------------------------------------------------------------