2016-10-04 00:09:13 +00:00
|
|
|
use std::fmt;
|
2016-04-11 10:57:23 +00:00
|
|
|
|
|
|
|
use term_painter::Color::*;
|
2016-10-14 01:39:23 +00:00
|
|
|
use term_painter::Color;
|
2016-04-11 10:57:23 +00:00
|
|
|
use term_painter::ToStyle;
|
2016-10-04 00:09:13 +00:00
|
|
|
|
2016-10-08 06:20:49 +00:00
|
|
|
#[must_use]
|
2016-10-14 01:39:23 +00:00
|
|
|
pub enum Outcome<S, E, F> {
|
|
|
|
/// Contains the success value.
|
|
|
|
Success(S),
|
|
|
|
/// Contains the failure error value.
|
|
|
|
Failure(E),
|
|
|
|
/// Contains the value to forward on.
|
|
|
|
Forward(F),
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
impl<S, E, F> Outcome<S, E, F> {
|
|
|
|
/// Unwraps the Outcome, yielding the contents of a Success.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the value is not Success.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn unwrap(self) -> S {
|
|
|
|
match self {
|
|
|
|
Outcome::Success(val) => val,
|
|
|
|
_ => panic!("Expected a successful outcome!")
|
2016-10-09 11:29:02 +00:00
|
|
|
}
|
2016-10-14 01:39:23 +00:00
|
|
|
}
|
2016-10-09 11:29:02 +00:00
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
/// Return true if this `Outcome` is a `Success`.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn is_success(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Outcome::Success(_) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
2016-10-09 11:29:02 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
/// Return true if this `Outcome` is a `Failure`.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn is_failure(&self) -> bool {
|
2016-04-23 02:48:03 +00:00
|
|
|
match *self {
|
2016-10-14 01:39:23 +00:00
|
|
|
Outcome::Failure(_) => true,
|
|
|
|
_ => false
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
/// Return true if this `Outcome` is a `Forward`.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn is_forward(&self) -> bool {
|
2016-04-23 02:48:03 +00:00
|
|
|
match *self {
|
2016-10-14 01:39:23 +00:00
|
|
|
Outcome::Forward(_) => true,
|
|
|
|
_ => false
|
2016-10-08 06:20:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
/// Converts from `Outcome<S, E, F>` to `Option<S>`.
|
|
|
|
///
|
|
|
|
/// Returns the `Some` of the `Success` if this is a `Success`, otherwise
|
|
|
|
/// returns `None`. `self` is consumed, and all other values are discarded.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn succeeded(self) -> Option<S> {
|
|
|
|
match self {
|
|
|
|
Outcome::Success(val) => Some(val),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts from `Outcome<S, E, F>` to `Option<E>`.
|
|
|
|
///
|
|
|
|
/// Returns the `Some` of the `Failure` if this is a `Failure`, otherwise
|
|
|
|
/// returns `None`. `self` is consumed, and all other values are discarded.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn failed(self) -> Option<E> {
|
|
|
|
match self {
|
|
|
|
Outcome::Failure(val) => Some(val),
|
|
|
|
_ => None
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
/// Converts from `Outcome<S, E, F>` to `Option<F>`.
|
|
|
|
///
|
|
|
|
/// Returns the `Some` of the `Forward` if this is a `Forward`, otherwise
|
|
|
|
/// returns `None`. `self` is consumed, and all other values are discarded.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn forwarded(self) -> Option<F> {
|
|
|
|
match self {
|
|
|
|
Outcome::Forward(val) => Some(val),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn formatting(&self) -> (Color, &'static str) {
|
|
|
|
match *self {
|
|
|
|
Outcome::Success(..) => (Green, "Succcess"),
|
|
|
|
Outcome::Failure(..) => (Red, "Failure"),
|
|
|
|
Outcome::Forward(..) => (Yellow, "Forward"),
|
|
|
|
}
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
impl<S, E, F> fmt::Debug for Outcome<S, E, F> {
|
2016-04-11 10:57:23 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-10-14 01:39:23 +00:00
|
|
|
write!(f, "Outcome::{}", self.formatting().1)
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
impl<S, E, F> fmt::Display for Outcome<S, E, F> {
|
2016-04-11 10:57:23 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-10-14 01:39:23 +00:00
|
|
|
let (color, string) = self.formatting();
|
|
|
|
write!(f, "{}", color.paint(string))
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|