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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use alloc::vec::Vec;
use core::fmt::{Debug, Display, Formatter};
use std::error::Error;
use crate::Validation::{Failure, Success};
#[cfg(feature = "nightly")]
mod try_from;
pub mod convert;
/// A validation result with multiple diagnostics.
#[derive(Debug)]
pub enum Validation<T, E> {
/// Verification process complete
Success {
/// The final product after successful verification
value: T,
/// Some diagnostics that does not stop the analysis
diagnostics: Vec<E>,
},
/// Verification process interrupted
Failure {
/// A fatal problem prevents the analysis from continuing
fatal: E,
/// Some diagnostics that does not stop the analysis
diagnostics: Vec<E>,
},
}
impl<T, E> Display for Validation<T, E>
where
T: Debug,
E: Error,
{
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
Debug::fmt(self, f)
}
}
impl<T, E> Error for Validation<T, E>
where
T: Debug,
E: Error,
{
}
/// Constructors and collectors of Validation
impl<T, E> Validation<T, E> {
/// A value error occurred
pub fn fine<I>(value: I) -> Self
where
I: Into<T>,
{
Success { value: value.into(), diagnostics: vec![] }
}
/// A fatal error occurred
pub fn fail<I>(error: I) -> Self
where
I: Into<E>,
{
Failure { fatal: error.into(), diagnostics: vec![] }
}
pub fn push<I>(&mut self, error: I)
where
I: Into<E>,
{
match self {
Success { diagnostics, .. } => diagnostics.push(error.into()),
Failure { diagnostics, .. } => diagnostics.push(error.into()),
}
}
}
impl<T, E> Extend<E> for Validation<T, E> {
fn extend<I>(&mut self, errors: I)
where
I: IntoIterator<Item = E>,
{
match self {
Success { diagnostics, .. } => diagnostics.extend(errors),
Failure { diagnostics, .. } => diagnostics.extend(errors),
}
}
}
impl<T, E> Validation<T, E> {
/// Check if the validate result is success
pub fn is_success(&self) -> bool {
matches!(self, Validation::Success { .. })
}
/// Check if the validate result is failure
pub fn is_failure(&self) -> bool {
matches!(self, Validation::Failure { .. })
}
/// Check if the validate result has no problem
pub fn no_problem(&self) -> bool {
match self {
Success { diagnostics, .. } => diagnostics.is_empty(),
Failure { .. } => false,
}
}
/// Returns the contained [`Validation::Success`] value, consuming the `self` value.
pub fn unwrap(self) -> T
where
E: Display,
{
match self {
Success { value, diagnostics: _ } => value,
Failure { fatal, diagnostics: _ } => panic!("{}", fatal),
}
}
/// Returns the contained [`Validation::Success`] value, consuming the `self` value.
pub fn unwrap_or_default(self) -> T
where
T: Default,
{
match self {
Success { value, .. } => value,
Failure { .. } => T::default(),
}
}
/// Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a
/// contained [`Ok`] value, leaving an [`Err`] value untouched.
///
/// This function can be used to compose the results of two functions.
///
/// # Examples
///
/// Print the numbers on each line of a string multiplied by two.
///
/// ```
/// let line = "1\n2\n3\n4\n";
///
/// for num in line.lines() {
/// match num.parse::<i32>().map(|i| i * 2) {
/// Ok(n) => println!("{n}"),
/// Err(..) => {}
/// }
/// }
/// ```
pub fn map<U, F>(self, f: F) -> Validation<U, E>
where
F: FnOnce(T) -> U,
{
match self {
Success { value, diagnostics } => Success { value: f(value), diagnostics },
Failure { fatal, diagnostics } => Failure { fatal, diagnostics },
}
}
/// Append the result to goods and fails
///
/// This is equivalent to using a [`for`] loop on the iterator, although
/// `break` and `continue` are not possible from a closure. It's generally
/// more idiomatic to use a `for` loop, but `for_each` may be more legible
/// when processing items at the end of longer iterator chains. In some
/// cases `for_each` may also be faster than a loop, because it will use
/// internal iteration on adapters like `Chain`.
///
/// [`for`]: ../../book/ch03-05-control-flow.html#looping-through-a-collection-with-for
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use std::sync::mpsc::channel;
///
/// let (tx, rx) = channel();
/// (0..5).map(|x| x * 2 + 1).for_each(move |x| tx.send(x).unwrap());
///
/// let v: Vec<_> = rx.iter().collect();
/// assert_eq!(v, vec![1, 3, 5, 7, 9]);
/// ```
///
/// For such a small example, a `for` loop may be cleaner, but `for_each`
/// might be preferable to keep a functional style with longer iterators:
///
/// ```
/// (0..5)
/// .flat_map(|x| x * 100..x * 110)
/// .enumerate()
/// .filter(|&(i, x)| (i + x) % 3 == 0)
/// .for_each(|(i, x)| println!("{i}:{x}"));
/// ```
pub fn and_then<U, F>(self, f: F) -> Validation<U, E>
where
F: FnOnce(T) -> Validation<U, E>,
{
match self {
Success { value, mut diagnostics } => match f(value) {
Success { value, diagnostics: new } => {
diagnostics.extend(new);
Success { value, diagnostics }
}
Failure { fatal, diagnostics: new } => {
diagnostics.extend(new);
Failure { fatal, diagnostics }
}
},
Failure { fatal, diagnostics } => Failure { fatal, diagnostics },
}
}
/// Append the result to goods and fails
///
/// This is equivalent to using a [`for`] loop on the iterator, although
/// `break` and `continue` are not possible from a closure. It's generally
/// more idiomatic to use a `for` loop, but `for_each` may be more legible
/// when processing items at the end of longer iterator chains. In some
/// cases `for_each` may also be faster than a loop, because it will use
/// internal iteration on adapters like `Chain`.
///
/// [`for`]: ../../book/ch03-05-control-flow.html#looping-through-a-collection-with-for
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use std::sync::mpsc::channel;
///
/// let (tx, rx) = channel();
/// (0..5).map(|x| x * 2 + 1).for_each(move |x| tx.send(x).unwrap());
///
/// let v: Vec<_> = rx.iter().collect();
/// assert_eq!(v, vec![1, 3, 5, 7, 9]);
/// ```
///
/// For such a small example, a `for` loop may be cleaner, but `for_each`
/// might be preferable to keep a functional style with longer iterators:
///
/// ```
/// (0..5)
/// .flat_map(|x| x * 100..x * 110)
/// .enumerate()
/// .filter(|&(i, x)| (i + x) % 3 == 0)
/// .for_each(|(i, x)| println!("{i}:{x}"));
/// ```
pub fn sending<G, F>(self, goods: &mut G, fails: &mut F)
where
G: Extend<T>,
F: Extend<E>,
{
match self {
Success { value, diagnostics } => {
fails.extend(diagnostics);
goods.extend(Some(value));
}
Failure { fatal, diagnostics } => {
fails.extend(diagnostics);
fails.extend(Some(fatal))
}
}
}
/// Omit the result
pub fn omit(self) {}
/// Returns the provided default (if [`Err`]), or
/// applies a function to the contained value (if [`Ok`]),
///
/// Arguments passed to `map_or` are eagerly evaluated; if you are passing
/// the result of a function call, it is recommended to use [`map_or_else`],
/// which is lazily evaluated.
///
/// [`map_or_else`]: Result::map_or_else
///
/// # Examples
///
/// ```
/// let x: Result<_, &str> = Ok("foo");
/// assert_eq!(x.map_or(42, |v| v.len()), 3);
///
/// let x: Result<&str, _> = Err("bar");
/// assert_eq!(x.map_or(42, |v| v.len()), 42);
/// ```
pub fn map_or<U, F>(self, default: U, f: F) -> U
where
F: FnOnce(T) -> U,
{
match self {
Success { value, .. } => f(value),
Failure { .. } => default,
}
}
}
impl<T, E> Validation<T, E> {
/// Turn a [`Validation`] into a [`Result`]
pub fn result<F>(self, f: F) -> Result<T, E>
where
F: FnMut(E) -> (),
{
match self {
Success { value, diagnostics } => {
diagnostics.into_iter().for_each(f);
Ok(value)
}
Failure { fatal, diagnostics } => {
diagnostics.into_iter().for_each(f);
Err(fatal)
}
}
}
/// Turn a [`Validation`] into a [`Option`]
pub fn option<F>(self, mut f: F) -> Option<T>
where
F: FnMut(E) -> (),
{
match self {
Success { value, diagnostics } => {
diagnostics.into_iter().for_each(f);
Some(value)
}
Failure { fatal, diagnostics } => {
for error in diagnostics.into_iter() {
f(error);
}
f(fatal);
None
}
}
}
/// Turn a [`Validation`] into a [`Option`]
pub fn each_error<F>(self, mut f: F)
where
F: FnMut(E) -> (),
{
match self {
Success { value: _, diagnostics } => {
diagnostics.into_iter().for_each(f);
}
Failure { fatal, diagnostics } => {
for error in diagnostics.into_iter() {
f(error);
}
f(fatal);
}
}
}
}