use crate::{Header, Length, Result, SliceWriter, Tagged, Writer};
#[cfg(feature = "alloc")]
use {alloc::vec::Vec, core::iter};
#[cfg(feature = "pem")]
use {
crate::PemWriter,
alloc::string::String,
pem_rfc7468::{self as pem, LineEnding, PemLabel},
};
#[cfg(any(feature = "alloc", feature = "pem"))]
use crate::ErrorKind;
#[cfg(doc)]
use crate::Tag;
pub trait Encode {
fn encoded_len(&self) -> Result<Length>;
fn encode(&self, encoder: &mut dyn Writer) -> Result<()>;
fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8]> {
let mut writer = SliceWriter::new(buf);
self.encode(&mut writer)?;
writer.finish()
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
fn encode_to_vec(&self, buf: &mut Vec<u8>) -> Result<Length> {
let expected_len = usize::try_from(self.encoded_len()?)?;
buf.reserve(expected_len);
buf.extend(iter::repeat(0).take(expected_len));
let mut writer = SliceWriter::new(buf);
self.encode(&mut writer)?;
let actual_len = writer.finish()?.len();
if expected_len != actual_len {
return Err(ErrorKind::Incomplete {
expected_len: expected_len.try_into()?,
actual_len: actual_len.try_into()?,
}
.into());
}
actual_len.try_into()
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
fn to_vec(&self) -> Result<Vec<u8>> {
let mut buf = Vec::new();
self.encode_to_vec(&mut buf)?;
Ok(buf)
}
}
impl<T> Encode for T
where
T: EncodeValue + Tagged,
{
fn encoded_len(&self) -> Result<Length> {
self.value_len().and_then(|len| len.for_tlv())
}
fn encode(&self, writer: &mut dyn Writer) -> Result<()> {
self.header()?.encode(writer)?;
self.encode_value(writer)
}
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub trait EncodePem: Encode + PemLabel {
fn to_pem(&self, line_ending: LineEnding) -> Result<String>;
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl<T: Encode + PemLabel> EncodePem for T {
fn to_pem(&self, line_ending: LineEnding) -> Result<String> {
let der_len = usize::try_from(self.encoded_len()?)?;
let pem_len = pem::encapsulated_len(Self::PEM_LABEL, line_ending, der_len)?;
let mut buf = vec![0u8; pem_len];
let mut writer = PemWriter::new(Self::PEM_LABEL, line_ending, &mut buf)?;
self.encode(&mut writer)?;
let actual_len = writer.finish()?;
buf.truncate(actual_len);
Ok(String::from_utf8(buf)?)
}
}
pub trait EncodeValue {
fn header(&self) -> Result<Header>
where
Self: Tagged,
{
Header::new(self.tag(), self.value_len()?)
}
fn value_len(&self) -> Result<Length>;
fn encode_value(&self, encoder: &mut dyn Writer) -> Result<()>;
}