1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::{Buffer, DnsMessage, DnsMessageError};
use crate::characters::Characters;
use crate::parse::Parse;
use crate::rdata::{RData, RDataParse};
use crate::write::WriteBytes;

/// # Host information
/// This record is used to return host information
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct HInfo<'a> {
    /// The CPU type
    pub cpu: Characters<'a>,
    /// The OS type
    pub os: Characters<'a>,
}

impl<'a> RDataParse<'a> for HInfo<'a> {
    #[inline]
    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
        let cpu = Characters::parse(rdata, i)?;
        let os = Characters::parse(rdata, i)?;

        Ok(Self {
            cpu,
            os,
        })
    }
}

impl<'a> WriteBytes for HInfo<'a> {
    #[inline]
    fn write<
        const PTR_STORAGE: usize,
        const DNS_SECTION: usize,
        B: Buffer,
    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
        let mut bytes = 0;

        bytes += self.cpu.write(message)?;
        bytes += self.os.write(message)?;

        Ok(bytes)
    }
}

#[cfg(test)]
mod test {
    use crate::rdata::testutils::parse_write_test;

    use super::*;

    parse_write_test!(
        8,
        [
            3, b'w', b'w', b'w',
            3, b'c', b'o', b'm',
        ],
        HInfo {
            cpu: unsafe { Characters::new_unchecked(b"www") },
            os: unsafe { Characters::new_unchecked(b"com") },
        },
    );
}