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.
8pub struct PemWriter<'w>(Encoder<'static, 'w>);
9
10impl<'w> PemWriter<'w> {
11    /// Create a new PEM writer which outputs into the provided buffer.
12    ///
13    /// Uses the default 64-character line wrapping.
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.
24    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.
31    pub fn finish(self) -> Result<usize> {
32        Ok(self.0.finish()?)
33    }
34}
35
36impl Writer for PemWriter<'_> {
37    fn write(&mut self, slice: &[u8]) -> Result<()> {
38        self.0.encode(slice)?;
39        Ok(())
40    }
41}