flex_dns/rdata/
a.rs

1use crate::{Buffer, DnsMessage, DnsMessageError, MutBuffer};
2use crate::parse::Parse;
3use crate::rdata::{RData, RDataParse};
4use crate::write::WriteBytes;
5
6/// # A host address
7/// This record is used to return a ipv4 address for a host
8#[derive(Copy, Clone, Debug, PartialEq)]
9pub struct A {
10    /// The host ipv4 address
11    pub address: [u8; 4],
12}
13
14impl<'a> RDataParse<'a> for A {
15    #[inline]
16    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
17        Ok(Self { address: <[u8; 4]>::parse(rdata.buffer, i)? })
18    }
19}
20
21impl WriteBytes for A {
22    #[inline]
23    fn write<
24        const PTR_STORAGE: usize,
25        const DNS_SECTION: usize,
26        B: MutBuffer + Buffer,
27    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
28        self.address.write(message)
29    }
30}
31
32#[cfg(test)]
33mod test {
34    use crate::rdata::testutils::parse_write_test;
35
36    use super::*;
37
38    parse_write_test!(
39        4,
40        [0x01, 0x02, 0x03, 0x04],
41        A {
42            address: [0x01, 0x02, 0x03, 0x04],
43        },
44    );
45}