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
use crate::{Decode, Encode, Reader, Writer};
use core::str::FromStr;
const MAX_LABEL_SIZE: usize = 48;
pub trait Label: AsRef<str> + FromStr<Err = Self::Error> {
type Error: From<crate::Error>;
}
impl<T: Label> Decode for T {
type Error = T::Error;
fn decode(reader: &mut impl Reader) -> Result<Self, T::Error> {
let mut buf = [0u8; MAX_LABEL_SIZE];
reader.read_string(buf.as_mut())?.parse()
}
}
impl<T: Label> Encode for T {
type Error = T::Error;
fn encoded_len(&self) -> Result<usize, T::Error> {
Ok(self.as_ref().encoded_len()?)
}
fn encode(&self, writer: &mut impl Writer) -> Result<(), T::Error> {
Ok(self.as_ref().encode(writer)?)
}
}