-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathencode.rs
More file actions
57 lines (50 loc) · 2.08 KB
/
encode.rs
File metadata and controls
57 lines (50 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use super::{LineEnding, PemLabel, writer::PemWriter};
use crate::{Encode, Error};
use core::str;
#[cfg(feature = "alloc")]
use {super::LINE_WIDTH, alloc::string::String};
/// Encoding trait for PEM documents.
///
/// This is an extension trait which is auto-impl'd for types which impl the [`Encode`] and
/// [`PemLabel`] traits.
#[diagnostic::on_unimplemented(
note = "Consider adding impls of `Encode` and `PemLabel` to `{Self}`"
)]
pub trait EncodePem: Encode + PemLabel {
/// Encode this type using the [`Encode`] trait, writing the resulting PEM document into the
/// provided `out` buffer.
///
/// # Errors
/// - Returns [`Error::Pem`] in the event of PEM encoding errors.
/// - Propagates errors returned from the [`Encode::encode`] method.
fn encode_pem<'o>(&self, line_ending: LineEnding, out: &'o mut [u8]) -> Result<&'o str, Error>;
/// Encode this type using the [`Encode`] trait, writing the resulting PEM document to a
/// returned [`String`].
///
/// # Errors
/// Propagates errors returned from [`EncodePem::encode_pem`].
#[cfg(feature = "alloc")]
fn encode_pem_string(&self, line_ending: LineEnding) -> Result<String, Error>;
}
impl<T: Encode + PemLabel> EncodePem for T {
fn encode_pem<'o>(&self, line_ending: LineEnding, out: &'o mut [u8]) -> Result<&'o str, Error> {
let mut writer = PemWriter::new(Self::PEM_LABEL, line_ending, out)?;
self.encode(&mut writer)?;
let encoded_len = writer.finish()?;
str::from_utf8(&out[..encoded_len]).map_err(Error::from)
}
#[cfg(feature = "alloc")]
fn encode_pem_string(&self, line_ending: LineEnding) -> Result<String, Error> {
let encoded_len = pem_rfc7468::encapsulated_len_wrapped(
Self::PEM_LABEL,
LINE_WIDTH,
line_ending,
self.encoded_len()?,
)
.map_err(Error::from)?;
let mut buf = vec![0u8; encoded_len];
let actual_len = self.encode_pem(line_ending, &mut buf)?.len();
buf.truncate(actual_len);
String::from_utf8(buf).map_err(Error::from)
}
}