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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::{FixedTag, Header, Reader, Result, SliceReader};
#[cfg(feature = "pem")]
use crate::{pem::PemLabel, PemReader};
#[cfg(doc)]
use crate::{Length, Tag};
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
pub trait Decode<'a>: Sized {
fn decode<R: Reader<'a>>(decoder: &mut R) -> Result<Self>;
fn from_der(bytes: &'a [u8]) -> Result<Self> {
let mut reader = SliceReader::new(bytes)?;
let result = Self::decode(&mut reader)?;
reader.finish(result)
}
}
impl<'a, T> Decode<'a> for T
where
T: DecodeValue<'a> + FixedTag,
{
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<T> {
let header = Header::decode(reader)?;
header.tag.assert_eq(T::TAG)?;
T::decode_value(reader, header)
}
}
pub trait DecodeOwned: for<'a> Decode<'a> {}
impl<T> DecodeOwned for T where T: for<'a> Decode<'a> {}
#[cfg(feature = "pem")]
pub trait DecodePem: DecodeOwned + PemLabel {
fn from_pem(pem: impl AsRef<[u8]>) -> Result<Self>;
}
#[cfg(feature = "pem")]
impl<T: DecodeOwned + PemLabel> DecodePem for T {
fn from_pem(pem: impl AsRef<[u8]>) -> Result<Self> {
let mut reader = PemReader::new(pem.as_ref())?;
Self::validate_pem_label(reader.type_label())?;
T::decode(&mut reader)
}
}
pub trait DecodeValue<'a>: Sized {
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self>;
}
#[cfg(feature = "alloc")]
impl<'a, T> DecodeValue<'a> for Box<T>
where
T: DecodeValue<'a>,
{
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
Ok(Box::new(T::decode_value(reader, header)?))
}
}