hickory_proto/rr/
mod.rs

1// Copyright 2015-2023 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// https://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// https://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! Resource record related components, e.g. `Name` aka label, `Record`, `RData`, ...
9
10pub mod dns_class;
11// TODO: rename to sec
12pub mod domain;
13mod lower_name;
14pub mod rdata;
15pub mod record_data;
16pub mod record_type;
17pub(crate) mod record_type_set;
18pub mod resource;
19mod rr_key;
20mod rr_set;
21pub mod serial_number;
22
23use core::fmt::{Debug, Display};
24
25use crate::{
26    error::ProtoResult,
27    serialize::binary::{BinDecodable, BinDecoder, BinEncodable, Restrict},
28};
29
30pub use self::dns_class::DNSClass;
31pub use self::domain::{IntoName, Name};
32pub use self::record_data::RData;
33pub use self::record_type::RecordType;
34pub(crate) use self::record_type_set::RecordTypeSet;
35pub use self::resource::Record;
36#[allow(deprecated)]
37pub use self::rr_set::IntoRecordSet;
38pub use self::rr_set::RecordSet;
39pub use self::rr_set::RrsetRecords;
40pub use lower_name::LowerName;
41pub use rr_key::RrKey;
42pub use serial_number::SerialNumber;
43
44/// RecordData that is stored in a DNS Record.
45///
46/// This trait allows for generic usage of `RecordData` types inside the `Record` type. Specific RecordData types can be used to enforce compile time constraints on a Record.
47pub trait RecordData: Clone + Sized + PartialEq + Eq + Display + Debug + BinEncodable {
48    /// Attempts to convert to this RecordData from the RData type, if it is not the correct type the original is returned
49    #[allow(clippy::result_large_err)]
50    fn try_from_rdata(data: RData) -> Result<Self, RData>;
51
52    /// Attempts to borrow this RecordData from the RData type, if it is not the correct type the original is returned
53    fn try_borrow(data: &RData) -> Option<&Self>;
54
55    /// Get the associated RecordType for the RecordData
56    fn record_type(&self) -> RecordType;
57
58    /// Converts this RecordData into generic RecordData
59    fn into_rdata(self) -> RData;
60
61    /// RDLENGTH = 0
62    fn is_update(&self) -> bool {
63        false
64    }
65}
66
67pub(crate) trait RecordDataDecodable<'r>: Sized {
68    /// Read the RecordData from the data stream.
69    ///
70    /// * `decoder` - data stream from which the RData will be read
71    /// * `record_type` - specifies the RecordType that has already been read from the stream
72    /// * `length` - the data length that should be read from the stream for this RecordData
73    fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self>;
74}
75
76impl<'r, T> RecordDataDecodable<'r> for T
77where
78    T: 'r + BinDecodable<'r> + Sized,
79{
80    fn read_data(decoder: &mut BinDecoder<'r>, _length: Restrict<u16>) -> ProtoResult<Self> {
81        T::read(decoder)
82    }
83}