use crate::{read_u16_be, write_u16_be, DnsError};
use fixed_buffer::FixedBuf;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum DnsClass {
Internet,
Any,
Unknown(u16),
}
impl DnsClass {
#[must_use]
pub fn new(value: u16) -> Self {
match value {
1 => DnsClass::Internet,
255 => DnsClass::Any,
other => DnsClass::Unknown(other),
}
}
#[must_use]
pub fn num(&self) -> u16 {
match self {
DnsClass::Internet => 1,
DnsClass::Any => 255,
DnsClass::Unknown(other) => *other,
}
}
pub fn read<const N: usize>(buf: &mut FixedBuf<N>) -> Result<Self, DnsError> {
Ok(Self::new(read_u16_be(buf)?))
}
pub fn write<const N: usize>(&self, out: &mut FixedBuf<N>) -> Result<(), DnsError> {
write_u16_be(out, self.num())
}
}