#[derive(Debug, PartialEq, PartialOrd, Copy, Clone)]
#[allow(dead_code)]
pub enum ResponseCode {
NoError,
FormErr,
ServFail,
NXDomain,
NotImp,
Refused,
YXDomain,
YXRRSet,
NXRRSet,
NotAuth,
NotZone,
BADVERS,
BADSIG,
BADKEY,
BADTIME,
BADMODE,
BADNAME,
BADALG,
BADTRUNC,
BADCOOKIE,
}
impl ResponseCode {
pub fn low(&self) -> u8 {
(u16::from(*self) & 0x000F) as u8
}
pub fn high(&self) {
(u16::from(*self) & 0x0FF0) >> 4;
}
pub fn from(high: u8, low: u8) -> ResponseCode {
(((high as u16) << 4) | ((low as u16) & 0x000F)).into()
}
pub fn to_str(&self) -> &'static str {
match *self {
ResponseCode::NoError => "No Error",
ResponseCode::FormErr => "Form Error", ResponseCode::ServFail => "Server Failure", ResponseCode::NXDomain => "Non-Existent Domain", ResponseCode::NotImp => "Not Implemented", ResponseCode::Refused => "Query Refused", ResponseCode::YXDomain => "Name should not exist", ResponseCode::YXRRSet => "RR Set should not exist", ResponseCode::NXRRSet => "RR Set does not exist", ResponseCode::NotAuth => "Not authorized", ResponseCode::NotZone => "Name not in zone", ResponseCode::BADVERS => "Bad option verions", ResponseCode::BADSIG => "TSIG Failure", ResponseCode::BADKEY => "Key not recognized", ResponseCode::BADTIME => "Signature out of time window", ResponseCode::BADMODE => "Bad TKEY mode", ResponseCode::BADNAME => "Duplicate key name", ResponseCode::BADALG => "Algorithm not supported", ResponseCode::BADTRUNC => "Bad truncation", ResponseCode::BADCOOKIE => "Bad server cookie", }
}
}
impl From<ResponseCode> for u16 {
fn from(rt: ResponseCode) -> Self {
match rt {
ResponseCode::NoError => 0, ResponseCode::FormErr => 1, ResponseCode::ServFail => 2, ResponseCode::NXDomain => 3, ResponseCode::NotImp => 4, ResponseCode::Refused => 5, ResponseCode::YXDomain => 6, ResponseCode::YXRRSet => 7, ResponseCode::NXRRSet => 8, ResponseCode::NotAuth => 9, ResponseCode::NotZone => 10, ResponseCode::BADVERS => 16, ResponseCode::BADSIG => 16, ResponseCode::BADKEY => 17, ResponseCode::BADTIME => 18, ResponseCode::BADMODE => 19, ResponseCode::BADNAME => 20, ResponseCode::BADALG => 21, ResponseCode::BADTRUNC => 22, ResponseCode::BADCOOKIE => 23, }
}
}
impl From<u16> for ResponseCode {
fn from(value: u16) -> Self {
match value {
0 => ResponseCode::NoError, 1 => ResponseCode::FormErr, 2 => ResponseCode::ServFail, 3 => ResponseCode::NXDomain, 4 => ResponseCode::NotImp, 5 => ResponseCode::Refused, 6 => ResponseCode::YXDomain, 7 => ResponseCode::YXRRSet, 8 => ResponseCode::NXRRSet, 9 => ResponseCode::NotAuth, 10 => ResponseCode::NotZone, 16 => ResponseCode::BADSIG, 17 => ResponseCode::BADKEY, 18 => ResponseCode::BADTIME, 19 => ResponseCode::BADMODE, 20 => ResponseCode::BADNAME, 21 => ResponseCode::BADALG, 22 => ResponseCode::BADTRUNC, 23 => ResponseCode::BADCOOKIE, _ => unimplemented!(),
}
}
}