Skip to content

Commit c21e18c

Browse files
authored
der: improve docs for Encode and Decode (#2053)
1 parent 3d93e85 commit c21e18c

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

der/src/decode.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ use alloc::boxed::Box;
1616
#[cfg(feature = "ber")]
1717
use crate::EncodingRules;
1818

19-
/// Decoding trait.
19+
/// Decode trait parses a complete TLV (Tag-Length-Value) structure.
2020
///
2121
/// This trait provides the core abstraction upon which all decoding operations
2222
/// are based.
23+
///
24+
/// When decoding fails, a [`Decode::Error`] type is thrown.
25+
/// Most ASN.1 DER objects return a builtin der [`Error`] type as [`Decode::Error`], which can be made from [`ErrorKind`].
2326
#[diagnostic::on_unimplemented(
2427
note = "Consider adding impls of `DecodeValue` and `FixedTag` to `{Self}`"
2528
)]
2629
pub trait Decode<'a>: Sized + 'a {
2730
/// Type returned in the event of a decoding error.
2831
type Error: From<Error> + 'static;
2932

30-
/// Attempt to decode this message using the provided decoder.
33+
/// Attempt to decode this TLV message using the provided decoder.
3134
fn decode<R: Reader<'a>>(decoder: &mut R) -> Result<Self, Self::Error>;
3235

3336
/// Parse `Self` from the provided BER-encoded byte slice.
@@ -127,13 +130,16 @@ impl<T: DecodeOwned<Error = Error> + PemLabel> DecodePem for T {
127130
}
128131
}
129132

130-
/// Decode the value part of a Tag-Length-Value encoded field, sans the [`Tag`]
131-
/// and [`Length`].
133+
/// DecodeValue trait parses the value part of a Tag-Length-Value object,
134+
/// sans the [`Tag`] and [`Length`].
135+
///
136+
/// As opposed to [`Decode`], implementer is expected to read the inner content only,
137+
/// without the [`Header`], which was decoded beforehand.
132138
pub trait DecodeValue<'a>: Sized {
133139
/// Type returned in the event of a decoding error.
134140
type Error: From<Error> + 'static;
135141

136-
/// Attempt to decode this message using the provided [`Reader`].
142+
/// Attempt to decode this value using the provided [`Reader`].
137143
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Self::Error>;
138144
}
139145

der/src/encode.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,30 @@ use {
1717
use crate::ErrorKind;
1818

1919
#[cfg(doc)]
20-
use crate::Tag;
20+
use crate::{FixedTag, Tag};
2121

22-
/// Encoding trait.
22+
/// Encode trait produces a complete TLV (Tag-Length-Value) structure.
23+
///
24+
/// As opposed to [`EncodeValue`], implementer is expected to write ASN.1 DER tag and length header before value.
2325
#[diagnostic::on_unimplemented(
2426
note = "Consider adding impls of `EncodeValue` and `FixedTag` to `{Self}`"
2527
)]
2628
pub trait Encode {
27-
/// Compute the length of this value in bytes when encoded as ASN.1 DER.
29+
/// Compute the length of this TLV object in bytes when encoded as ASN.1 DER.
2830
fn encoded_len(&self) -> Result<Length>;
2931

30-
/// Encode this value as ASN.1 DER using the provided [`Writer`].
32+
/// Encode this TLV object as ASN.1 DER using the provided [`Writer`].
3133
fn encode(&self, encoder: &mut impl Writer) -> Result<()>;
3234

33-
/// Encode this value to the provided byte slice, returning a sub-slice
35+
/// Encode this TLV object to the provided byte slice, returning a sub-slice
3436
/// containing the encoded message.
3537
fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8]> {
3638
let mut writer = SliceWriter::new(buf);
3739
self.encode(&mut writer)?;
3840
writer.finish()
3941
}
4042

41-
/// Encode this message as ASN.1 DER, appending it to the provided
43+
/// Encode this TLV object as ASN.1 DER, appending it to the provided
4244
/// byte vector.
4345
#[cfg(feature = "alloc")]
4446
fn encode_to_vec(&self, buf: &mut Vec<u8>) -> Result<Length> {
@@ -60,7 +62,7 @@ pub trait Encode {
6062
actual_len.try_into()
6163
}
6264

63-
/// Encode this type as DER, returning a byte vector.
65+
/// Encode this TLV object as ASN.1 DER, returning a byte vector.
6466
#[cfg(feature = "alloc")]
6567
fn to_der(&self) -> Result<Vec<u8>> {
6668
let mut buf = Vec::new();
@@ -73,12 +75,12 @@ impl<T> Encode for T
7375
where
7476
T: EncodeValue + Tagged + ?Sized,
7577
{
76-
/// Compute the length of this value in bytes when encoded as ASN.1 DER.
78+
/// Compute the length of this TLV object in bytes when encoded as ASN.1 DER.
7779
fn encoded_len(&self) -> Result<Length> {
7880
self.value_len().and_then(|len| len.for_tlv(self.tag()))
7981
}
8082

81-
/// Encode this value as ASN.1 DER using the provided [`Writer`].
83+
/// Encode this TLV object as ASN.1 DER using the provided [`Writer`].
8284
fn encode(&self, writer: &mut impl Writer) -> Result<()> {
8385
self.header()?.encode(writer)?;
8486
self.encode_value(writer)
@@ -134,6 +136,12 @@ where
134136

135137
/// Encode the value part of a Tag-Length-Value encoded field, sans the [`Tag`]
136138
/// and [`Length`].
139+
///
140+
/// As opposed to [`Encode`], implementer is expected to write the inner content only,
141+
/// without the [`Header`].
142+
///
143+
/// When [`EncodeValue`] is paired with [`FixedTag`],
144+
/// it produces a complete TLV ASN.1 DER encoding as [`Encode`] trait.
137145
pub trait EncodeValue {
138146
/// Get the [`Header`] used to encode this value.
139147
fn header(&self) -> Result<Header>

0 commit comments

Comments
 (0)