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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2017-2021 Lukas Pustina <lukas@pustina.de>
//
// Derived from trust-dns by Benjamin Fry <benjaminfry@me.com>
// cf. https://github.com/bluejekyll/trust-dns
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use crate::{Error, Result};

use serde::Serialize;
use std::fmt;
use std::str::FromStr;
use trust_dns_resolver::proto::rr::dnssec::rdata::DNSSECRecordType;

#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
#[allow(dead_code)]
#[derive(Serialize)]
pub enum RecordType {
    A,
    AAAA,
    ANAME,
    ANY,
    AXFR,
    CAA,
    CNAME,
    IXFR,
    MX,
    NAPTR,
    NS,
    NULL,
    OPENPGPKEY,
    OPT,
    PTR,
    SOA,
    SRV,
    SSHFP,
    TLSA,
    TXT,
    // TODO: DNSSEC(DNSSECRecordType),
    DNSSEC,
    Unknown(u16),
    ZERO,
}

impl RecordType {
    #[inline]
    pub fn is_any(self) -> bool {
        self == RecordType::ANY
    }

    #[inline]
    pub fn is_cname(self) -> bool {
        self == RecordType::CNAME
    }

    #[inline]
    pub fn is_srv(self) -> bool {
        self == RecordType::SRV
    }

    #[inline]
    pub fn is_ip_addr(self) -> bool {
        matches!(self, RecordType::A | RecordType::AAAA)
    }

    #[inline]
    pub fn is_unknown(self) -> bool {
        matches!(self, RecordType::Unknown(_))
    }

    #[inline]
    pub fn all() -> Vec<RecordType> {
        use RecordType::*;
        vec![
            A, AAAA, ANAME, ANY, AXFR, CAA, CNAME, IXFR, MX, NAPTR, NS, NULL, OPENPGPKEY, OPT, PTR, SOA, SRV, SSHFP,
            TLSA, TXT, DNSSEC, ZERO,
        ]
    }
}

#[doc(hidden)]
impl From<RecordType> for trust_dns_resolver::proto::rr::RecordType {
    fn from(rt: RecordType) -> Self {
        use trust_dns_resolver::proto::rr::RecordType as Trt;

        match rt {
            RecordType::A => Trt::A,
            RecordType::AAAA => Trt::AAAA,
            RecordType::ANAME => Trt::ANAME,
            RecordType::ANY => Trt::ANY,
            RecordType::AXFR => Trt::AXFR,
            RecordType::CAA => Trt::CAA,
            RecordType::CNAME => Trt::CNAME,
            RecordType::IXFR => Trt::IXFR,
            RecordType::MX => Trt::MX,
            RecordType::NAPTR => Trt::NAPTR,
            RecordType::NS => Trt::NS,
            RecordType::NULL => Trt::NULL,
            RecordType::OPENPGPKEY => Trt::OPENPGPKEY,
            RecordType::OPT => Trt::OPT,
            RecordType::PTR => Trt::PTR,
            RecordType::SOA => Trt::SOA,
            RecordType::SRV => Trt::SRV,
            RecordType::SSHFP => Trt::SSHFP,
            RecordType::TLSA => Trt::TLSA,
            RecordType::TXT => Trt::TXT,
            // TODO: RecordType::DNSSEC(dnssec_rt) => Trt::DNSSEC(dnssec_rt),
            RecordType::DNSSEC => Trt::DNSSEC(DNSSECRecordType::Unknown(0)),
            RecordType::Unknown(value) => Trt::Unknown(value),
            RecordType::ZERO => Trt::ZERO,
        }
    }
}

