hickory_proto/rr/dnssec/rdata/
cdnskey.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//! CDNSKEY type and related implementations
9
10use std::{fmt, ops::Deref};
11
12#[cfg(feature = "serde-config")]
13use serde::{Deserialize, Serialize};
14
15use crate::{
16    error::ProtoResult,
17    rr::{RData, RecordData, RecordDataDecodable, RecordType},
18    serialize::binary::{BinDecoder, BinEncodable, BinEncoder, Restrict},
19};
20
21use super::{DNSSECRData, DNSKEY};
22
23/// RRSIG is really a derivation of the original SIG record data. See SIG for more documentation
24#[cfg_attr(feature = "serde-config", derive(Deserialize, Serialize))]
25#[derive(Debug, PartialEq, Eq, Hash, Clone)]
26pub struct CDNSKEY(DNSKEY);
27
28impl Deref for CDNSKEY {
29    type Target = DNSKEY;
30
31    fn deref(&self) -> &Self::Target {
32        &self.0
33    }
34}
35
36impl BinEncodable for CDNSKEY {
37    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
38        self.0.emit(encoder)
39    }
40}
41
42impl<'r> RecordDataDecodable<'r> for CDNSKEY {
43    fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
44        DNSKEY::read_data(decoder, length).map(Self)
45    }
46}
47
48impl RecordData for CDNSKEY {
49    fn try_from_rdata(data: RData) -> Result<Self, RData> {
50        match data {
51            RData::DNSSEC(DNSSECRData::CDNSKEY(cdnskey)) => Ok(cdnskey),
52            _ => Err(data),
53        }
54    }
55
56    fn try_borrow(data: &RData) -> Option<&Self> {
57        match data {
58            RData::DNSSEC(DNSSECRData::CDNSKEY(cdnskey)) => Some(cdnskey),
59            _ => None,
60        }
61    }
62
63    fn record_type(&self) -> RecordType {
64        RecordType::CDNSKEY
65    }
66
67    fn into_rdata(self) -> RData {
68        RData::DNSSEC(DNSSECRData::CDNSKEY(self))
69    }
70}
71
72impl fmt::Display for CDNSKEY {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
74        write!(f, "{}", self.0)
75    }
76}