#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum UnaryResult<T> {
New(T),
Old,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum BinaryResult<T> {
New(T),
Left,
Right,
Ambi,
}
impl<T> BinaryResult<T> {
pub fn or_left(opt: Option<T>) -> BinaryResult<T> {
opt.map(BinaryResult::New).unwrap_or(BinaryResult::Left)
}
pub fn or_right(opt: Option<T>) -> BinaryResult<T> {
opt.map(BinaryResult::New).unwrap_or(BinaryResult::Right)
}
pub fn unwrap(self) -> T {
match self {
BinaryResult::New(n) => n,
_ => panic!("Expected result to be New(_)!"),
}
}
pub fn unwrap_or_clone(self, left: &T, right: &T) -> T
where
T: Clone,
{
match self {
BinaryResult::New(n) => n,
BinaryResult::Left => left.clone(),
BinaryResult::Right => right.clone(),
BinaryResult::Ambi => left.clone(),
}
}
pub fn map<F, U>(self, func: F) -> BinaryResult<U>
where
F: FnOnce(T) -> U,
{
match self {
BinaryResult::New(t) => BinaryResult::New(func(t)),
BinaryResult::Left => BinaryResult::Left,
BinaryResult::Right => BinaryResult::Right,
BinaryResult::Ambi => BinaryResult::Ambi,
}
}
pub fn swap_sides(self) -> BinaryResult<T> {
match self {
BinaryResult::New(t) => BinaryResult::New(t),
BinaryResult::Left => BinaryResult::Right,
BinaryResult::Right => BinaryResult::Left,
BinaryResult::Ambi => BinaryResult::Ambi,
}
}
}