simple_dns/dns/rdata/
minfo.rs

1use crate::{
2    bytes_buffer::BytesBuffer,
3    dns::{Name, WireFormat},
4    lib::{Seek, Write},
5};
6
7use super::RR;
8
9/// MINFO recors are used to acquire mailbox or mail list information
10#[derive(Debug, PartialEq, Eq, Hash, Clone)]
11pub struct MINFO<'a> {
12    /// A [Name](`Name`) which specifies a mailbox which is responsible for the mailing list or mailbox.  
13    pub rmailbox: Name<'a>,
14    /// A [Name](`Name`) which specifies a mailbox which is to receive error messages related to  
15    /// the mailing list or mailbox specified by the owner of the MINFO RR
16    pub emailbox: Name<'a>,
17}
18
19impl RR for MINFO<'_> {
20    const TYPE_CODE: u16 = 14;
21}
22
23impl MINFO<'_> {
24    /// Transforms the inner data into its owned type
25    pub fn into_owned<'b>(self) -> MINFO<'b> {
26        MINFO {
27            rmailbox: self.rmailbox.into_owned(),
28            emailbox: self.emailbox.into_owned(),
29        }
30    }
31}
32
33impl<'a> WireFormat<'a> for MINFO<'a> {
34    const MINIMUM_LEN: usize = 0;
35    fn parse(data: &mut BytesBuffer<'a>) -> crate::Result<Self>
36    where
37        Self: Sized,
38    {
39        let rmailbox = Name::parse(data)?;
40        let emailbox = Name::parse(data)?;
41
42        Ok(Self { rmailbox, emailbox })
43    }
44
45    fn write_to<T: Write>(&self, out: &mut T) -> crate::Result<()> {
46        self.rmailbox.write_to(out)?;
47        self.emailbox.write_to(out)
48    }
49
50    fn write_compressed_to<T: Write + Seek>(
51        &'a self,
52        out: &mut T,
53        name_refs: &mut crate::lib::BTreeMap<&[crate::Label<'a>], u16>,
54    ) -> crate::Result<()> {
55        self.rmailbox.write_compressed_to(out, name_refs)?;
56        self.emailbox.write_compressed_to(out, name_refs)
57    }
58
59    fn len(&self) -> usize {
60        self.rmailbox.len() + self.emailbox.len()
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use crate::lib::{ToString, Vec};
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}