use alloc::vec::Vec;
use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::{
error::ProtoResult,
rr::{RData, RecordData, RecordDataDecodable, RecordType},
serialize::binary::{BinDecoder, BinEncodable, BinEncoder, Restrict},
};
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct OPENPGPKEY {
public_key: Vec<u8>,
}
impl OPENPGPKEY {
pub fn new(public_key: Vec<u8>) -> Self {
Self { public_key }
}
pub fn public_key(&self) -> &[u8] {
&self.public_key
}
}
impl BinEncodable for OPENPGPKEY {
fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
encoder.emit_vec(self.public_key())
}
}
impl<'r> RecordDataDecodable<'r> for OPENPGPKEY {
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
let rdata_length = length.map(usize::from).unverified();
let public_key =
decoder.read_vec(rdata_length)?.unverified();
Ok(Self::new(public_key))
}
}
impl RecordData for OPENPGPKEY {
fn try_from_rdata(data: RData) -> Result<Self, RData> {
match data {
RData::OPENPGPKEY(csync) => Ok(csync),
_ => Err(data),
}
}
fn try_borrow(data: &RData) -> Option<&Self> {
match data {
RData::OPENPGPKEY(csync) => Some(csync),
_ => None,
}
}
fn record_type(&self) -> RecordType {
RecordType::OPENPGPKEY
}
fn into_rdata(self) -> RData {
RData::OPENPGPKEY(self)
}
}
impl fmt::Display for OPENPGPKEY {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str(&data_encoding::BASE64.encode(&self.public_key))
}
}