process_results/
errors.rs1use ControlFlow::*;
2
3#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
5pub enum ControlFlow {
6 Break,
8 Continue,
10}
11
12pub trait ErrorCollector<E> {
15 type Collection;
17
18 fn empty() -> Self;
20
21 fn push_err(&mut self, err: E) -> ControlFlow;
24
25 fn with_value<T>(self, val: T) -> Result<T, Self::Collection>;
28}
29
30#[derive(Debug, Copy, Clone)]
33pub struct Ignore;
34
35impl<E> ErrorCollector<E> for Ignore {
36 type Collection = Infallible;
37
38 #[inline]
39 fn empty() -> Self {
40 Self
41 }
42
43 #[inline]
44 fn push_err(&mut self, _err: E) -> ControlFlow {
45 Continue
46 }
47
48 #[inline]
49 fn with_value<T>(self, val: T) -> Result<T, Self::Collection> {
50 Ok(val)
51 }
52}
53
54impl<E, F: From<E>> ErrorCollector<E> for Option<F> {
55 type Collection = F;
56
57 #[inline]
58 fn empty() -> Self {
59 None
60 }
61
62 #[inline]
63 fn push_err(&mut self, err: E) -> ControlFlow {
64 *self = Some(err.into());
65 Break
66 }
67
68 #[inline]
69 fn with_value<T>(self, val: T) -> Result<T, Self::Collection> {
70 if let Some(err) = self {
71 Err(err)
72 } else {
73 Ok(val)
74 }
75 }
76}
77
78impl<E, F: From<E>> ErrorCollector<E> for Vec<F> {
79 type Collection = Self;
80
81 #[inline]
82 fn empty() -> Self {
83 Self::new()
84 }
85
86 #[inline]
87 fn push_err(&mut self, err: E) -> ControlFlow {
88 self.push(err.into());
89 Continue
90 }
91
92 #[inline]
93 fn with_value<T>(self, val: T) -> Result<T, Self::Collection> {
94 if self.is_empty() {
95 Ok(val)
96 } else {
97 Err(self)
98 }
99 }
100}
101
102#[cfg(feature = "arrayvec")]
103use arrayvec::ArrayVec;
104use std::convert::Infallible;
105
106#[cfg(feature = "arrayvec")]
108impl<E, F: From<E>, const CAP: usize> ErrorCollector<E> for ArrayVec<F, CAP> {
109 type Collection = Self;
110
111 #[inline]
112 fn empty() -> Self {
113 Self::new()
114 }
115
116 #[inline]
117 fn push_err(&mut self, err: E) -> ControlFlow {
118 match self.try_push(err.into()) {
119 Ok(_) => Continue,
120 Err(_) => Break,
121 }
122 }
123
124 #[inline]
125 fn with_value<T>(self, val: T) -> Result<T, Self::Collection> {
126 if self.is_empty() {
127 Ok(val)
128 } else {
129 Err(self)
130 }
131 }
132}