use core::fmt;
use domain_macros::*;
use crate::new::base::wire::U16;
#[derive(
PartialEq, Eq, Hash, AsBytes, BuildBytes, ParseBytesZC, UnsizedCopy,
)]
#[repr(C)]
pub struct ExtError {
pub code: ExtErrorCode,
text: str,
}
impl ExtError {
pub fn text(&self) -> Option<&str> {
if !self.text.is_empty() {
Some(self.text.strip_suffix('\0').unwrap_or(&self.text))
} else {
None
}
}
}
impl fmt::Debug for ExtError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ExtError")
.field("code", &self.code)
.field("text", &self.text())
.finish()
}
}
#[derive(
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
AsBytes,
BuildBytes,
ParseBytes,
ParseBytesZC,
SplitBytes,
SplitBytesZC,
UnsizedCopy,
)]
#[repr(transparent)]
pub struct ExtErrorCode {
pub code: U16,
}
impl ExtErrorCode {
const fn new(code: u16) -> Self {
Self {
code: U16::new(code),
}
}
pub const OTHER: Self = Self::new(0);
pub const BAD_DNSKEY_ALG: Self = Self::new(1);
pub const BAD_DS_ALG: Self = Self::new(2);
pub const STALE_ANSWER: Self = Self::new(3);
pub const FORGED_ANSWER: Self = Self::new(4);
pub const DNSSEC_INDETERMINATE: Self = Self::new(5);
pub const DNSSEC_BOGUS: Self = Self::new(6);
pub const SIG_EXPIRED: Self = Self::new(7);
pub const SIG_FUTURE: Self = Self::new(8);
pub const DNSKEY_MISSING: Self = Self::new(9);
pub const RRSIGS_MISSING: Self = Self::new(10);
pub const NOT_ZSK: Self = Self::new(11);
pub const NSEC_MISSING: Self = Self::new(12);
pub const CACHED_ERROR: Self = Self::new(13);
pub const NOT_READY: Self = Self::new(14);
pub const BLOCKED: Self = Self::new(15);
pub const CENSORED: Self = Self::new(16);
pub const FILTERED: Self = Self::new(17);
pub const PROHIBITED: Self = Self::new(18);
pub const STALE_NXDOMAIN: Self = Self::new(19);
pub const NOT_AUTHORITATIVE: Self = Self::new(20);
pub const NOT_SUPPORTED: Self = Self::new(21);
pub const NO_REACHABLE_AUTHORITY: Self = Self::new(22);
pub const NETWORK_ERROR: Self = Self::new(23);
pub const INVALID_DATA: Self = Self::new(24);
pub const TOO_EARLY: Self = Self::new(26);
pub const BAD_NSEC3_ITERS: Self = Self::new(27);
}
impl ExtErrorCode {
pub fn is_private(&self) -> bool {
self.code >= 49152
}
}
impl fmt::Debug for ExtErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let text = match *self {
Self::OTHER => "other",
Self::BAD_DNSKEY_ALG => "unsupported DNSKEY algorithm",
Self::BAD_DS_ALG => "unspported DS digest type",
Self::STALE_ANSWER => "stale answer",
Self::FORGED_ANSWER => "forged answer",
Self::DNSSEC_INDETERMINATE => "DNSSEC indeterminate",
Self::DNSSEC_BOGUS => "DNSSEC bogus",
Self::SIG_EXPIRED => "signature expired",
Self::SIG_FUTURE => "signature not yet valid",
Self::DNSKEY_MISSING => "DNSKEY missing",
Self::RRSIGS_MISSING => "RRSIGs missing",
Self::NOT_ZSK => "no zone key bit set",
Self::NSEC_MISSING => "nsec missing",
Self::CACHED_ERROR => "cached error",
Self::NOT_READY => "not ready",
Self::BLOCKED => "blocked",
Self::CENSORED => "censored",
Self::FILTERED => "filtered",
Self::PROHIBITED => "prohibited",
Self::STALE_NXDOMAIN => "stale NXDOMAIN answer",
Self::NOT_AUTHORITATIVE => "not authoritative",
Self::NOT_SUPPORTED => "not supported",
Self::NO_REACHABLE_AUTHORITY => "no reachable authority",
Self::NETWORK_ERROR => "network error",
Self::INVALID_DATA => "invalid data",
Self::TOO_EARLY => "too early",
Self::BAD_NSEC3_ITERS => "unsupported NSEC3 iterations value",
_ => {
return f
.debug_tuple("ExtErrorCode")
.field(&self.code.get())
.finish();
}
};
f.debug_tuple("ExtErrorCode")
.field(&self.code.get())
.field(&text)
.finish()
}
}