ssh-encoding 0.4.0

Pure Rust implementation of SSH data type decoders/encoders as described in RFC4251
Documentation
use crate::{Reader, Result};
use core::fmt;

/// Inner PEM decoder.
type Inner<'i> = pem_rfc7468::Decoder<'i>;

/// Reader for the Base64 body of a PEM document.
#[derive(Clone)]
pub struct PemReader<'i> {
    /// Inner PEM reader.
    inner: Inner<'i>,
}

impl<'i> PemReader<'i> {
    /// Create a new PEM reader which autodetects the line width of the input.
    ///
    /// # Errors
    /// Returns [`crate::Error::Pem`] on errors parsing the encapsulation or initializing
    /// the Base64 decoder.
    pub fn new(pem: &'i [u8]) -> Result<Self> {
        let inner = Inner::new_detect_wrap(pem)?;

        Ok(Self { inner })
    }

    /// Get the PEM type label for the input document.
    #[must_use]
    pub fn type_label(&self) -> &'i str {
        self.inner.type_label()
    }
}

impl fmt::Debug for PemReader<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PemReader")
            .field("remaining_len", &self.remaining_len())
            .finish_non_exhaustive()
    }
}

impl Reader for PemReader<'_> {
    fn read<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]> {
        Ok(self.inner.decode(out)?)
    }

    fn remaining_len(&self) -> usize {
        self.inner.remaining_len()
    }
}