@@ -14,12 +14,41 @@ use core::{cmp::Ordering, fmt};
1414const CONSTRUCTED_FLAG : u8 = 0b100000 ;
1515
1616/// Types which have a constant ASN.1 [`Tag`].
17+ ///
18+ /// ## Example
19+ /// ```
20+ /// use der::{FixedTag, Tag};
21+ ///
22+ /// struct MyOctetString;
23+ ///
24+ /// impl FixedTag for MyOctetString {
25+ /// const TAG: Tag = Tag::OctetString;
26+ /// }
27+ /// ```
1728pub trait FixedTag {
1829 /// ASN.1 tag
1930 const TAG : Tag ;
2031}
2132
2233/// Types which have an ASN.1 [`Tag`].
34+ ///
35+ /// ## Example
36+ /// ```
37+ /// use der::{Tag, Tagged};
38+ ///
39+ /// /// Struct, which Tag depends on data
40+ /// struct MyOctetOrBitString(bool);
41+ ///
42+ /// impl Tagged for MyOctetOrBitString {
43+ /// fn tag(&self) -> Tag {
44+ /// if self.0 {
45+ /// Tag::OctetString
46+ /// } else {
47+ /// Tag::BitString
48+ /// }
49+ /// }
50+ /// }
51+ /// ```
2352#[ diagnostic:: on_unimplemented( note = "Consider adding impl of `FixedTag` to `{Self}`" ) ]
2453pub trait Tagged {
2554 /// Get the ASN.1 tag that this type is encoded with.
@@ -36,6 +65,38 @@ impl<T: FixedTag + ?Sized> Tagged for T {
3665/// Types which have a constant ASN.1 constructed bit.
3766///
3867/// Auto-implemented on all types that implement [`FixedTag`].
68+ ///
69+ /// ## Example
70+ /// ```
71+ /// use der::{asn1::ContextSpecific, DecodeValue, ErrorKind, Header, IsConstructed, Length, Reader, Result, SliceReader, TagNumber};
72+ ///
73+ /// /// Type, which can be decoded for example as `CONTEXT-SPECIFIC [0] (primitive)`
74+ /// struct MyPrimitiveYear(u16);
75+ ///
76+ /// impl IsConstructed for MyPrimitiveYear {
77+ /// const CONSTRUCTED: bool = false;
78+ /// }
79+ ///
80+ /// impl<'a> DecodeValue<'a> for MyPrimitiveYear {
81+ /// type Error = der::Error;
82+ ///
83+ /// fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
84+ /// let slice = reader.read_slice(Length::new(4))?;
85+ /// let year = std::str::from_utf8(slice).ok().and_then(|s| s.parse::<u16>().ok());
86+ /// if let Some(year) = year {
87+ /// Ok(Self(year))
88+ /// } else {
89+ /// Err(ErrorKind::DateTime.into())
90+ /// }
91+ /// }
92+ /// }
93+ ///
94+ /// let mut reader = SliceReader::new(b"\x80\x041670".as_slice()).unwrap();
95+ ///
96+ /// let decoded = ContextSpecific::<MyPrimitiveYear>::decode_implicit(&mut reader, TagNumber(0)).unwrap().unwrap();
97+ ///
98+ /// assert_eq!(decoded.value.0, 1670);
99+ /// ```
39100#[ diagnostic:: on_unimplemented( note = "Consider adding impl of `FixedTag` to `{Self}`" ) ]
40101pub trait IsConstructed {
41102 /// ASN.1 constructed bit
0 commit comments