#[derive(Debug, Copy, Eq, PartialEq, Clone)]
pub enum MsgCodeClass {
Method = 0,
Success = 2,
ClientError = 4,
ServerError = 5,
Signal = 7,
}
impl MsgCodeClass {
pub fn try_from(x: u8) -> Option<MsgCodeClass> {
match x {
0 => Some(MsgCodeClass::Method),
2 => Some(MsgCodeClass::Success),
4 => Some(MsgCodeClass::ClientError),
5 => Some(MsgCodeClass::ServerError),
7 => Some(MsgCodeClass::Signal),
_ => None,
}
}
pub fn contains(self, code: MsgCode) -> bool {
let code_u8 = code as u8;
code_u8 != 0 && (code_u8 >> 5) == self as u8
}
}
const fn calc_code(class: u8, detail: u8) -> isize {
(((class & 0x7) << 5) + detail) as isize
}
#[derive(Debug, Copy, Eq, PartialEq, Clone)]
pub enum MsgCode {
Empty = 0x00,
MethodGet = 0x01,
MethodPost = 0x02,
MethodPut = 0x03,
MethodDelete = 0x04,
MethodFetch = 0x05,
MethodPatch = 0x06,
MethodIPatch = 0x07,
SuccessCreated = 0x41,
SuccessDeleted = 0x42,
SuccessValid = 0x43,
SuccessChanged = 0x44,
SuccessContent = 0x45,
SuccessContinue = 0x5F,
ClientErrorBadRequest = 0x80,
ClientErrorUnauthorized = 0x81,
ClientErrorBadOption = 0x82,
ClientErrorForbidden = 0x83,
ClientErrorNotFound = 0x84,
ClientErrorMethodNotAllowed = 0x85,
ClientErrorNotAcceptable = 0x86,
ClientErrorRequestEntityIncomplete = 0x88,
ClientErrorPreconditionFailed = 0x8C,
ClientErrorRequestEntityTooLarge = 0x8D,
ClientErrorUnsupportedMediaType = 0x8F,
ClientErrorTooManyRequests = calc_code(4, 29),
ServerErrorInternalServerError = 0xA0,
ServerErrorNotImplemented = 0xA1,
ServerErrorBadGateway = 0xA2,
ServerErrorServiceUnavailable = 0xA3,
ServerErrorGatewayTimeout = 0xA4,
ServerErrorProxyingNotSupported = 0xA5,
SignalCsm = 0xE1,
SignalPing = 0xE2,
SignalPong = 0xE3,
SignalRelease = 0xE4,
SignalAbort = 0xE5,
}
impl MsgCode {
pub fn try_from(x: u8) -> Option<MsgCode> {
use MsgCode::*;
match x {
0x00 => Some(Empty),
0x01 => Some(MethodGet),
0x02 => Some(MethodPost),
0x03 => Some(MethodPut),
0x04 => Some(MethodDelete),
0x41 => Some(SuccessCreated),
0x42 => Some(SuccessDeleted),
0x43 => Some(SuccessValid),
0x44 => Some(SuccessChanged),
0x45 => Some(SuccessContent),
0x5F => Some(SuccessContinue),
0x80 => Some(ClientErrorBadRequest),
0x81 => Some(ClientErrorUnauthorized),
0x82 => Some(ClientErrorBadOption),
0x83 => Some(ClientErrorForbidden),
0x84 => Some(ClientErrorNotFound),
0x85 => Some(ClientErrorMethodNotAllowed),
0x86 => Some(ClientErrorNotAcceptable),
0x88 => Some(ClientErrorRequestEntityIncomplete),
0x8C => Some(ClientErrorPreconditionFailed),
0x8D => Some(ClientErrorRequestEntityTooLarge),
0x8F => Some(ClientErrorUnsupportedMediaType),
0x9D => Some(ClientErrorTooManyRequests),
0xA0 => Some(ServerErrorInternalServerError),
0xA1 => Some(ServerErrorNotImplemented),
0xA2 => Some(ServerErrorBadGateway),
0xA3 => Some(ServerErrorServiceUnavailable),
0xA4 => Some(ServerErrorGatewayTimeout),
0xA5 => Some(ServerErrorProxyingNotSupported),
0xE1 => Some(SignalCsm),
0xE2 => Some(SignalPing),
0xE3 => Some(SignalPong),
0xE4 => Some(SignalRelease),
0xE5 => Some(SignalAbort),
_ => None,
}
}
pub fn to_http_code(self) -> u16 {
((self as u8) >> 5) as u16 * 100 + (self as u8 as u16) & 0b11111
}
pub fn is_empty(self) -> bool {
self as u8 == 0
}
pub fn is_method(self) -> bool {
MsgCodeClass::Method.contains(self)
}
pub fn is_client_error(self) -> bool {
MsgCodeClass::ClientError.contains(self)
}
pub fn is_server_error(self) -> bool {
MsgCodeClass::ServerError.contains(self)
}
pub fn is_error(self) -> bool {
self.is_client_error() || self.is_server_error()
}
pub fn is_success(self) -> bool {
MsgCodeClass::Success.contains(self)
}
pub fn is_signal(self) -> bool {
MsgCodeClass::Signal.contains(self)
}
}
impl Default for MsgCode {
fn default() -> Self {
MsgCode::Empty
}
}
impl core::convert::From<MsgCode> for u8 {
fn from(code: MsgCode) -> Self {
code as u8
}
}
impl core::convert::From<MsgCode> for u16 {
fn from(code: MsgCode) -> Self {
code as u16
}
}
impl core::convert::From<MsgCode> for u32 {
fn from(code: MsgCode) -> Self {
code as u32
}
}