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
use fmt;
use ;
/// A newtype wrapper around `Vec<E>` aimed at supporting multi-error scenarios.
///
/// # `Display`
///
/// [ErrorVec] implements [std::fmt::Display] by prepending each error's display with a count:
///
/// ```
/// use errorvec::ErrorVec;
///
/// let ev: ErrorVec<&str> = ["whoops", "something borked", "ouch!"].into_iter().collect();
/// let expected_display = r#"
/// [error 1 of 3] whoops
///
/// [error 2 of 3] something borked
///
/// [error 3 of 3] ouch!
/// "#.trim_start();
///
/// assert_eq!(expected_display, &ev.to_string());
/// ```
///
/// # `Vec` deref
///
/// [ErrorVec] implements [Deref] and [DerefMut] for `Target = Vec<E>`, exposing all [Vec] methods
/// directly:
///
/// ```
/// use errorvec::ErrorVec;
///
/// let mut ev = ErrorVec::default();
/// ev.push(42);
/// ev.push(17);
/// assert_eq!(&[42, 17], ev.as_slice());
/// assert_eq!(Some(17), ev.pop());
/// assert_eq!(1, ev.len());
/// assert_eq!(Some(42), ev.pop());
/// assert!(ev.is_empty());
/// ```
///
/// # `ResultIterator` usage
///
/// A common usage is via
/// [ResultIterator::into_errorvec_result](crate::ResultIterator::into_errorvec_result) for gathering
/// all errors in an [Iterator] over [Result] values.
///
/// # Empty `ErrorVec`
///
/// An [ErrorVec] containing no values (ie `ErrorVec::is_empty() == true`) typically does not
/// represent an error, and the [ErrorVec::into_result] and [ErrorVec::into_result_with] are often
/// useful in this case:
///
/// ```
/// use errorvec::ErrorVec;
///
/// let ev: ErrorVec<()> = ErrorVec::default();
/// assert!(ev.into_result().is_ok());
/// ```
///
/// # Example - Gathering errors with `take_error` and `into_result_with`
///
/// For scenarios where [ResultIterator](crate::ResultIterator) isn't
/// useful, the [ErrorVec::take_error] and [ErrorVec::into_result_with]
/// methods may be useful.
///
/// ```
/// use std::path::Path;
/// use errorvec::ErrorVec;
///
/// /// Return the string contents of all the paths listed in the `manifest_file`, reporting all
/// /// errors encountered.
/// fn read_manifest_files(manifest_file: &Path) -> Result<Vec<String>, ErrorVec<std::io::Error>> {
/// use std::fs::read_to_string;
///
/// let mut contents = vec![];
/// let mut errs = ErrorVec::default();
///
/// if let Some(manifest) = errs.take_error(read_to_string(manifest_file)) {
/// for line in manifest.lines() {
/// let path = &Path::new(line.trim_end());
/// if let Some(content) = errs.take_error(read_to_string(path)) {
/// contents.push(content)
/// }
/// }
/// }
///
/// errs.into_result_with(contents)
/// }
/// ```
;