use std::fmt; use term_painter::Color::*; use term_painter::ToStyle; #[must_use] pub enum Outcome { /// Signifies that all processing completed successfully. Success, /// 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), } impl Outcome { pub fn as_str(&self) -> &'static str { match *self { Outcome::Success => "Success", Outcome::Failure => "FailStop", Outcome::Forward(..) => "Forward", } } fn as_int(&self) -> isize { match *self { Outcome::Success => 0, Outcome::Failure => 1, Outcome::Forward(..) => 2, } } pub fn expect_success(&self) { if *self != Outcome::Success { panic!("expected a successful outcome"); } } } impl PartialEq for Outcome { fn eq(&self, other: &Outcome) -> bool { self.as_int() == other.as_int() } } impl fmt::Debug for Outcome { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Outcome::{}", self.as_str()) } } impl fmt::Display for Outcome { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Outcome::Success => write!(f, "{}", Green.paint("Success")), Outcome::Failure => write!(f, "{}", Red.paint("Failure")), Outcome::Forward(..) => write!(f, "{}", Cyan.paint("Forwarding")), } } }