#[doc(hidden)]
impl From<trust_dns_resolver::proto::rr::RecordType> for RecordType {
    fn from(rt: trust_dns_resolver::proto::rr::RecordType) -> Self {
        use trust_dns_resolver::proto::rr::RecordType as Trt;

        match rt {
            Trt::A => RecordType::A,
            Trt::AAAA => RecordType::AAAA,
            Trt::ANAME => RecordType::ANAME,
            Trt::ANY => RecordType::ANY,
            Trt::AXFR => RecordType::AXFR,
            Trt::CAA => RecordType::CAA,
            Trt::CNAME => RecordType::CNAME,
            Trt::IXFR => RecordType::IXFR,
            Trt::MX => RecordType::MX,
            Trt::NAPTR => RecordType::NAPTR,
            Trt::NS => RecordType::NS,
            Trt::NULL => RecordType::NULL,
            Trt::OPENPGPKEY => RecordType::OPENPGPKEY,
            Trt::OPT => RecordType::OPT,
            Trt::PTR => RecordType::PTR,
            Trt::SOA => RecordType::SOA,
            Trt::SRV => RecordType::SRV,
            Trt::SSHFP => RecordType::SSHFP,
            Trt::TLSA => RecordType::TLSA,
            Trt::TXT => RecordType::TXT,
            // TODO: Trt::DNSSEC(dnssec_rt) => RecordType::DNSSEC(dnssec_rt),
            Trt::DNSSEC(_) => RecordType::DNSSEC,
            Trt::Unknown(value) => RecordType::Unknown(value),
            Trt::ZERO => RecordType::ZERO,
        }
    }
}

impl FromStr for RecordType {
    type Err = Error;

    fn from_str(str: &str) -> Result<Self> {
        match str {
            "A" => Ok(RecordType::A),
            "AAAA" => Ok(RecordType::AAAA),
            "ANAME" => Ok(RecordType::ANAME),
            "CAA" => Ok(RecordType::CAA),
            "CNAME" => Ok(RecordType::CNAME),
            "NULL" => Ok(RecordType::NULL),
            "MX" => Ok(RecordType::MX),
            "NAPTR" => Ok(RecordType::NAPTR),
            "NS" => Ok(RecordType::NS),
            "OPENPGPKEY" => Ok(RecordType::OPENPGPKEY),
            "PTR" => Ok(RecordType::PTR),
            "SOA" => Ok(RecordType::SOA),
            "SRV" => Ok(RecordType::SRV),
            "SSHFP" => Ok(RecordType::SSHFP),
            "TLSA" => Ok(RecordType::TLSA),
            "TXT" => Ok(RecordType::TXT),
            "ANY" | "*" => Ok(RecordType::ANY),
            "AXFR" => Ok(RecordType::AXFR),
            "DNSKEY" | "DS" | "KEY" | "NSEC" | "NSEC3" | "NSEC3PARAM" | "RRSIG" | "SIG" => {
                // TODO: Ok(RecordType::DNSSEC(str.parse()?))
                Ok(RecordType::DNSSEC)
            }
            _ => Err(Error::ParserError {
                what: str.to_string(),
                to: "RecordType",
                why: "invalid record type".to_string(),
            }),
        }
    }
}

impl From<RecordType> for &'static str {
    fn from(rt: RecordType) -> &'static str {
        match rt {
            RecordType::A => "A",
            RecordType::AAAA => "AAAA",
            RecordType::ANAME => "ANAME",
            RecordType::ANY => "ANY",
            RecordType::AXFR => "AXFR",
            RecordType::CAA => "CAA",
            RecordType::CNAME => "CNAME",
            RecordType::ZERO => "",
            RecordType::IXFR => "IXFR",
            RecordType::MX => "MX",
            RecordType::NAPTR => "NAPTR",
            RecordType::NS => "NS",
            RecordType::NULL => "NULL",
            RecordType::OPENPGPKEY => "OPENPGPKEY",
            RecordType::OPT => "OPT",
            RecordType::PTR => "PTR",
            RecordType::SOA => "SOA",
            RecordType::SRV => "SRV",
            RecordType::SSHFP => "SSHFP",
            RecordType::TLSA => "TLSA",
            RecordType::TXT => "TXT",
            // TODO: RecordType::DNSSEC(rt) => rt.into(),
            RecordType::DNSSEC => "DNSSEC",
            RecordType::Unknown(_) => "Unknown",
        }
    }
}

impl fmt::Display for RecordType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(Into::<&str>::into(*self))
    }
}