hickory_proto/rr/rdata/
openpgpkey.rs1use std::fmt;
10
11#[cfg(feature = "serde-config")]
12use serde::{Deserialize, Serialize};
13
14use crate::{
15 error::ProtoResult,
16 rr::{RData, RecordData, RecordDataDecodable, RecordType},
17 serialize::binary::{BinDecoder, BinEncodable, BinEncoder, Restrict},
18};
19
20#[cfg_attr(feature = "serde-config", derive(Deserialize, Serialize))]
28#[derive(Debug, PartialEq, Eq, Hash, Clone)]
29pub struct OPENPGPKEY {
30 public_key: Vec<u8>,
31}
32
33impl OPENPGPKEY {
34 pub fn new(public_key: Vec<u8>) -> Self {
41 Self { public_key }
42 }
43
44 pub fn public_key(&self) -> &[u8] {
47 &self.public_key
48 }
49}
50
51impl BinEncodable for OPENPGPKEY {
52 fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
53 encoder.emit_vec(self.public_key())
54 }
55}
56
57impl<'r> RecordDataDecodable<'r> for OPENPGPKEY {
58 fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
59 let rdata_length = length.map(usize::from).unverified();
60 let public_key =
61 decoder.read_vec(rdata_length)?.unverified();
62 Ok(Self::new(public_key))
63 }
64}
65
66impl RecordData for OPENPGPKEY {
67 fn try_from_rdata(data: RData) -> Result<Self, RData> {
68 match data {
69 RData::OPENPGPKEY(csync) => Ok(csync),
70 _ => Err(data),
71 }
72 }
73
74 fn try_borrow(data: &RData) -> Option<&Self> {
75 match data {
76 RData::OPENPGPKEY(csync) => Some(csync),
77 _ => None,
78 }
79 }
80
81 fn record_type(&self) -> RecordType {
82 RecordType::OPENPGPKEY
83 }
84
85 fn into_rdata(self) -> RData {
86 RData::OPENPGPKEY(self)
87 }
88}
89
90impl fmt::Display for OPENPGPKEY {
103 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
104 f.write_str(&data_encoding::BASE64.encode(&self.public_key))
105 }
106}
107
108