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. has_left(&self) -> bool18 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. has_right(&self) -> bool23 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`]. is_left(&self) -> bool29 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`]. is_right(&self) -> bool38 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()`. is_both(&self) -> bool47 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`. left(self) -> Option<A>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`. right(self) -> Option<B>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. both(self) -> Option<(A, B)>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>`. as_ref(&self) -> 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>`. as_mut(&mut self) -> 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>`. flip(self) -> 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. map_left<F, M>(self, f: F) -> EitherOrBoth<M, B> where F: FnOnce(A) -> M,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. map_right<F, M>(self, f: F) -> EitherOrBoth<A, M> where F: FnOnce(B) -> M,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. map_any<F, L, G, R>(self, f: F, g: G) -> EitherOrBoth<L, R> where F: FnOnce(A) -> L, G: FnOnce(B) -> R,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 `b` in `Right(b)` or `Both(a, _)` variants if it is 144 /// present. left_and_then<F, L>(self, f: F) -> EitherOrBoth<L, B> where F: FnOnce(A) -> EitherOrBoth<L, B>,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 `a` 156 /// in `Left(a)` or `Both(a, _)` variants if it is present. right_and_then<F, R>(self, f: F) -> EitherOrBoth<A, R> where F: FnOnce(B) -> EitherOrBoth<A, R>,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 168 impl<T> EitherOrBoth<T, T> { 169 /// Return either value of left, right, or the product of `f` applied where `Both` are present. reduce<F>(self, f: F) -> T where F: FnOnce(T, T) -> T,170 pub fn reduce<F>(self, f: F) -> T 171 where 172 F: FnOnce(T, T) -> T, 173 { 174 match self { 175 Left(a) => a, 176 Right(b) => b, 177 Both(a, b) => f(a, b), 178 } 179 } 180 } 181 182 impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B> { into(self) -> Option<Either<A, B>>183 fn into(self) -> Option<Either<A, B>> { 184 match self { 185 EitherOrBoth::Left(l) => Some(Either::Left(l)), 186 EitherOrBoth::Right(r) => Some(Either::Right(r)), 187 _ => None, 188 } 189 } 190 } 191