Skip to content

Commit f7bb64d

Browse files
committed
der: docs: MyByteMonth struct example
1 parent b717fbf commit f7bb64d

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

der/src/decode.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,32 +161,32 @@ impl<T: DecodeOwned<Error = Error> + PemLabel> DecodePem for T {
161161
///
162162
/// ## Example
163163
/// ```
164-
/// use der::{Decode, DecodeValue, ErrorKind, FixedTag, Header, Length, Reader, Tag};
164+
/// use der::{Decode, DecodeValue, ErrorKind, FixedTag, Header, Reader, Tag};
165165
///
166-
/// /// 4-digit year
167-
/// struct MyStringYear(u16);
166+
/// /// 1-byte month
167+
/// struct MyByteMonth(u8);
168168
///
169-
/// impl<'a> DecodeValue<'a> for MyStringYear {
169+
/// impl<'a> DecodeValue<'a> for MyByteMonth {
170170
/// type Error = der::Error;
171171
///
172172
/// fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<Self> {
173-
/// let slice = reader.read_slice(Length::new(4))?;
174-
/// let year = std::str::from_utf8(slice).ok().and_then(|s| s.parse::<u16>().ok());
175-
/// if let Some(year) = year {
176-
/// Ok(Self(year))
173+
/// let month = reader.read_byte()?;
174+
///
175+
/// if (0..12).contains(&month) {
176+
/// Ok(Self(month))
177177
/// } else {
178178
/// Err(reader.error(ErrorKind::DateTime))
179179
/// }
180180
/// }
181181
/// }
182182
///
183-
/// impl FixedTag for MyStringYear {
184-
/// const TAG: Tag = Tag::Utf8String;
183+
/// impl FixedTag for MyByteMonth {
184+
/// const TAG: Tag = Tag::OctetString;
185185
/// }
186186
///
187-
/// let year = MyStringYear::from_der(b"\x0C\x041670").expect("year to decode");
187+
/// let month = MyByteMonth::from_der(b"\x04\x01\x09").expect("month to decode");
188188
///
189-
/// assert_eq!(year.0, 1670);
189+
/// assert_eq!(month.0, 9);
190190
/// ```
191191
pub trait DecodeValue<'a>: Sized {
192192
/// Type returned in the event of a decoding error.

0 commit comments

Comments
 (0)