2016-10-04 00:09:13 +00:00
|
|
|
use std::fmt;
|
2016-04-11 10:57:23 +00:00
|
|
|
|
|
|
|
use term_painter::Color::*;
|
|
|
|
use term_painter::ToStyle;
|
2016-10-04 00:09:13 +00:00
|
|
|
|
2016-10-08 06:20:49 +00:00
|
|
|
#[must_use]
|
2016-10-08 02:09:05 +00:00
|
|
|
pub enum Outcome<T> {
|
2016-10-08 06:20:49 +00:00
|
|
|
/// Signifies that all processing completed successfully.
|
2016-10-08 02:09:05 +00:00
|
|
|
Success,
|
2016-10-08 06:20:49 +00:00
|
|
|
/// Signifies that some processing occurred that ultimately resulted in
|
|
|
|
/// failure. As a result, no further processing can occur.
|
|
|
|
Failure,
|
|
|
|
/// Signifies that no processing occured and as such, processing can be
|
|
|
|
/// forwarded to the next available target.
|
|
|
|
Forward(T),
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 02:09:05 +00:00
|
|
|
impl<T> Outcome<T> {
|
2016-10-09 11:29:02 +00:00
|
|
|
pub fn of<A, B: fmt::Debug>(result: Result<A, B>) -> Outcome<T> {
|
|
|
|
if let Err(e) = result {
|
|
|
|
error_!("{:?}", e);
|
|
|
|
return Outcome::Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
Outcome::Success
|
|
|
|
}
|
|
|
|
|
2016-04-11 10:57:23 +00:00
|
|
|
pub fn as_str(&self) -> &'static str {
|
2016-04-23 02:48:03 +00:00
|
|
|
match *self {
|
2016-10-08 02:09:05 +00:00
|
|
|
Outcome::Success => "Success",
|
2016-10-08 06:20:49 +00:00
|
|
|
Outcome::Failure => "FailStop",
|
|
|
|
Outcome::Forward(..) => "Forward",
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_int(&self) -> isize {
|
2016-04-23 02:48:03 +00:00
|
|
|
match *self {
|
2016-10-08 02:09:05 +00:00
|
|
|
Outcome::Success => 0,
|
2016-10-08 06:20:49 +00:00
|
|
|
Outcome::Failure => 1,
|
|
|
|
Outcome::Forward(..) => 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expect_success(&self) {
|
|
|
|
if *self != Outcome::Success {
|
|
|
|
panic!("expected a successful outcome");
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 02:09:05 +00:00
|
|
|
impl<T> PartialEq for Outcome<T> {
|
|
|
|
fn eq(&self, other: &Outcome<T>) -> bool {
|
2016-04-11 10:57:23 +00:00
|
|
|
self.as_int() == other.as_int()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 02:09:05 +00:00
|
|
|
impl<T> fmt::Debug for Outcome<T> {
|
2016-04-11 10:57:23 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "Outcome::{}", self.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 02:09:05 +00:00
|
|
|
impl<T> fmt::Display for Outcome<T> {
|
2016-04-11 10:57:23 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-04-23 02:48:03 +00:00
|
|
|
match *self {
|
2016-10-08 02:09:05 +00:00
|
|
|
Outcome::Success => write!(f, "{}", Green.paint("Success")),
|
2016-10-08 06:20:49 +00:00
|
|
|
Outcome::Failure => write!(f, "{}", Red.paint("Failure")),
|
|
|
|
Outcome::Forward(..) => write!(f, "{}", Cyan.paint("Forwarding")),
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|