pub fn format_dotted(code: u8, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}.{:02}", code >> 5, code & 0x1f)
}
#[cfg(feature = "alloc")]
pub fn to_dotted(code: u8) -> alloc::string::String {
alloc::format!("{}.{:02}", code >> 5, code & 0x1f)
}
#[non_exhaustive]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Class {
Success,
ClientError,
ServerError,
}
#[non_exhaustive]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Range {
Empty,
Request,
Response(Class),
Signaling,
Reserved,
}
#[inline]
#[must_use]
pub fn classify(code: u8) -> Range {
match code {
0 => Range::Empty,
0x01..=0x1f => Range::Request,
0x40..=0x5f => Range::Response(Class::Success),
0x80..=0x9f => Range::Response(Class::ClientError),
0xa0..=0xbf => Range::Response(Class::ServerError),
0xe0..=0xff => Range::Signaling,
_ => Range::Reserved,
}
}
macro_rules! code {
( $class:tt . $detail:tt ) => {
($class << 5) + $detail
};
}
macro_rules! codes {
( $( $name:tt $constname:ident $class:tt $detail:expr ) , * ) => { $(
#[doc=$name]
pub const $constname: u8 = code!($class.$detail);
)*
#[must_use]
pub fn to_name(code: u8) -> Option<&'static str> {
match code {
$(
$constname => Some($name),
)*
_ => None
}
}
}
}
codes!(
"Empty" EMPTY 0 0,
"GET" GET 0 1,
"POST" POST 0 2,
"PUT" PUT 0 3,
"DELETE" DELETE 0 4,
"FETCH" FETCH 0 5,
"PATCH" PATCH 0 6,
"iPATCH" IPATCH 0 7,
"Created" CREATED 2 1,
"Deleted" DELETED 2 2,
"Valid" VALID 2 3,
"Changed" CHANGED 2 4,
"Content" CONTENT 2 5,
"Continue" CONTINUE 2 31,
"Bad Request" BAD_REQUEST 4 0,
"Unauthorized" UNAUTHORIZED 4 1,
"Bad Option" BAD_OPTION 4 2,
"Forbidden" FORBIDDEN 4 3,
"Not Found" NOT_FOUND 4 4,
"Method Not Allowed" METHOD_NOT_ALLOWED 4 5,
"Not Acceptable" NOT_ACCEPTABLE 4 6,
"Request Entity Incomplete" REQUEST_ENTITY_INCOMPLETE 4 8,
"Conflict" CONFLICT 4 9,
"Precondition Failed" PRECONDITION_FAILED 4 12,
"Request Entity Too Large" REQUEST_ENTITY_TOO_LARGE 4 13,
"Unsupported Content-Format" UNSUPPORTED_CONTENT_FORMAT 4 15,
"Unprocessable Entity" UNPROCESSABLE_ENTITY 4 22,
"Too Many Requests" TOO_MANY_REQUESTS 4 29,
"Internal Server Error" INTERNAL_SERVER_ERROR 5 0,
"Not Implemented" NOT_IMPLEMENTED 5 1,
"Bad Gateway" BAD_GATEWAY 5 2,
"Service Unavailable" SERVICE_UNAVAILABLE 5 3,
"Gateway Timeout" GATEWAY_TIMEOUT 5 4,
"Proxying Not Supported" PROXYING_NOT_SUPPORTED 5 5,
"Hop Limit Reached" HOP_LIMIT_REACHED 5 8,
"CSM" CSM 7 1,
"Ping" PING 7 2,
"Pong" PONG 7 3,
"Release" RELEASE 7 4,
"Abort" ABORT 7 5
);