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 pub struct PemWriter<'w>(Encoder<'static, 'w>);
9 
10 impl<'w> PemWriter<'w> {
11     /// Create a new PEM writer which outputs into the provided buffer.
12     ///
13     /// Uses the default 64-character line wrapping.
new( type_label: &'static str, line_ending: LineEnding, out: &'w mut [u8], ) -> Result<Self>14     pub fn new(
15         type_label: &'static str,
16         line_ending: LineEnding,
17         out: &'w mut [u8],
18     ) -> Result<Self> {
19         Ok(Self(Encoder::new(type_label, line_ending, out)?))
20     }
21 
22     /// Get the PEM label which will be used in the encapsulation boundaries
23     /// for this document.
type_label(&self) -> &'static str24     pub fn type_label(&self) -> &'static str {
25         self.0.type_label()
26     }
27 
28     /// Finish encoding PEM, writing the post-encapsulation boundary.
29     ///
30     /// On success, returns the total number of bytes written to the output buffer.
finish(self) -> Result<usize>31     pub fn finish(self) -> Result<usize> {
32         Ok(self.0.finish()?)
33     }
34 }
35 
36 impl Writer for PemWriter<'_> {
write(&mut self, slice: &[u8]) -> Result<()>37     fn write(&mut self, slice: &[u8]) -> Result<()> {
38         self.0.encode(slice)?;
39         Ok(())
40     }
41 }
42