use derive_more::{Display, IsVariant, TryUnwrap, Unwrap};
#[derive(
Debug, Display, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, IsVariant, Unwrap, TryUnwrap,
)]
#[display("{}", self.as_str())]
#[non_exhaustive]
pub enum ResponseCode {
NoError,
FormatError,
ServerFailure,
NameError,
NotImplemented,
Refused,
Unknown(u8),
}
impl ResponseCode {
pub const fn as_str(&self) -> &'static str {
match self {
Self::NoError => "no_error",
Self::FormatError => "format_error",
Self::ServerFailure => "server_failure",
Self::NameError => "name_error",
Self::NotImplemented => "not_implemented",
Self::Refused => "refused",
Self::Unknown(_) => "unknown",
}
}
#[inline(always)]
pub const fn to_u8(self) -> u8 {
match self {
Self::NoError => 0,
Self::FormatError => 1,
Self::ServerFailure => 2,
Self::NameError => 3,
Self::NotImplemented => 4,
Self::Refused => 5,
Self::Unknown(v) => v,
}
}
#[inline(always)]
pub const fn from_u8(v: u8) -> Self {
match v {
0 => Self::NoError,
1 => Self::FormatError,
2 => Self::ServerFailure,
3 => Self::NameError,
4 => Self::NotImplemented,
5 => Self::Refused,
other => Self::Unknown(other),
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests;