error_combinator/
cmberr.rs1
2pub trait CombineError<EA, EB> {
3 type Out;
4
5 fn left(&mut self, ea: EA);
6 fn right(&mut self, eb: EB);
7 fn finish(self) -> Self::Out;
8}
9
10pub trait CombineErrorBuilder<EA, EB> {
11 type Combiner: CombineError<EA, EB, Out = Self::Out>;
12 type Out;
13
14 fn build() -> Self::Combiner;
15}
16
17pub struct DefaultCombine<E> {
20 data: Option<E>
21}
22
23impl<E> CombineErrorBuilder<E, E> for DefaultCombine<E> {
24 type Combiner = Self;
25 type Out = E;
26
27 fn build() -> Self::Combiner {
28 DefaultCombine { data:None }
29 }
30}
31
32impl<E> CombineError<E, E> for DefaultCombine<E> {
33 type Out = E;
34
35 fn left(&mut self, ea: E) {
36 self.data = Some(ea);
37 }
38
39 fn right(&mut self, eb: E) {
40 self.data = Some(eb);
41 }
42
43 fn finish(self) -> Self::Out {
44 self.data.unwrap()
45 }
46}
47
48pub struct VecCombine<T> {
51 data: Vec<T>
52}
53
54impl<E> CombineErrorBuilder<E, E> for VecCombine<E> {
55 type Combiner = Self;
56 type Out = Vec<E>;
57
58 fn build() -> Self::Combiner {
59 VecCombine { data: Vec::new() }
60 }
61}
62
63impl<E> CombineErrorBuilder<Vec<E>, E> for VecCombine<E> {
64 type Combiner = Self;
65 type Out = Vec<E>;
66
67 fn build() -> Self::Combiner {
68 VecCombine { data: Vec::new() }
69 }
70}
71
72impl<T> CombineError<T, T> for VecCombine<T> {
73 type Out = Vec<T>;
74
75 fn left(&mut self, ea: T) {
76 self.data.push(ea);
77 }
78
79 fn right(&mut self, eb: T) {
80 self.data.push(eb);
81 }
82
83 fn finish(self) -> Self::Out {
84 self.data
85 }
86}
87
88impl<E> CombineError<Vec<E>, E> for VecCombine<E> {
89 type Out = Vec<E>;
90
91 fn left(&mut self, ea: Vec<E>) {
92 self.data.extend(ea);
93 }
94
95 fn right(&mut self, eb: E) {
96 self.data.push(eb);
97 }
98
99 fn finish(self) -> Self::Out {
100 self.data
101 }
102}
103