use std::fmt;
use colored::Color;
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProtocolStatus: u8 {
const OK = 0b0000_0001;
const ERROR = 0b0000_0010;
const WAITING = 0b0000_0100;
const MALFORMED = 0b0001_0000; const REFUSED = 0b0010_0000; const RESERVED = 0b0100_0000; const VERSION = 0b1000_0000;
const OUTOFBAND = Self::ERROR.bits() | Self::REFUSED.bits() | Self::VERSION.bits();
const NOTINBAND = Self::OK.bits() | Self::VERSION.bits();
const SIDEGRADE = Self::WAITING.bits() | Self::MALFORMED.bits() | Self::RESERVED.bits();
const TIMEDOUT = Self::ERROR.bits() | Self::WAITING.bits();
const GAVEUP = Self::OK.bits() | Self::WAITING.bits();
const WAITSEC = Self::OK.bits() | Self::WAITING.bits() | Self::RESERVED.bits();
}
}
impl ProtocolStatus {
pub fn has_flag(&self, flag: ProtocolStatus) -> bool {
self.contains(flag)
}
pub fn is_error(&self) -> bool {
self.contains(ProtocolStatus::ERROR)
}
pub fn is_ok(&self) -> bool {
self.contains(ProtocolStatus::OK)
}
pub fn is_waiting(&self) -> bool {
self.contains(ProtocolStatus::WAITING)
}
pub fn get_status_color(&self) -> Color {
match *self {
ProtocolStatus::OK => Color::Green,
ProtocolStatus::ERROR => Color::Red,
ProtocolStatus::WAITING => Color::Yellow,
ProtocolStatus::SIDEGRADE => Color::BrightMagenta,
_ => Color::White,
}
}
}
impl fmt::Display for ProtocolStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let description = match *self {
ProtocolStatus::OK => "OK",
ProtocolStatus::ERROR => "Error",
ProtocolStatus::WAITING => "Waiting",
ProtocolStatus::SIDEGRADE => "SideGrade",
_ => "Unknown",
};
write!(f, "{}", description)
}
}