| 1 |
|
| 2 |
|
| 3 |
use crate::{Error, FixedTag, Header, Reader, SliceReader, reader::read_value}; |
| 4 |
|
| 5 |
use core::marker::PhantomData; |
| 6 |
|
| 7 |
#[cfg(feature = "pem")] |
| 8 |
use crate::{PemReader, pem::PemLabel}; |
| 9 |
|
| 10 |
#[cfg(doc)] |
| 11 |
use crate::{ErrorKind, Length, Tag}; |
| 12 |
|
| 13 |
#[cfg(feature = "alloc")] |
| 14 |
use alloc::{ |
| 15 |
borrow::{Cow, ToOwned}, |
| 16 |
boxed::Box, |
| 17 |
}; |
| 18 |
|
| 19 |
#[cfg(feature = "ber")] |
| 20 |
use crate::EncodingRules; |
| 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 |
#[diagnostic::on_unimplemented( |
| 53 |
note = "Consider adding impls of `DecodeValue` and `FixedTag` to `{Self}`" |
| 54 |
)] |
| 55 |
pub trait Decode<'a>: Sized + 'a { |
| 56 |
|
| 57 |
type Error: core::error::Error + From<Error> + 'static; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
fn decode<R: Reader<'a>>(decoder: &mut R) -> Result<Self, Self::Error>; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#[cfg(feature = "ber")] |
| 73 |
fn from_ber(bytes: &'a [u8]) -> Result<Self, Self::Error> { |
| 74 |
let mut reader = SliceReader::new_with_encoding_rules(bytes, EncodingRules::Ber)?; |
| 75 |
let result = Self::decode(&mut reader)?; |
| 76 |
reader.finish()?; |
| 77 |
Ok(result) |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
fn from_der(bytes: &'a [u8]) -> Result<Self, Self::Error> { |
| 85 |
let mut reader = SliceReader::new(bytes)?; |
| 86 |
let result = Self::decode(&mut reader)?; |
| 87 |
reader.finish()?; |
| 88 |
Ok(result) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
fn from_der_partial(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), Self::Error> { |
| 98 |
let mut reader = SliceReader::new(bytes)?; |
| 99 |
let result = Self::decode(&mut reader)?; |
| 100 |
|
| 101 |
let remaining = reader.remaining()?; |
| 102 |
Ok((result, remaining)) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl<'a, T> Decode<'a> for T |
| 107 |
where |
| 108 |
T: DecodeValue<'a> + FixedTag + 'a, |
| 109 |
{ |
| 110 |
type Error = <T as DecodeValue<'a>>::Error; |
| 111 |
|
| 112 |
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<T, <T as DecodeValue<'a>>::Error> { |
| 113 |
let header = Header::decode(reader)?; |
| 114 |
header.tag().assert_eq(T::TAG)?; |
| 115 |
read_value(reader, header, T::decode_value) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
impl<'a, T> Decode<'a> for PhantomData<T> |
| 122 |
where |
| 123 |
T: ?Sized + 'a, |
| 124 |
{ |
| 125 |
type Error = Error; |
| 126 |
|
| 127 |
fn decode<R: Reader<'a>>(_reader: &mut R) -> Result<PhantomData<T>, Error> { |
| 128 |
Ok(PhantomData) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#[diagnostic::on_unimplemented( |
| 141 |
note = "`DecodeOwned` is auto-impl'd for all lifetime-free types which impl `Decode`" |
| 142 |
)] |
| 143 |
pub trait DecodeOwned: for<'a> Decode<'a> {} |
| 144 |
|
| 145 |
impl<T> DecodeOwned for T where T: for<'a> Decode<'a> {} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
#[cfg(feature = "pem")] |
| 152 |
#[diagnostic::on_unimplemented( |
| 153 |
note = "`DecodePem` is auto-impl'd for all lifetime-free types which impl both `Decode` and `PemLabel`" |
| 154 |
)] |
| 155 |
pub trait DecodePem: DecodeOwned + PemLabel { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
fn from_pem(pem: impl AsRef<[u8]>) -> Result<Self, <Self as Decode<'static>>::Error>; |
| 161 |
} |
| 162 |
|
| 163 |
#[cfg(feature = "pem")] |
| 164 |
impl<T: DecodeOwned<Error = Error> + PemLabel> DecodePem for T { |
| 165 |
fn from_pem(pem: impl AsRef<[u8]>) -> Result<T, Error> { |
| 166 |
let mut reader = PemReader::new(pem.as_ref())?; |
| 167 |
Self::validate_pem_label(reader.type_label()).map_err(Error::from)?; |
| 168 |
T::decode(&mut reader) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
pub trait DecodeValue<'a>: Sized { |
| 208 |
|
| 209 |
type Error: core::error::Error + From<Error> + 'static; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Self::Error>; |
| 216 |
} |
| 217 |
|
| 218 |
#[cfg(feature = "alloc")] |
| 219 |
impl<'a, T> DecodeValue<'a> for Box<T> |
| 220 |
where |
| 221 |
T: DecodeValue<'a>, |
| 222 |
{ |
| 223 |
type Error = T::Error; |
| 224 |
|
| 225 |
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Self::Error> { |
| 226 |
Ok(Box::new(T::decode_value(reader, header)?)) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
#[cfg(feature = "alloc")] |
| 231 |
impl<'a, T, E> DecodeValue<'a> for Cow<'a, T> |
| 232 |
where |
| 233 |
T: ToOwned + ?Sized, |
| 234 |
&'a T: DecodeValue<'a, Error = E>, |
| 235 |
T::Owned: for<'b> DecodeValue<'b, Error = E>, |
| 236 |
E: core::error::Error + From<Error> + 'static, |
| 237 |
{ |
| 238 |
type Error = E; |
| 239 |
|
| 240 |
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Self::Error> { |
| 241 |
if R::CAN_READ_SLICE { |
| 242 |
<&'a T>::decode_value(reader, header).map(Cow::Borrowed) |
| 243 |
} else { |
| 244 |
T::Owned::decode_value(reader, header).map(Cow::Owned) |
| 245 |
} |
| 246 |
} |
| 247 |
} |