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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! A simple library for working with composable errors.
//!
//! # Examples
//!
//! Basic usage:
//!
//! ```
//! use std::{
//! num::{IntErrorKind, ParseIntError},
//! iter::Sum,
//! str::FromStr
//! };
//!
//! use fused_error::{Accumulator, FusedResult, IteratorExt};
//!
//! /// Take an iterator of textual data, adding up all of the parsed numbers.
//! ///
//! /// Unlike the standard way of returning a `Result<N, N::Err>`, this doesn't
//! /// short-circuit, it keeps track of the current sum, and reports any
//! /// further diagnostics past the first failure.
//! fn calculate_sum<N, E, I>(iter: I) -> FusedResult<N, N::Err>
//! where
//! N: FromStr + Sum,
//! E: AsRef<str>,
//! I: IntoIterator<Item = E>,
//! {
//! // Error accumulators collect errors to defer handling them, providing
//! // more holistic diagnostics:
//! let mut acc = Accumulator::new();
//! let sum = iter
//! .into_iter()
//! .map(|item| item.as_ref().parse::<N>())
//! // `fused_error` adds certain methods to iterators; no more
//! // disrupting iterator chains and `collect` hells for results!
//! .accumulate(&mut acc)
//! .sum();
//! // fused results let you easily pass around error accumulators and
//! // are perfect for cases where a yielded "ok" value and an error case
//! // aren't mutually exclusive.
//! FusedResult::new(sum, acc)
//! }
//!
//! let result: FusedResult<i32, _> = calculate_sum(["1", "2", "3", "4"]);
//! assert_eq!(result.value(), &10);
//! assert_eq!(result.errors(), []);
//!
//! let result: FusedResult<i8, _> = calculate_sum(["", "-129", "foo", "128"]);
//! assert_eq!(result.value(), &0);
//! assert_eq!(
//! result
//! .errors()
//! .into_iter()
//! .map(|err| err.kind().clone())
//! .collect::<Vec<_>>(),
//! [
//! IntErrorKind::Empty,
//! IntErrorKind::NegOverflow,
//! IntErrorKind::InvalidDigit,
//! IntErrorKind::PosOverflow,
//! ],
//! );
//!
//! let result: FusedResult<u8, _> = calculate_sum(["-1", "", "0", "1"]);
//! assert_eq!(result.value(), &1);
//! assert_eq!(
//! result
//! .errors()
//! .into_iter()
//! .map(|err| err.kind().clone())
//! .collect::<Vec<_>>(),
//! [IntErrorKind::InvalidDigit, IntErrorKind::Empty],
//! );
//! ```
//!
//! # Features
//!
//! So far, there is only one opt-in feature: `syn`. Enabling this feature
//! implements [`FusedError`] on [`syn::Error`], as that was one of the main
//! motivations for creating this library.
//!
//! # Motivation
//!
//! [`syn`] already has great composable errors that you combine with
//! [`syn::Error::combine`]. Also, [`darling`] has a great system that was the
//! primary inspiration for error accumulators and their drop mechanic. However,
//! of course, [`darling`]'s accumulators are only to be used with [`darling`]
//! errors and their accumulator API is far more limited to reflect this.
//!
//! The original use case for this crate, deferring and collecting multiple
//! errors, is primarily helpful in parsing: the more diagnostics you can
//! provide in one pass limits the need of frequently changing something,
//! building, fixing the one error, and trying again.
//!
//! [`darling`]: https://docs.rs/darling/latest/darling/
// fused_error types in rustdoc of other crates get linked to here
pub use Accumulated;
pub use Accumulator;
pub use IteratorExt;
pub use ;
/// Interface for error types that can store multiple error messages within one
/// instance.
///
/// Instead of making your own newtype to implement `FusedError` on a remote
/// error type, consider using [`Accumulated`] instead.
///
/// # Examples
///
/// ```
/// use fused_error::{Accumulated, FusedError};
///
/// struct Error<'a> {
/// messages: Vec<&'a str>,
/// }
///
/// impl FusedError for Error<'_> {
/// fn combine(&mut self, mut other: Self) {
/// self.messages.append(&mut other.messages);
/// }
/// }
///
/// let mut err1 = Error { messages: vec!["foo"] };
/// let err2 = Error { messages: vec!["bar", "baz"] };
///
/// err1.combine(err2);
/// assert_eq!(err1.messages, ["foo", "bar", "baz"]);
/// ```
/// A trait for splitting a result value into its "ok" and "error" parts.
///
/// # Safety
///
/// When implementing [`into_result_parts`], it is guaranteed that it **can
/// not** return [`None`] for both the "ok" and "error" parts. In other words,
/// it's only valid to return one of the following combinations:
///
/// * `(Some(T), Some(E))`
/// * `(Some(T), None)`
/// * `(None, Some(E))`
///
/// [`into_result_parts`]: IntoResultParts::into_result_parts
pub unsafe
unsafe
/// A prelude to import the main items exported by this library:
///
/// * [`Accumulator<E>`]
/// * [`FusedError`]
/// * [`FusedResult<T, E>`](FusedResult)
/// * [`IteratorExt`]
/// * [`PackedResult<T, E>`](PackedResult)
///
/// *Note:* [`Accumulated<E>`] isn't in the prelude, as its use isn't advised
/// unless necessary. It's expected a minority of projects will actually use
/// [`Accumulated<E>`].