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
|
|
|
|
|
|
|
use http::hyper::FreshHyperResponse;
|
2016-04-11 10:57:23 +00:00
|
|
|
|
2016-10-08 02:09:05 +00:00
|
|
|
pub enum Outcome<T> {
|
2016-09-22 01:23:44 +00:00
|
|
|
/// Signifies a response that completed sucessfully.
|
2016-10-08 02:09:05 +00:00
|
|
|
Success,
|
|
|
|
/// Signifies a failing response that started responding but fail, so no
|
|
|
|
/// further processing can occur.
|
2016-04-11 10:57:23 +00:00
|
|
|
FailStop,
|
2016-10-08 02:09:05 +00:00
|
|
|
/// Signifies a response that failed internally without beginning to
|
|
|
|
/// respond but no further processing should occur.
|
|
|
|
Bad(T),
|
|
|
|
/// Signifies a failing response that failed internally without beginning to
|
|
|
|
/// respond. Further processing should be attempted.
|
|
|
|
FailForward(T),
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 02:09:05 +00:00
|
|
|
pub type ResponseOutcome<'a> = Outcome<FreshHyperResponse<'a>>;
|
|
|
|
|
|
|
|
impl<T> Outcome<T> {
|
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-04-23 02:48:03 +00:00
|
|
|
Outcome::FailStop => "FailStop",
|
2016-09-22 01:31:26 +00:00
|
|
|
Outcome::Bad(..) => "Bad",
|
2016-04-23 02:48:03 +00:00
|
|
|
Outcome::FailForward(..) => "FailForward",
|
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-09-22 01:31:26 +00:00
|
|
|
Outcome::Bad(..) => 1,
|
2016-09-22 01:23:44 +00:00
|
|
|
Outcome::FailStop => 2,
|
|
|
|
Outcome::FailForward(..) => 3,
|
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-09-30 22:20:11 +00:00
|
|
|
Outcome::Bad(..) => write!(f, "{}", Yellow.paint("Bad Completion")),
|
|
|
|
Outcome::FailStop => write!(f, "{}", Red.paint("Failed")),
|
|
|
|
Outcome::FailForward(..) => write!(f, "{}", Cyan.paint("Forwarding")),
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|