simple_dns/dns/rdata/
isdn.rs

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