hickory_proto/rr/rdata/
smimea.rs1use alloc::vec::Vec;
4use core::{fmt, ops::Deref};
5
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9use super::tlsa::{CertUsage, Matching, Selector, TLSA};
10use crate::{
11 error::ProtoResult,
12 rr::{RData, RecordData, RecordDataDecodable, RecordType},
13 serialize::{
14 binary::{BinDecoder, BinEncodable, BinEncoder, DecodeError, Restrict},
15 txt::ParseError,
16 },
17};
18
19#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
24#[derive(Debug, PartialEq, Eq, Hash, Clone)]
25pub struct SMIMEA(pub TLSA);
26
27impl SMIMEA {
28 pub fn new(
48 cert_usage: CertUsage,
49 selector: Selector,
50 matching: Matching,
51 cert_data: Vec<u8>,
52 ) -> Self {
53 Self(TLSA::new(cert_usage, selector, matching, cert_data))
54 }
55
56 pub(crate) fn from_tokens<'i, I: Iterator<Item = &'i str>>(
58 tokens: I,
59 ) -> Result<Self, ParseError> {
60 TLSA::from_tokens(tokens).map(Self)
61 }
62}
63
64impl Deref for SMIMEA {
67 type Target = TLSA;
68
69 fn deref(&self) -> &Self::Target {
70 &self.0
71 }
72}
73
74impl BinEncodable for SMIMEA {
75 #[inline]
76 fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
77 BinEncodable::emit(&self.0, encoder)
78 }
79}
80
81impl RecordDataDecodable<'_> for SMIMEA {
82 #[inline]
83 fn read_data(decoder: &mut BinDecoder<'_>, length: Restrict<u16>) -> Result<Self, DecodeError> {
84 TLSA::read_data(decoder, length).map(Self)
85 }
86}
87
88impl RecordData for SMIMEA {
89 fn try_borrow(data: &RData) -> Option<&Self> {
90 match data {
91 RData::SMIMEA(data) => Some(data),
92 _ => None,
93 }
94 }
95
96 fn record_type(&self) -> RecordType {
97 RecordType::SMIMEA
98 }
99
100 fn into_rdata(self) -> RData {
101 RData::SMIMEA(self)
102 }
103}
104
105impl fmt::Display for SMIMEA {
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
107 fmt::Display::fmt(&self.0, f)
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn test_parsing() {
117 assert!(
118 SMIMEA::from_tokens(
119 vec![
120 "0",
121 "0",
122 "1",
123 "d2abde240d7cd3ee6b4b28c54df034b9",
124 "7983a1d16e8a410e4561cb106618e971",
125 ]
126 .into_iter()
127 )
128 .is_ok()
129 );
130 assert!(
131 SMIMEA::from_tokens(
132 vec![
133 "1",
134 "1",
135 "2",
136 "92003ba34942dc74152e2f2c408d29ec",
137 "a5a520e7f2e06bb944f4dca346baf63c",
138 "1b177615d466f6c4b71c216a50292bd5",
139 "8c9ebdd2f74e38fe51ffd48c43326cbc",
140 ]
141 .into_iter()
142 )
143 .is_ok()
144 );
145 }
146}