use std::collections::HashMap;
use crate::dns::{name::Label, Name, PacketPart};
use super::RR;
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct MINFO<'a> {
pub rmailbox: Name<'a>,
pub emailbox: Name<'a>,
}
impl<'a> RR for MINFO<'a> {
const TYPE_CODE: u16 = 14;
}
impl<'a> MINFO<'a> {
pub fn into_owned<'b>(self) -> MINFO<'b> {
MINFO {
rmailbox: self.rmailbox.into_owned(),
emailbox: self.emailbox.into_owned(),
}
}
}
impl<'a> PacketPart<'a> for MINFO<'a> {
fn parse(data: &'a [u8], position: usize) -> crate::Result<Self>
where
Self: Sized,
{
let rmailbox = Name::parse(data, position)?;
let emailbox = Name::parse(data, position + rmailbox.len())?;
Ok(Self { rmailbox, emailbox })
}
fn write_to<T: std::io::Write>(&self, out: &mut T) -> crate::Result<()> {
self.rmailbox.write_to(out)?;
self.emailbox.write_to(out)
}
fn write_compressed_to<T: std::io::Write + std::io::Seek>(
&'a self,
out: &mut T,
name_refs: &mut HashMap<&'a [Label<'a>], usize>,
) -> crate::Result<()> {
self.rmailbox.write_compressed_to(out, name_refs)?;
self.emailbox.write_compressed_to(out, name_refs)
}
fn len(&self) -> usize {
self.rmailbox.len() + self.emailbox.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_and_write_minfo() {
let minfo = MINFO {
rmailbox: Name::new("r.mailbox.com").unwrap(),
emailbox: Name::new("e.mailbox.com").unwrap(),
};
let mut data = Vec::new();
assert!(minfo.write_to(&mut data).is_ok());
let minfo = MINFO::parse(&data, 0);
assert!(minfo.is_ok());
let minfo = minfo.unwrap();
assert_eq!(data.len(), minfo.len());
assert_eq!("r.mailbox.com", minfo.rmailbox.to_string());
assert_eq!("e.mailbox.com", minfo.emailbox.to_string());
}
}