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
12#[cfg(feature = "dnssec")]
13#[cfg_attr(docsrs, doc(cfg(feature = "dnssec")))]
14pub mod dnssec;
15pub mod domain;
16mod lower_name;
17pub mod rdata;
18pub mod record_data;
19pub mod record_type;
20pub mod resource;
21mod rr_key;
22mod rr_set;
23pub mod type_bit_map;
24
25use std::fmt;
26
27use crate::{
28    error::ProtoResult,
29    serialize::binary::{BinDecodable, BinDecoder, BinEncodable, Restrict},
30};
31
32pub use self::dns_class::DNSClass;
33pub use self::domain::{IntoName, Name, TryParseIp};
34pub use self::record_data::RData;
35pub use self::record_type::RecordType;
36pub use self::resource::Record;
37#[allow(deprecated)]
38pub use self::rr_set::IntoRecordSet;
39pub use self::rr_set::RecordSet;
40pub use self::rr_set::RrsetRecords;
41pub use lower_name::LowerName;
42pub use rr_key::RrKey;
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 + fmt::Display + 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
62trait RecordDataDecodable<'r>: Sized {
63    /// Read the RecordData from the data stream.
64    ///
65    /// * `decoder` - data stream from which the RData will be read
66    /// * `record_type` - specifies the RecordType that has already been read from the stream
67    /// * `length` - the data length that should be read from the stream for this RecordData
68    fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self>;
69}
70
71impl<'r, T> RecordDataDecodable<'r> for T
72where
73    T: 'r + BinDecodable<'r> + Sized,
74{
75    fn read_data(decoder: &mut BinDecoder<'r>, _length: Restrict<u16>) -> ProtoResult<Self> {
76        T::read(decoder)
77    }
78}