Skip to content

Commit 475db3b

Browse files
authored
Add documentation to public trait methods (#544)
1 parent 82632f1 commit 475db3b

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/types.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,25 @@ use crate::{
1717

1818
/// Any type that can be parsed as DER ASN.1.
1919
pub trait Asn1Readable<'a>: Sized {
20+
/// Parse a value from the given parser.
21+
///
22+
/// This method should read exactly one ASN.1 TLV from the parser,
23+
/// consuming the appropriate bytes and returning the parsed value.
2024
fn parse(parser: &mut Parser<'a>) -> ParseResult<Self>;
25+
26+
/// Returns whether this type can parse values with the given tag.
2127
fn can_parse(tag: Tag) -> bool;
2228
}
2329

2430
/// Types with a fixed-tag that can be parsed as DER ASN.1
2531
pub trait SimpleAsn1Readable<'a>: Sized {
32+
/// The ASN.1 tag that this type expects when parsing.
2633
const TAG: Tag;
2734

35+
/// Parse the value from the given data bytes.
36+
///
37+
/// This method receives the value portion of a TLV (without the tag or
38+
/// length) and should parse it into the appropriate type.
2839
fn parse_data(data: &'a [u8]) -> ParseResult<Self>;
2940
}
3041

@@ -56,22 +67,44 @@ impl<'a, T: SimpleAsn1Readable<'a>> SimpleAsn1Readable<'a> for Box<T> {
5667

5768
/// Any type that can be written as DER ASN.1.
5869
pub trait Asn1Writable: Sized {
70+
/// Write this value to the given writer.
71+
///
72+
/// This method should write the complete ASN.1 encoding of this value,
73+
/// including the tag, length, and content bytes.
5974
fn write(&self, dest: &mut Writer<'_>) -> WriteResult;
6075
}
6176

62-
// Types with a fixed-tag that can be written as DER ASN.1.
77+
/// Types with a fixed-tag that can be written as DER ASN.1.
6378
pub trait SimpleAsn1Writable: Sized {
79+
/// The ASN.1 tag that this type uses when writing.
6480
const TAG: Tag;
6581

82+
/// Write the value's data to the given buffer.
83+
///
84+
/// This method should write only the value bytes (without the tag and
85+
/// length) to the buffer.
6686
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult;
6787
}
6888

89+
/// A trait for types that can be parsed based on a `DEFINED BY` value.
90+
///
91+
/// `T` is the type of the `DEFINED BY` field (nearly always `ObjectIdentifier`).
6992
pub trait Asn1DefinedByReadable<'a, T: Asn1Readable<'a>>: Sized {
93+
/// Parse a value based on the previously parsed item.
94+
///
95+
/// The `item` parameter contains the value that determines how to parse
96+
/// the current value from the parser.
7097
fn parse(item: T, parser: &mut Parser<'a>) -> ParseResult<Self>;
7198
}
7299

100+
/// A trait for types that can be written based on a `DEFINED BY` value.
101+
///
102+
/// `T` is the type of the `DEFINED BY` field (nearly always `ObjectIdentifier`).
73103
pub trait Asn1DefinedByWritable<T: Asn1Writable>: Sized {
104+
/// Get a reference to the `DEFINED BY` value.
74105
fn item(&self) -> &T;
106+
107+
/// Write this value to the given writer.
75108
fn write(&self, dest: &mut Writer<'_>) -> WriteResult;
76109
}
77110

0 commit comments

Comments
 (0)