From b717fbfb3f8aa111b77ed390700af3f872981a9a Mon Sep 17 00:00:00 2001 From: dishmaker <141624503+dishmaker@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:45:54 +0200 Subject: [PATCH 1/2] der: docs: add `DecodeValue` trait example --- der/src/decode.rs | 30 ++++++++++++++++++++++++++++++ der/src/tag.rs | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/der/src/decode.rs b/der/src/decode.rs index 58b6b4ba3..ab896e799 100644 --- a/der/src/decode.rs +++ b/der/src/decode.rs @@ -158,6 +158,36 @@ impl + PemLabel> DecodePem for T { /// /// As opposed to [`Decode`], implementer is expected to read the inner content only, /// without the [`Header`], which was decoded beforehand. +/// +/// ## Example +/// ``` +/// use der::{Decode, DecodeValue, ErrorKind, FixedTag, Header, Length, Reader, Tag}; +/// +/// /// 4-digit year +/// struct MyStringYear(u16); +/// +/// impl<'a> DecodeValue<'a> for MyStringYear { +/// type Error = der::Error; +/// +/// fn decode_value>(reader: &mut R, header: Header) -> der::Result { +/// let slice = reader.read_slice(Length::new(4))?; +/// let year = std::str::from_utf8(slice).ok().and_then(|s| s.parse::().ok()); +/// if let Some(year) = year { +/// Ok(Self(year)) +/// } else { +/// Err(reader.error(ErrorKind::DateTime)) +/// } +/// } +/// } +/// +/// impl FixedTag for MyStringYear { +/// const TAG: Tag = Tag::Utf8String; +/// } +/// +/// let year = MyStringYear::from_der(b"\x0C\x041670").expect("year to decode"); +/// +/// assert_eq!(year.0, 1670); +/// ``` pub trait DecodeValue<'a>: Sized { /// Type returned in the event of a decoding error. type Error: From + 'static; diff --git a/der/src/tag.rs b/der/src/tag.rs index b0c316b9b..34644892e 100644 --- a/der/src/tag.rs +++ b/der/src/tag.rs @@ -86,7 +86,7 @@ impl Tagged for T { /// if let Some(year) = year { /// Ok(Self(year)) /// } else { -/// Err(ErrorKind::DateTime.into()) +/// Err(reader.error(ErrorKind::DateTime)) /// } /// } /// } From f7bb64d7e3230e9025e7fc0a5f32a6fffdfa79db Mon Sep 17 00:00:00 2001 From: dishmaker <141624503+dishmaker@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:53:30 +0200 Subject: [PATCH 2/2] der: docs: MyByteMonth struct example --- der/src/decode.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/der/src/decode.rs b/der/src/decode.rs index ab896e799..700c3145a 100644 --- a/der/src/decode.rs +++ b/der/src/decode.rs @@ -161,32 +161,32 @@ impl + PemLabel> DecodePem for T { /// /// ## Example /// ``` -/// use der::{Decode, DecodeValue, ErrorKind, FixedTag, Header, Length, Reader, Tag}; +/// use der::{Decode, DecodeValue, ErrorKind, FixedTag, Header, Reader, Tag}; /// -/// /// 4-digit year -/// struct MyStringYear(u16); +/// /// 1-byte month +/// struct MyByteMonth(u8); /// -/// impl<'a> DecodeValue<'a> for MyStringYear { +/// impl<'a> DecodeValue<'a> for MyByteMonth { /// type Error = der::Error; /// /// fn decode_value>(reader: &mut R, header: Header) -> der::Result { -/// let slice = reader.read_slice(Length::new(4))?; -/// let year = std::str::from_utf8(slice).ok().and_then(|s| s.parse::().ok()); -/// if let Some(year) = year { -/// Ok(Self(year)) +/// let month = reader.read_byte()?; +/// +/// if (0..12).contains(&month) { +/// Ok(Self(month)) /// } else { /// Err(reader.error(ErrorKind::DateTime)) /// } /// } /// } /// -/// impl FixedTag for MyStringYear { -/// const TAG: Tag = Tag::Utf8String; +/// impl FixedTag for MyByteMonth { +/// const TAG: Tag = Tag::OctetString; /// } /// -/// let year = MyStringYear::from_der(b"\x0C\x041670").expect("year to decode"); +/// let month = MyByteMonth::from_der(b"\x04\x01\x09").expect("month to decode"); /// -/// assert_eq!(year.0, 1670); +/// assert_eq!(month.0, 9); /// ``` pub trait DecodeValue<'a>: Sized { /// Type returned in the event of a decoding error.