1 //! Streaming PEM writer. 2 3 use super::Writer; 4 use crate::Result; 5 use pem_rfc7468::{Encoder, LineEnding}; 6 7 /// `Writer` type which outputs PEM-encoded data. 8 #[cfg_attr(docsrs, doc(cfg(feature = "pem")))] 9 pub struct PemWriter<'w>(Encoder<'static, 'w>); 10 11 impl<'w> PemWriter<'w> { 12 /// Create a new PEM writer which outputs into the provided buffer. 13 /// 14 /// Uses the default 64-character line wrapping. new( type_label: &'static str, line_ending: LineEnding, out: &'w mut [u8], ) -> Result<Self>15 pub fn new( 16 type_label: &'static str, 17 line_ending: LineEnding, 18 out: &'w mut [u8], 19 ) -> Result<Self> { 20 Ok(Self(Encoder::new(type_label, line_ending, out)?)) 21 } 22 23 /// Get the PEM label which will be used in the encapsulation boundaries 24 /// for this document. type_label(&self) -> &'static str25 pub fn type_label(&self) -> &'static str { 26 self.0.type_label() 27 } 28 29 /// Finish encoding PEM, writing the post-encapsulation boundary. 30 /// 31 /// On success, returns the total number of bytes written to the output buffer. finish(self) -> Result<usize>32 pub fn finish(self) -> Result<usize> { 33 Ok(self.0.finish()?) 34 } 35 } 36 37 impl Writer for PemWriter<'_> { write(&mut self, slice: &[u8]) -> Result<()>38 fn write(&mut self, slice: &[u8]) -> Result<()> { 39 self.0.encode(slice)?; 40 Ok(()) 41 } 42 } 43