Debian Codegraph @

src/writer/pem.rs

raw symbols SCIP
1 //! Streaming PEM writer.
2
3 use super::Writer;
4 use crate::Result;
5 use core::fmt;
6 use pem_rfc7468::{Encoder, LineEnding};
7
8 /// `Writer` type which outputs PEM-encoded data.
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.
15 ///
16 /// # Errors
17 /// If the type label is invalid.
18 pub fn new(
19 type_label: &'static str,
20 line_ending: LineEnding,
21 out: &'w mut [u8],
22 ) -> Result<Self> {
23 Ok(Self(Encoder::new(type_label, line_ending, out)?))
24 }
25
26 /// Get the PEM label which will be used in the encapsulation boundaries
27 /// for this document.
28 #[must_use]
29 pub fn type_label(&self) -> &'static str {
30 self.0.type_label()
31 }
32
33 /// Finish encoding PEM, writing the post-encapsulation boundary.
34 ///
35 /// On success, returns the total number of bytes written to the output buffer.
36 ///
37 /// # Errors
38 /// If internal finalization failed.
39 pub fn finish(self) -> Result<usize> {
40 Ok(self.0.finish()?)
41 }
42 }
43
44 impl Writer for PemWriter<'_> {
45 fn write(&mut self, slice: &[u8]) -> Result<()> {
46 self.0.encode(slice)?;
47 Ok(())
48 }
49 }
50
51 impl fmt::Debug for PemWriter<'_> {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 f.debug_struct("PemWriter").finish_non_exhaustive()
54 }
55 }