simple_dns/dns/rdata/
minfo.rs

1use std::collections::HashMap;
2
3use crate::{
4    bytes_buffer::BytesBuffer,
5    dns::{name::Label, Name, WireFormat},
6};
7
8use super::RR;
9
10/// MINFO recors are used to acquire mailbox or mail list information
11#[derive(Debug, PartialEq, Eq, Hash, Clone)]
12pub struct MINFO<'a> {
13    /// A [Name](`Name`) which specifies a mailbox which is responsible for the mailing list or mailbox.  
14    pub rmailbox: Name<'a>,
15    /// A [Name](`Name`) which specifies a mailbox which is to receive error messages related to  
16    /// the mailing list or mailbox specified by the owner of the MINFO RR
17    pub emailbox: Name<'a>,
18}
19
20impl RR for MINFO<'_> {
21    const TYPE_CODE: u16 = 14;
22}
23
24impl MINFO<'_> {
25    /// Transforms the inner data into its owned type
26    pub fn into_owned<'b>(self) -> MINFO<'b> {
27        MINFO {
28            rmailbox: self.rmailbox.into_owned(),
29            emailbox: self.emailbox.into_owned(),
30        }
31    }
32}
33
34impl<'a> WireFormat<'a> for MINFO<'a> {
35    const MINIMUM_LEN: usize = 0;
36    fn parse(data: &mut BytesBuffer<'a>) -> crate::Result<Self>
37    where
38        Self: Sized,
39    {
40        let rmailbox = Name::parse(data)?;
41        let emailbox = Name::parse(data)?;
42
43        Ok(Self { rmailbox, emailbox })
44    }
45
46    fn write_to<T: std::io::Write>(&self, out: &mut T) -> crate::Result<()> {
47        self.rmailbox.write_to(out)?;
48        self.emailbox.write_to(out)
49    }
50
51    fn write_compressed_to<T: std::io::Write + std::io::Seek>(
52        &'a self,
53        out: &mut T,
54        name_refs: &mut HashMap<&'a [Label<'a>], usize>,
55    ) -> crate::Result<()> {
56        self.rmailbox.write_compressed_to(out, name_refs)?;
57        self.emailbox.write_compressed_to(out, name_refs)
58    }
59
60    fn len(&self) -> usize {
61        self.rmailbox.len() + self.emailbox.len()
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn parse_and_write_minfo() {
71        let minfo = MINFO {
72            rmailbox: Name::new("r.mailbox.com").unwrap(),
73            emailbox: Name::new("e.mailbox.com").unwrap(),
74        };
75
76        let mut data = Vec::new();
77        assert!(minfo.write_to(&mut data).is_ok());
78
79        let minfo = MINFO::parse(&mut (&data[..]).into());
80        assert!(minfo.is_ok());
81        let minfo = minfo.unwrap();
82
83        assert_eq!(data.len(), minfo.len());
84        assert_eq!("r.mailbox.com", minfo.rmailbox.to_string());
85        assert_eq!("e.mailbox.com", minfo.emailbox.to_string());
86    }
87}