1use crate::EitherOrBoth::*;
2
3use either::Either;
4
5/// Value that either holds a single A or B, or both.
6#[derive(Clone, PartialEq, Eq, Hash, Debug)]
7pub enum EitherOrBoth<A, B> {
8 /// Both values are present.
9 Both(A, B),
10 /// Only the left value of type `A` is present.
11 Left(A),
12 /// Only the right value of type `B` is present.
13 Right(B),
14}
15
16impl<A, B> EitherOrBoth<A, B> {
17 /// If `Left`, or `Both`, return true, otherwise, return false.
18 pub fn has_left(&self) -> bool {
19 self.as_ref().left().is_some()
20 }
21
22 /// If `Right`, or `Both`, return true, otherwise, return false.
23 pub fn has_right(&self) -> bool {
24 self.as_ref().right().is_some()
25 }
26
27 /// If Left, return true otherwise, return false.
28 /// Exclusive version of [`has_left`](EitherOrBoth::has_left).
29 pub fn is_left(&self) -> bool {
30 match *self {
31 Left(_) => true,
32 _ => false,
33 }
34 }
35
36 /// If Right, return true otherwise, return false.
37 /// Exclusive version of [`has_right`](EitherOrBoth::has_right).
38 pub fn is_right(&self) -> bool {
39 match *self {
40 Right(_) => true,
41 _ => false,
42 }
43 }
44
45 /// If Right, return true otherwise, return false.
46 /// Equivalent to `self.as_ref().both().is_some()`.
47 pub fn is_both(&self) -> bool {
48 self.as_ref().both().is_some()
49 }
50
51 /// If `Left`, or `Both`, return `Some` with the left value, otherwise, return `None`.
52 pub fn left(self) -> Option<A> {
53 match self {
54 Left(left) | Both(left, _) => Some(left),
55 _ => None,
56 }
57 }
58
59 /// If `Right`, or `Both`, return `Some` with the right value, otherwise, return `None`.
60 pub fn right(self) -> Option<B> {
61 match self {
62 Right(right) | Both(_, right) => Some(right),
63 _ => None,
64 }
65 }
66
67 /// If Both, return `Some` tuple containing left and right.
68 pub fn both(self) -> Option<(A, B)> {
69 match self {
70 Both(a, b) => Some((a, b)),
71 _ => None,
72 }
73 }
74
75 /// Converts from `&EitherOrBoth<A, B>` to `EitherOrBoth<&A, &B>`.
76 pub fn as_ref(&self) -> EitherOrBoth<&A, &B> {
77 match *self {
78 Left(ref left) => Left(left),
79 Right(ref right) => Right(right),
80 Both(ref left, ref right) => Both(left, right),
81 }
82 }
83
84 /// Converts from `&mut EitherOrBoth<A, B>` to `EitherOrBoth<&mut A, &mut B>`.
85 pub fn as_mut(&mut self) -> EitherOrBoth<&mut A, &mut B> {
86 match *self {
87 Left(ref mut left) => Left(left),
88 Right(ref mut right) => Right(right),
89 Both(ref mut left, ref mut right) => Both(left, right),
90 }
91 }
92
93 /// Convert `EitherOrBoth<A, B>` to `EitherOrBoth<B, A>`.
94 pub fn flip(self) -> EitherOrBoth<B, A> {
95 match self {
96 Left(a) => Right(a),
97 Right(b) => Left(b),
98 Both(a, b) => Both(b, a),
99 }
100 }
101
102 /// Apply the function `f` on the value `a` in `Left(a)` or `Both(a, b)` variants. If it is
103 /// present rewrapping the result in `self`'s original variant.
104 pub fn map_left<F, M>(self, f: F) -> EitherOrBoth<M, B>
105 where
106 F: FnOnce(A) -> M,
107 {
108 match self {
109 Both(a, b) => Both(f(a), b),
110 Left(a) => Left(f(a)),
111 Right(b) => Right(b),
112 }
113 }
114
115 /// Apply the function `f` on the value `b` in `Right(b)` or `Both(a, b)` variants.
116 /// If it is present rewrapping the result in `self`'s original variant.
117 pub fn map_right<F, M>(self, f: F) -> EitherOrBoth<A, M>
118 where
119 F: FnOnce(B) -> M,
120 {
121 match self {
122 Left(a) => Left(a),
123 Right(b) => Right(f(b)),
124 Both(a, b) => Both(a, f(b)),
125 }
126 }
127
128 /// Apply the functions `f` and `g` on the value `a` and `b` respectively;
129 /// found in `Left(a)`, `Right(b)`, or `Both(a, b)` variants.
130 /// The Result is rewrapped `self`'s original variant.
131 pub fn map_any<F, L, G, R>(self, f: F, g: G) -> EitherOrBoth<L, R>
132 where
133 F: FnOnce(A) -> L,
134 G: FnOnce(B) -> R,
135 {
136 match self {
137 Left(a) => Left(f(a)),
138 Right(b) => Right(g(b)),
139 Both(a, b) => Both(f(a), g(b)),
140 }
141 }
142
143 /// Apply the function `f` on the value `a` in `Left(a)` or `Both(a, _)` variants if it is
144 /// present.
145 pub fn left_and_then<F, L>(self, f: F) -> EitherOrBoth<L, B>
146 where
147 F: FnOnce(A) -> EitherOrBoth<L, B>,
148 {
149 match self {
150 Left(a) | Both(a, _) => f(a),
151 Right(b) => Right(b),
152 }
153 }
154
155 /// Apply the function `f` on the value `b`
156 /// in `Right(b)` or `Both(_, b)` variants if it is present.
157 pub fn right_and_then<F, R>(self, f: F) -> EitherOrBoth<A, R>
158 where
159 F: FnOnce(B) -> EitherOrBoth<A, R>,
160 {
161 match self {
162 Left(a) => Left(a),
163 Right(b) | Both(_, b) => f(b),
164 }
165 }
166
167 /// Returns a tuple consisting of the `l` and `r` in `Both(l, r)`, if present.
168 /// Otherwise, returns the wrapped value for the present element, and the supplied
169 /// value for the other. The first (`l`) argument is used for a missing `Left`
170 /// value. The second (`r`) argument is used for a missing `Right` value.
171 ///
172 /// Arguments passed to `or` are eagerly evaluated; if you are passing
173 /// the result of a function call, it is recommended to use [`or_else`],
174 /// which is lazily evaluated.
175 ///
176 /// [`or_else`]: EitherOrBoth::or_else
177 ///
178 /// # Examples
179 ///
180 /// ```
181 /// # use itertools::EitherOrBoth;
182 /// assert_eq!(EitherOrBoth::Both("tree", 1).or("stone", 5), ("tree", 1));
183 /// assert_eq!(EitherOrBoth::Left("tree").or("stone", 5), ("tree", 5));
184 /// assert_eq!(EitherOrBoth::Right(1).or("stone", 5), ("stone", 1));
185 /// ```
186 pub fn or(self, l: A, r: B) -> (A, B) {
187 match self {
188 Left(inner_l) => (inner_l, r),
189 Right(inner_r) => (l, inner_r),
190 Both(inner_l, inner_r) => (inner_l, inner_r),
191 }
192 }
193
194 /// Returns a tuple consisting of the `l` and `r` in `Both(l, r)`, if present.
195 /// Otherwise, returns the wrapped value for the present element, and the [`default`](Default::default)
196 /// for the other.
197 pub fn or_default(self) -> (A, B)
198 where
199 A: Default,
200 B: Default,
201 {
202 match self {
203 EitherOrBoth::Left(l) => (l, B::default()),
204 EitherOrBoth::Right(r) => (A::default(), r),
205 EitherOrBoth::Both(l, r) => (l, r),
206 }
207 }
208
209 /// Returns a tuple consisting of the `l` and `r` in `Both(l, r)`, if present.
210 /// Otherwise, returns the wrapped value for the present element, and computes the
211 /// missing value with the supplied closure. The first argument (`l`) is used for a
212 /// missing `Left` value. The second argument (`r`) is used for a missing `Right` value.
213 ///
214 /// # Examples
215 ///
216 /// ```
217 /// # use itertools::EitherOrBoth;
218 /// let k = 10;
219 /// assert_eq!(EitherOrBoth::Both("tree", 1).or_else(|| "stone", || 2 * k), ("tree", 1));
220 /// assert_eq!(EitherOrBoth::Left("tree").or_else(|| "stone", || 2 * k), ("tree", 20));
221 /// assert_eq!(EitherOrBoth::Right(1).or_else(|| "stone", || 2 * k), ("stone", 1));
222 /// ```
223 pub fn or_else<L: FnOnce() -> A, R: FnOnce() -> B>(self, l: L, r: R) -> (A, B) {
224 match self {
225 Left(inner_l) => (inner_l, r()),
226 Right(inner_r) => (l(), inner_r),
227 Both(inner_l, inner_r) => (inner_l, inner_r),
228 }
229 }
230}
231
232impl<T> EitherOrBoth<T, T> {
233 /// Return either value of left, right, or the product of `f` applied where `Both` are present.
234 pub fn reduce<F>(self, f: F) -> T
235 where
236 F: FnOnce(T, T) -> T,
237 {
238 match self {
239 Left(a) => a,
240 Right(b) => b,
241 Both(a, b) => f(a, b),
242 }
243 }
244}
245
246impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B> {
247 fn into(self) -> Option<Either<A, B>> {
248 match self {
249 EitherOrBoth::Left(l) => Some(Either::Left(l)),
250 EitherOrBoth::Right(r) => Some(Either::Right(r)),
251 _ => None,
252 }
253 }
254}
255