simple_dns/dns/rdata/
isdn.rs

1use crate::{
2    bytes_buffer::BytesBuffer,
3    dns::{CharacterString, WireFormat},
4    lib::{Seek, Write},
5};
6
7use super::RR;
8
9/// An ISDN (Integrated Service Digital Network) number is simply a telephone number.
10#[derive(Debug, PartialEq, Eq, Hash, Clone)]
11pub struct ISDN<'a> {
12    /// A [CharacterString](`CharacterString`) which specifies the address.
13    pub address: CharacterString<'a>,
14    /// A [CharacterString](`CharacterString`) which specifies the subaddress.
15    pub sa: CharacterString<'a>,
16}
17
18impl RR for ISDN<'_> {
19    const TYPE_CODE: u16 = 20;
20}
21
22impl ISDN<'_> {
23    /// Transforms the inner data into its owned type
24    pub fn into_owned<'b>(self) -> ISDN<'b> {
25        ISDN {
26            address: self.address.into_owned(),
27            sa: self.sa.into_owned(),
28        }
29    }
30}
31
32impl<'a> WireFormat<'a> for ISDN<'a> {
33    const MINIMUM_LEN: usize = 0;
34    fn parse(data: &mut BytesBuffer<'a>) -> crate::Result<Self>
35    where
36        Self: Sized,
37    {
38        let address = CharacterString::parse(data)?;
39        let sa = CharacterString::parse(data)?;
40
41        Ok(Self { address, sa })
42    }
43
44    fn write_to<T: Write>(&self, out: &mut T) -> crate::Result<()> {
45        self.address.write_to(out)?;
46        self.sa.write_to(out)
47    }
48
49    fn write_compressed_to<T: Write + Seek>(
50        &'a self,
51        out: &mut T,
52        name_refs: &mut crate::lib::BTreeMap<&[crate::Label<'a>], u16>,
53    ) -> crate::Result<()> {
54        self.address.write_compressed_to(out, name_refs)?;
55        self.sa.write_compressed_to(out, name_refs)
56    }
57
58    fn len(&self) -> usize {
59        self.address.len() + self.sa.len()
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use crate::lib::{ToString, Vec};
67
68    #[test]
69    fn parse_and_write_isdn() {
70        let isdn = ISDN {
71            address: CharacterString::new(b"150862028003217").unwrap(),
72            sa: CharacterString::new(b"004").unwrap(),
73        };
74
75        let mut data = Vec::new();
76        assert!(isdn.write_to(&mut data).is_ok());
77
78        let isdn = ISDN::parse(&mut (&data[..]).into());
79        assert!(isdn.is_ok());
80        let isdn = isdn.unwrap();
81
82        assert_eq!(data.len(), isdn.len());
83        assert_eq!("150862028003217", isdn.address.to_string());
84        assert_eq!("004", isdn.sa.to_string());
85    }
86
87    #[test]
88    #[cfg(feature = "std")]
89    fn parse_sample() -> Result<(), Box<dyn std::error::Error>> {
90        use crate::{rdata::RData, ResourceRecord};
91        let sample_file = std::fs::read("samples/zonefile/ISDN.sample")?;
92
93        let sample_rdata = match ResourceRecord::parse(&mut (&sample_file[..]).into())?.rdata {
94            RData::ISDN(rdata) => rdata,
95            _ => unreachable!(),
96        };
97
98        assert_eq!(sample_rdata.address, "isdn-address".try_into()?);
99        assert_eq!(sample_rdata.sa, "subaddress".try_into()?);
100        Ok(())
101    }
102}