From 583d2311c2cc7353f1bd8f24c50fb575f1450da8 Mon Sep 17 00:00:00 2001 From: dishmaker <141624503+dishmaker@users.noreply.github.com> Date: Tue, 21 Oct 2025 15:44:56 +0200 Subject: [PATCH 1/3] der: docs: add `EncodeValue` trait example + doc fn decode_value --- der/src/decode.rs | 3 +++ der/src/encode.rs | 42 +++++++++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/der/src/decode.rs b/der/src/decode.rs index 700c3145a..60d1d7d73 100644 --- a/der/src/decode.rs +++ b/der/src/decode.rs @@ -193,6 +193,9 @@ pub trait DecodeValue<'a>: Sized { type Error: From + 'static; /// Attempt to decode this value using the provided [`Reader`]. + /// + /// Note: in this method, implementers should *not* check header tag (which can be + /// different from the usual object tag when using IMPLICIT tagging, for example). fn decode_value>(reader: &mut R, header: Header) -> Result; } diff --git a/der/src/encode.rs b/der/src/encode.rs index 4a4b1d1df..9640b6d0b 100644 --- a/der/src/encode.rs +++ b/der/src/encode.rs @@ -41,8 +41,8 @@ use crate::{FixedTag, Tag}; /// self.0.encoded_len() /// } /// -/// fn encode(&self, encoder: &mut impl Writer) -> der::Result<()> { -/// self.0.encode(encoder) +/// fn encode(&self, writer: &mut impl Writer) -> der::Result<()> { +/// self.0.encode(writer) /// } /// } /// # } @@ -55,7 +55,7 @@ pub trait Encode { fn encoded_len(&self) -> Result; /// Encode this TLV object as ASN.1 DER using the provided [`Writer`]. - fn encode(&self, encoder: &mut impl Writer) -> Result<()>; + fn encode(&self, writer: &mut impl Writer) -> Result<()>; /// Encode this TLV object to the provided byte slice, returning a sub-slice /// containing the encoded message. @@ -167,6 +167,34 @@ where /// /// When [`EncodeValue`] is paired with [`FixedTag`], /// it produces a complete TLV ASN.1 DER encoding as [`Encode`] trait. +/// +/// ## Example +/// ``` +/// use der::{Encode, EncodeValue, ErrorKind, FixedTag, Length, Tag, Writer}; +/// +/// /// 1-byte month +/// struct MyByteMonth(u8); +/// +/// impl EncodeValue for MyByteMonth { +/// +/// fn value_len(&self) -> der::Result { +/// Ok(Length::new(1)) +/// } +/// +/// fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> { +/// writer.write_byte(self.0)?; +/// Ok(()) +/// } +/// } +/// +/// impl FixedTag for MyByteMonth { +/// const TAG: Tag = Tag::OctetString; +/// } +/// +/// let month_der = MyByteMonth(9).to_der().expect("month to encode"); +/// +/// assert_eq!(month_der, b"\x04\x01\x09"); +/// ``` pub trait EncodeValue { /// Get the [`Header`] used to encode this value. fn header(&self) -> Result
@@ -182,7 +210,7 @@ pub trait EncodeValue { /// Encode value (sans [`Tag`]+[`Length`] header) as ASN.1 DER using the /// provided [`Writer`]. - fn encode_value(&self, encoder: &mut impl Writer) -> Result<()>; + fn encode_value(&self, writer: &mut impl Writer) -> Result<()>; } #[cfg(feature = "alloc")] @@ -203,7 +231,7 @@ pub(crate) fn encode_value_to_slice<'a, T>(buf: &'a mut [u8], value: &T) -> Resu where T: EncodeValue, { - let mut encoder = SliceWriter::new(buf); - value.encode_value(&mut encoder)?; - encoder.finish() + let mut writer = SliceWriter::new(buf); + value.encode_value(&mut writer)?; + writer.finish() } From 9f9ce5eadd2ea7a6c1f8225088d2b05b5582859b Mon Sep 17 00:00:00 2001 From: dishmaker <141624503+dishmaker@users.noreply.github.com> Date: Tue, 21 Oct 2025 15:53:59 +0200 Subject: [PATCH 2/3] der: docs: encode_to_slice instead of to_der --- der/src/encode.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/der/src/encode.rs b/der/src/encode.rs index 9640b6d0b..72827b240 100644 --- a/der/src/encode.rs +++ b/der/src/encode.rs @@ -191,7 +191,9 @@ where /// const TAG: Tag = Tag::OctetString; /// } /// -/// let month_der = MyByteMonth(9).to_der().expect("month to encode"); +/// let month = MyByteMonth(9); +/// let mut buf = [0u8; 16]; +/// let month_der = month.encode_to_slice(&mut buf).expect("month to encode"); /// /// assert_eq!(month_der, b"\x04\x01\x09"); /// ``` From 9ff545fd71c2c0155ee3fcad53dbcfc4e115f1d1 Mon Sep 17 00:00:00 2001 From: dishmaker <141624503+dishmaker@users.noreply.github.com> Date: Mon, 27 Oct 2025 14:31:02 +0100 Subject: [PATCH 3/3] der: docs: rm decode_value comment --- der/src/decode.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/der/src/decode.rs b/der/src/decode.rs index 60d1d7d73..700c3145a 100644 --- a/der/src/decode.rs +++ b/der/src/decode.rs @@ -193,9 +193,6 @@ pub trait DecodeValue<'a>: Sized { type Error: From + 'static; /// Attempt to decode this value using the provided [`Reader`]. - /// - /// Note: in this method, implementers should *not* check header tag (which can be - /// different from the usual object tag when using IMPLICIT tagging, for example). fn decode_value>(reader: &mut R, header: Header) -> Result; }