Rocket/lib/src/response/outcome.rs

61 lines
1.7 KiB
Rust
Raw Normal View History

use std::fmt;
use term_painter::Color::*;
use term_painter::ToStyle;
use http::hyper::FreshHyperResponse;
pub enum Outcome<'h> {
2016-09-22 01:23:44 +00:00
/// Signifies a response that completed sucessfully.
Complete,
2016-09-22 01:31:26 +00:00
/// Signifies a response that failed internally.
2016-09-30 04:44:27 +00:00
Bad(FreshHyperResponse<'h>),
2016-09-22 01:23:44 +00:00
/// Signifies a failing response where no further processing should happen.
FailStop,
2016-09-22 01:23:44 +00:00
/// Signifies a failing response whose request should be processed further.
2016-09-30 04:44:27 +00:00
FailForward(FreshHyperResponse<'h>),
}
impl<'h> Outcome<'h> {
pub fn as_str(&self) -> &'static str {
match *self {
Outcome::Complete => "Complete",
Outcome::FailStop => "FailStop",
2016-09-22 01:31:26 +00:00
Outcome::Bad(..) => "Bad",
Outcome::FailForward(..) => "FailForward",
}
}
fn as_int(&self) -> isize {
match *self {
Outcome::Complete => 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,
}
}
}
impl<'h> PartialEq for Outcome<'h> {
fn eq(&self, other: &Outcome<'h>) -> bool {
self.as_int() == other.as_int()
}
}
impl<'h> fmt::Debug for Outcome<'h> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Outcome::{}", self.as_str())
}
}
impl<'h> fmt::Display for Outcome<'h> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Outcome::Complete => write!(f, "{}", Green.paint("Complete")),
Outcome::Bad(..) => write!(f, "{}", Yellow.paint("Bad Completion")),
Outcome::FailStop => write!(f, "{}", Red.paint("Failed")),
Outcome::FailForward(..) => write!(f, "{}", Cyan.paint("Forwarding")),
}
}
}