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
//! The [`Error`] and [`ErrorExt`] traits and related items.
use ;
use crate::;
pub use ErrorExt;
/// Trait for types that represent an error condition.
///
/// # Implementing
///
/// In order for [`Visitor`]s and [`Error`]s to work correctly, [`Error`]s must
/// follow several rules in their implementations of various trait methods. It
/// is recommended to use the derive macro to implement these trait methods, as
/// it will ensure these rules are followed.
///
/// It is considered a logic error to implement these trait methods such that
/// these rules are violated. Therefore, `unsafe` code must not rely on the
/// correctness of implementations of this trait.
///
/// The rules for each trait method are described below.
///
/// ## [`Debug::fmt`]
///
/// There are no additional requirements on the implementation of this method.
///
/// ## [`Display::fmt`]
///
/// If this [`Error`] value will be [skipped], its implementation of
/// [`Display::fmt`] must forward to the child, which will follow these rules
/// itself. Otherwise, the below rules must be followed.
///
/// The text written should:
///
/// * Describe the error condition.
/// * Be concise.
/// * Start with a lowercase letter, except in certain situations, such as if an
/// initialism or acronym appears at the start of the text.
///
/// The text written should not:
///
/// * Contain trailing punctuation.
/// * Contain newlines.
/// * Describe the error condition(s) that caused this error condition, if any.
/// Instead, such information should be provided by implementing
/// [`Error::accept`] such that these error conditions are visited as children
/// of this [`Error`].
///
/// ## [`Error::accept`]
///
/// If any [`Visitor`] method returns [`ControlFlow::Break`], further calls to
/// [`Visitor`] methods must not be made for the remainder of the current method
/// call. This also applies to [`VisitorExt`] methods that call [`Visitor`]
/// methods internally.
///
/// Each [`Error`] value must implement exactly one of the following behaviors.
/// Typically, the behavior is chosen on a per-`struct` and per-`enum`-variant
/// basis. The chosen behavior must match that of [`Error::details`].
///
/// ### Exactly zero children
///
/// 1. Call [`Visitor::visit`], passing `self` to `visitee` and `ctx` to `ctx`.
///
/// ### Zero or more children
///
/// 1. Call [`Visitor::visit`], passing `self` to `visitee` and `ctx` to `ctx`.
/// 2. If `self` has at least one child:
/// 1. Call [`Visitor::push`].
/// 2. Call [`VisitorExt::visit_many`] or [`VisitorExt::visit_map_many`],
/// passing the children to `errors`.
/// 3. Call [`Visitor::pop`].
///
/// ### Exactly one child, skipping `self`
///
/// 1. Call [`VisitorExt::visit_many`] or [`VisitorExt::visit_map_many`],
/// passing a single-item iterator yielding the child to `errors`.
///
/// In this situation, `self`'s [`Display::fmt`] implementation must forward to
/// the child's.
///
/// ### Exactly one child, skipping the child
///
/// 1. Call [`Visitor::visit`], passing `self` to `visitee` and `ctx` to `ctx`.
/// 2. If the child has at least one child ([`ErrorExt::has_children`] can be
/// used for detecting this case):
/// 1. Call [`Visitor::push`].
/// 2. Call [`VisitorExt::visit_children_of`] or
/// [`VisitorExt::visit_map_children_of`], passing the child to `error`.
/// 3. Call [`Visitor::pop`].
///
/// ## [`Error::details`]
///
/// Each [`Error`] value must implement exactly one of the following behaviors.
/// Typically, the behavior is chosen on a per-`struct` and per-`enum`-variant
/// basis. The chosen behavior must match that of [`Error::accept`].
///
/// ### Exactly zero children
///
/// 1. Return `self`'s details.
///
/// ### Zero or more children
///
/// 1. Return `self`'s details.
///
/// ### Exactly one child, skipping `self`
///
/// 1. If [`VisitorExt::visit_many`] is used for this child in the corresponding
/// [`Error::accept`] implementation, return the value obtained by calling
/// [`Error::details`] on the child. If [`VisitorExt::visit_map_many`] is
/// used for this child in the corresponding [`Error::accept`]
/// implementation, call [`Error::details`] on the child and pass the value
/// to the function passed to `map_details` in the
/// [`VisitorExt::visit_map_many`] call and return the resulting value.
///
/// ### Exactly one child, skipping the child
///
/// 1. Return `self`'s details.
///
/// [`Debug::fmt`]: fmt::Debug::fmt
/// [`Display::fmt`]: fmt::Display::fmt
/// [`Display`]: fmt::Display
/// [`VisitorExt::visit_children_of`]: crate::VisitorExt::visit_children_of
/// [`VisitorExt::visit_many`]: crate::VisitorExt::visit_many
/// [`VisitorExt::visit_map_children_of`]: crate::VisitorExt::visit_map_children_of
/// [`VisitorExt::visit_map_many`]: crate::VisitorExt::visit_map_many
/// [`VisitorExt`]: crate::VisitorExt
/// [skipped]: Error#exactly-one-child-skipping-self
/// Wraps a [`core::error::Error`] type so that it implements [`Error`].
///
/// This type's [`Error::accept`] implementation will visit `self` and its
/// children by calling [`core::error::Error::source`] recursively, and its
/// [`Display::fmt`] implementation directly calls `E`'s [`Display::fmt`]
/// implementation.
///
/// [`Display::fmt`]: fmt::Display::fmt
;