use core::fmt;
#[must_use]
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Status(pub usize);
impl Status {
pub const ERROR_BIT: usize = 1 << (usize::BITS - 1);
pub const SUCCESS: Self = Self(0);
pub const WARN_UNKNOWN_GLYPH: Self = Self(1);
pub const WARN_DELETE_FAILURE: Self = Self(2);
pub const WARN_WRITE_FAILURE: Self = Self(3);
pub const WARN_BUFFER_TOO_SMALL: Self = Self(4);
pub const WARN_STALE_DATA: Self = Self(5);
pub const WARN_FILE_SYSTEM: Self = Self(6);
pub const WARN_RESET_REQUIRED: Self = Self(7);
pub const LOAD_ERROR: Self = Self(Self::ERROR_BIT | 1);
pub const INVALID_PARAMETER: Self = Self(Self::ERROR_BIT | 2);
pub const UNSUPPORTED: Self = Self(Self::ERROR_BIT | 3);
pub const BAD_BUFFER_SIZE: Self = Self(Self::ERROR_BIT | 4);
pub const BUFFER_TOO_SMALL: Self = Self(Self::ERROR_BIT | 5);
pub const NOT_READY: Self = Self(Self::ERROR_BIT | 6);
pub const DEVICE_ERROR: Self = Self(Self::ERROR_BIT | 7);
pub const WRITE_PROTECTED: Self = Self(Self::ERROR_BIT | 8);
pub const OUT_OF_RESOURCES: Self = Self(Self::ERROR_BIT | 9);
pub const VOLUME_CORRUPTED: Self = Self(Self::ERROR_BIT | 10);
pub const VOLUME_FULL: Self = Self(Self::ERROR_BIT | 11);
pub const NO_MEDIA: Self = Self(Self::ERROR_BIT | 12);
pub const MEDIA_CHANGED: Self = Self(Self::ERROR_BIT | 13);
pub const NOT_FOUND: Self = Self(Self::ERROR_BIT | 14);
pub const ACCESS_DENIED: Self = Self(Self::ERROR_BIT | 15);
pub const NO_RESPONSE: Self = Self(Self::ERROR_BIT | 16);
pub const NO_MAPPING: Self = Self(Self::ERROR_BIT | 17);
pub const TIMEOUT: Self = Self(Self::ERROR_BIT | 18);
pub const NOT_STARTED: Self = Self(Self::ERROR_BIT | 19);
pub const ALREADY_STARTED: Self = Self(Self::ERROR_BIT | 20);
pub const ABORTED: Self = Self(Self::ERROR_BIT | 21);
pub const ICMP_ERROR: Self = Self(Self::ERROR_BIT | 22);
pub const TFTP_ERROR: Self = Self(Self::ERROR_BIT | 23);
pub const PROTOCOL_ERROR: Self = Self(Self::ERROR_BIT | 24);
pub const INCOMPATIBLE_VERSION: Self = Self(Self::ERROR_BIT | 25);
pub const SECURITY_VIOLATION: Self = Self(Self::ERROR_BIT | 26);
pub const CRC_ERROR: Self = Self(Self::ERROR_BIT | 27);
pub const END_OF_MEDIA: Self = Self(Self::ERROR_BIT | 28);
pub const END_OF_FILE: Self = Self(Self::ERROR_BIT | 31);
pub const INVALID_LANGUAGE: Self = Self(Self::ERROR_BIT | 32);
pub const COMPROMISED_DATA: Self = Self(Self::ERROR_BIT | 33);
pub const IP_ADDRESS_CONFLICT: Self = Self(Self::ERROR_BIT | 34);
pub const HTTP_ERROR: Self = Self(Self::ERROR_BIT | 35);
pub fn is_success(&self) -> bool {
self == &Self::SUCCESS
}
pub fn is_error(&self) -> bool {
(self.0 & Self::ERROR_BIT) == Self::ERROR_BIT
}
pub fn is_warning(&self) -> bool {
!self.is_error() && !self.is_success()
}
}
impl From<usize> for Status {
fn from(value: usize) -> Self {
Self(value)
}
}
impl fmt::Display for Status {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::SUCCESS => write!(f, "success"),
Self::WARN_UNKNOWN_GLYPH => write!(f, "unknown glyph"),
Self::WARN_DELETE_FAILURE => write!(f, "delete failure"),
Self::WARN_WRITE_FAILURE => write!(f, "write failure"),
Self::WARN_STALE_DATA => write!(f, "stale data"),
Self::WARN_FILE_SYSTEM => write!(f, "file system"),
Self::WARN_RESET_REQUIRED => write!(f, "reset required"),
Self::LOAD_ERROR => write!(f, "load error"),
Self::INVALID_PARAMETER => write!(f, "invalid paramter"),
Self::UNSUPPORTED => write!(f, "unsupported"),
Self::BAD_BUFFER_SIZE => write!(f, "bad buffer size"),
Self::WARN_BUFFER_TOO_SMALL | Self::BUFFER_TOO_SMALL => write!(f, "buffer too small"),
Self::NOT_READY => write!(f, "not ready"),
Self::DEVICE_ERROR => write!(f, "device error"),
Self::WRITE_PROTECTED => write!(f, "write protected"),
Self::OUT_OF_RESOURCES => write!(f, "out of resources"),
Self::VOLUME_CORRUPTED => write!(f, "volume corrupted"),
Self::VOLUME_FULL => write!(f, "volume full"),
Self::NO_MEDIA => write!(f, "no media"),
Self::MEDIA_CHANGED => write!(f, "media changed"),
Self::NOT_FOUND => write!(f, "not found"),
Self::ACCESS_DENIED => write!(f, "acccess denied"),
Self::NO_RESPONSE => write!(f, "no response"),
Self::NO_MAPPING => write!(f, "no mapping"),
Self::TIMEOUT => write!(f, "timeout"),
Self::NOT_STARTED => write!(f, "not started"),
Self::ALREADY_STARTED => write!(f, "already started"),
Self::ABORTED => write!(f, "aborted"),
Self::ICMP_ERROR => write!(f, "ICMP error"),
Self::TFTP_ERROR => write!(f, "TFTP error"),
Self::PROTOCOL_ERROR => write!(f, "protocol error"),
Self::INCOMPATIBLE_VERSION => write!(f, "incompatible version"),
Self::SECURITY_VIOLATION => write!(f, "security violation"),
Self::CRC_ERROR => write!(f, "CRC error"),
Self::END_OF_MEDIA => write!(f, "end of media"),
Self::END_OF_FILE => write!(f, "end of file"),
Self::INVALID_LANGUAGE => write!(f, "invalid language"),
Self::COMPROMISED_DATA => write!(f, "compromised data"),
Self::IP_ADDRESS_CONFLICT => write!(f, "IP address conflict"),
Self::HTTP_ERROR => write!(f, "HTTP error"),
_ => write!(f, "{:#X}", self.0),
}
}
}
impl fmt::LowerHex for Status {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(&self.0, f)
}
}
impl fmt::UpperHex for Status {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::UpperHex::fmt(&self.0, f)
}
}
pub type Result<T> = core::result::Result<T, Status>;
impl From<Status> for Result<()> {
fn from(status: Status) -> Self {
match status {
Status::SUCCESS => Ok(()),
e => Err(e),
}
}
}
impl<T> From<Result<T>> for Status {
fn from(res: Result<T>) -> Self {
match res {
Ok(_) => Status::SUCCESS,
Err(e) => e,
}
}
}