use std::fmt::{self};
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
pub struct Code {
code: u64,
}
impl Code {
pub const fn value(&self) -> u64 {
self.code
}
}
impl PartialEq<u64> for Code {
fn eq(&self, other: &u64) -> bool {
*other == self.code
}
}
macro_rules! codes {
(
$(
$(#[$docs:meta])*
($num:expr, $name:ident);
)+
) => {
impl Code {
$(
$(#[$docs])*
pub const $name: Code = Code{code: $num};
)+
}
impl fmt::Debug for Code {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.code {
$(
$num => f.write_str(stringify!($name)),
)+
other => write!(f, "{:#x}", other),
}
}
}
impl fmt::Display for Code{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.code {
$(
$num => f.write_str(stringify!($name)),
)+
other => write!(f, "{:#x}", other),
}
}
}
}
}
codes! {
(0x33, H3_DATAGRAM_ERROR);
(0x100, H3_NO_ERROR);
(0x101, H3_GENERAL_PROTOCOL_ERROR);
(0x102, H3_INTERNAL_ERROR);
(0x103, H3_STREAM_CREATION_ERROR);
(0x104, H3_CLOSED_CRITICAL_STREAM);
(0x105, H3_FRAME_UNEXPECTED);
(0x106, H3_FRAME_ERROR);
(0x107, H3_EXCESSIVE_LOAD);
(0x108, H3_ID_ERROR);
(0x109, H3_SETTINGS_ERROR);
(0x10a, H3_MISSING_SETTINGS);
(0x10b, H3_REQUEST_REJECTED);
(0x10c, H3_REQUEST_CANCELLED);
(0x10d, H3_REQUEST_INCOMPLETE);
(0x10e, H3_MESSAGE_ERROR);
(0x10f, H3_CONNECT_ERROR);
(0x110, H3_VERSION_FALLBACK);
(0x200, QPACK_DECOMPRESSION_FAILED);
(0x201, QPACK_ENCODER_STREAM_ERROR);
(0x202, QPACK_DECODER_STREAM_ERROR);
}
impl From<Code> for u64 {
fn from(code: Code) -> u64 {
code.code
}
}
impl From<u64> for Code {
fn from(code: u64) -> Code {
Code { code }
}
}