der/writer/
pem.rs

1//! Streaming PEM writer.
2
3use super::Writer;
4use crate::Result;
5use pem_rfc7468::{Encoder, LineEnding};
6
7/// `Writer` type which outputs PEM-encoded data.
8#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
9pub struct PemWriter<'w>(Encoder<'static, 'w>);
10
11impl<'w> PemWriter<'w> {
12    /// Create a new PEM writer which outputs into the provided buffer.
13    ///
14    /// Uses the default 64-character line wrapping.
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.
25    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.
32    pub fn finish(self) -> Result<usize> {
33        Ok(self.0.finish()?)
34    }
35}
36
37impl Writer for PemWriter<'_> {
38    fn write(&mut self, slice: &[u8]) -> Result<()> {
39        self.0.encode(slice)?;
40        Ok(())
41    }
42}