hickory_proto/rr/rdata/
a.rs1pub use std::net::Ipv4Addr;
35use std::{fmt, net::AddrParseError, ops::Deref, str};
36
37#[cfg(feature = "serde-config")]
38use serde::{Deserialize, Serialize};
39
40use crate::{
41 error::*,
42 rr::{RData, RecordData, RecordType},
43 serialize::binary::*,
44};
45
46#[cfg_attr(feature = "serde-config", derive(Deserialize, Serialize))]
48#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
49pub struct A(pub Ipv4Addr);
50
51impl A {
52 pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Self {
54 Self(Ipv4Addr::new(a, b, c, d))
55 }
56}
57
58impl RecordData for A {
59 fn try_from_rdata(data: RData) -> Result<Self, crate::rr::RData> {
60 match data {
61 RData::A(ipv4) => Ok(ipv4),
62 _ => Err(data),
63 }
64 }
65
66 fn try_borrow(data: &RData) -> Option<&Self> {
67 match data {
68 RData::A(ipv4) => Some(ipv4),
69 _ => None,
70 }
71 }
72
73 fn record_type(&self) -> RecordType {
74 RecordType::A
75 }
76
77 fn into_rdata(self) -> RData {
78 RData::A(self)
79 }
80}
81
82impl BinEncodable for A {
83 fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
84 let segments = self.octets();
85
86 encoder.emit(segments[0])?;
87 encoder.emit(segments[1])?;
88 encoder.emit(segments[2])?;
89 encoder.emit(segments[3])?;
90 Ok(())
91 }
92}
93
94impl<'r> BinDecodable<'r> for A {
95 fn read(decoder: &mut BinDecoder<'r>) -> ProtoResult<Self> {
96 Ok(Ipv4Addr::new(
98 decoder.pop()?.unverified(),
99 decoder.pop()?.unverified(),
100 decoder.pop()?.unverified(),
101 decoder.pop()?.unverified(),
102 )
103 .into())
104 }
105}
106
107#[deprecated(note = "use the BinDecodable::read method instead")]
109pub fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<A> {
110 <A as BinDecodable>::read(decoder)
111}
112
113#[deprecated(note = "use the BinEncodable::emit method instead")]
115pub fn emit(encoder: &mut BinEncoder<'_>, address: Ipv4Addr) -> ProtoResult<()> {
116 BinEncodable::emit(&A::from(address), encoder)
117}
118
119impl From<Ipv4Addr> for A {
120 fn from(a: Ipv4Addr) -> Self {
121 Self(a)
122 }
123}
124
125impl From<A> for Ipv4Addr {
126 fn from(a: A) -> Self {
127 a.0
128 }
129}
130
131impl Deref for A {
132 type Target = Ipv4Addr;
133
134 fn deref(&self) -> &Self::Target {
135 &self.0
136 }
137}
138
139impl fmt::Display for A {
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
141 write!(f, "{}", self.0)
142 }
143}
144
145impl str::FromStr for A {
146 type Err = AddrParseError;
147 fn from_str(s: &str) -> Result<Self, AddrParseError> {
148 Ipv4Addr::from_str(s).map(From::from)
149 }
150}
151
152#[cfg(test)]
153mod mytests {
154 use std::str::FromStr;
155
156 use super::*;
157 use crate::serialize::binary::bin_tests::{test_emit_data_set, test_read_data_set};
158
159 fn get_data() -> Vec<(A, Vec<u8>)> {
160 vec![
161 (A::from_str("0.0.0.0").unwrap(), vec![0, 0, 0, 0]), (A::from_str("1.0.0.0").unwrap(), vec![1, 0, 0, 0]),
163 (A::from_str("0.1.0.0").unwrap(), vec![0, 1, 0, 0]),
164 (A::from_str("0.0.1.0").unwrap(), vec![0, 0, 1, 0]),
165 (A::from_str("0.0.0.1").unwrap(), vec![0, 0, 0, 1]),
166 (A::from_str("127.0.0.1").unwrap(), vec![127, 0, 0, 1]),
167 (
168 A::from_str("192.168.64.32").unwrap(),
169 vec![192, 168, 64, 32],
170 ),
171 ]
172 }
173
174 #[test]
175 fn test_parse() {
176 test_read_data_set(get_data(), |ref mut d| A::read(d));
177 }
178
179 #[test]
180 fn test_write_to() {
181 test_emit_data_set(get_data(), |ref mut e, d| d.emit(e));
182 }
183}