ssh_encoding/pem/
decode.rs

1use super::{PemLabel, reader::PemReader};
2use crate::{Decode, Reader};
3
4/// Decoding trait for PEM documents.
5///
6/// This is an extension trait which is auto-impl'd for types which impl the
7/// [`Decode`], [`PemLabel`], and [`Sized`] traits.
8pub trait DecodePem: Decode + PemLabel + Sized {
9    /// Decode the provided PEM-encoded string, interpreting the Base64-encoded
10    /// body of the document using the [`Decode`] trait.
11    fn decode_pem(pem: impl AsRef<[u8]>) -> Result<Self, Self::Error>;
12}
13
14impl<T: Decode + PemLabel + Sized> DecodePem for T {
15    fn decode_pem(pem: impl AsRef<[u8]>) -> Result<Self, Self::Error> {
16        let mut reader = PemReader::new(pem.as_ref())?;
17        Self::validate_pem_label(reader.type_label()).map_err(crate::Error::from)?;
18
19        let ret = Self::decode(&mut reader)?;
20        Ok(reader.finish(ret)?)
21    }
22}