Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 1 | use crate::EitherOrBoth::*; |
| 2 | |
| 3 | use either::Either; |
| 4 | |
| 5 | /// Value that either holds a single A or B, or both. |
| 6 | #[derive(Clone, PartialEq, Eq, Hash, Debug)] |
| 7 | pub 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 | |
| 16 | impl<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. |
Joel Galenson | b593e25 | 2021-06-21 13:15:57 -0700 | [diff] [blame] | 28 | /// Exclusive version of [`has_left`](EitherOrBoth::has_left). |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 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. |
Joel Galenson | b593e25 | 2021-06-21 13:15:57 -0700 | [diff] [blame] | 37 | /// Exclusive version of [`has_right`](EitherOrBoth::has_right). |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 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 | |
Joel Galenson | b593e25 | 2021-06-21 13:15:57 -0700 | [diff] [blame] | 143 | /// Apply the function `f` on the value `a` in `Left(a)` or `Both(a, _)` variants if it is |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 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 | |
Joel Galenson | b593e25 | 2021-06-21 13:15:57 -0700 | [diff] [blame] | 155 | /// Apply the function `f` on the value `b` |
| 156 | /// in `Right(b)` or `Both(_, b)` variants if it is present. |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 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 | } |
Joel Galenson | b593e25 | 2021-06-21 13:15:57 -0700 | [diff] [blame] | 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 [`default`](Default::default) |
| 169 | /// for the other. |
| 170 | pub fn or_default(self) -> (A, B) |
| 171 | where |
| 172 | A: Default, |
| 173 | B: Default, |
| 174 | { |
| 175 | match self { |
| 176 | EitherOrBoth::Left(l) => (l, B::default()), |
| 177 | EitherOrBoth::Right(r) => (A::default(), r), |
| 178 | EitherOrBoth::Both(l, r) => (l, r), |
| 179 | } |
| 180 | } |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | impl<T> EitherOrBoth<T, T> { |
| 184 | /// Return either value of left, right, or the product of `f` applied where `Both` are present. |
| 185 | pub fn reduce<F>(self, f: F) -> T |
| 186 | where |
| 187 | F: FnOnce(T, T) -> T, |
| 188 | { |
| 189 | match self { |
| 190 | Left(a) => a, |
| 191 | Right(b) => b, |
| 192 | Both(a, b) => f(a, b), |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B> { |
| 198 | fn into(self) -> Option<Either<A, B>> { |
| 199 | match self { |
| 200 | EitherOrBoth::Left(l) => Some(Either::Left(l)), |
| 201 | EitherOrBoth::Right(r) => Some(Either::Right(r)), |
| 202 | _ => None, |
| 203 | } |
| 204 | } |
| 205 | } |