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::{DecodeValue, Decoder, FixedTag, Header, Result};
pub trait Decodable<'a>: Sized {
fn decode(decoder: &mut Decoder<'a>) -> Result<Self>;
fn from_der(bytes: &'a [u8]) -> Result<Self> {
let mut decoder = Decoder::new(bytes)?;
let result = Self::decode(&mut decoder)?;
decoder.finish(result)
}
}
impl<'a, T> Decodable<'a> for T
where
T: DecodeValue<'a> + FixedTag,
{
fn decode(decoder: &mut Decoder<'a>) -> Result<T> {
let header = Header::decode(decoder)?;
header.tag.assert_eq(T::TAG)?;
T::decode_value(decoder, header.length)
}
}