Skip to content

Commit f8991bc

Browse files
committed
ssh-encoding: improve rustdoc
For the listing of RFC4251 types, uses better headings for each type, and adds a list of types which have compatible `Decode`/`Encode` impls
1 parent f0b55c8 commit f8991bc

2 files changed

Lines changed: 59 additions & 36 deletions

File tree

ssh-encoding/src/decode.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use alloc::{string::String, vec::Vec};
1212
use bytes::Bytes;
1313

1414
/// Maximum size of a `usize` this library will accept.
15-
const MAX_SIZE: usize = 0xFFFFF;
15+
const MAX_USIZE: usize = 0xFFFFF;
1616

1717
/// Decoding trait.
1818
///
@@ -102,7 +102,7 @@ impl Decode for usize {
102102
fn decode(reader: &mut impl Reader) -> Result<Self> {
103103
let n = usize::try_from(u32::decode(reader)?)?;
104104

105-
if n <= MAX_SIZE {
105+
if n <= MAX_USIZE {
106106
Ok(n)
107107
} else {
108108
Err(Error::Overflow)
@@ -162,6 +162,24 @@ impl Decode for String {
162162
}
163163
}
164164

165+
/// Decodes `Vec<String>` from `name-list` as described in [RFC4251 § 5]:
166+
///
167+
/// > A string containing a comma-separated list of names. A name-list
168+
/// > is represented as a uint32 containing its length (number of bytes
169+
/// > that follow) followed by a comma-separated list of zero or more
170+
/// > names. A name MUST have a non-zero length, and it MUST NOT
171+
/// > contain a comma (","). As this is a list of names, all of the
172+
/// > elements contained are names and MUST be in US-ASCII. Context may
173+
/// > impose additional restrictions on the names. For example, the
174+
/// > names in a name-list may have to be a list of valid algorithm
175+
/// > identifiers (see Section 6 below), or a list of [RFC3066] language
176+
/// > tags. The order of the names in a name-list may or may not be
177+
/// > significant. Again, this depends on the context in which the list
178+
/// > is used. Terminating null characters MUST NOT be used, neither
179+
/// > for the individual names, nor for the list as a whole.
180+
///
181+
/// [RFC3066]: https://datatracker.ietf.org/doc/html/rfc3066
182+
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
165183
#[cfg(feature = "alloc")]
166184
impl Decode for Vec<String> {
167185
type Error = Error;

ssh-encoding/src/lib.rs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323

2424
//! ## Conventions used in this crate
2525
//!
26-
//! This crate uses the following type labels which are described in
27-
//! [RFC4251 § 5]:
26+
//! This crate uses the following type labels which are described in [RFC4251 § 5], and also lists
27+
//! types with [`Decode`]/[`Encode`] trait impls which are compatible with this format:
2828
//!
29-
//! #### `byte`, `byte[n]`, `byte[]`
30-
//!
31-
//! A byte represents an arbitrary 8-bit value (octet).
29+
//! ### `byte`, `byte[n]`, `byte[]`: arbitrary 8-bit value (octet) or sequence thereof
30+
//! #### [`Decode`]/[`Encode`] trait impls:
31+
//! - `byte`: `u8`
32+
//! - `byte[n]`: `[u8; N]`
33+
//! - `byte[]`: `Vec<u8>`, `bytes::Bytes` (requires `bytes` crate feature)
3234
//!
3335
//! Fixed length data is sometimes represented as an array of bytes, written
3436
//! `byte[n]` where `n` is the number of bytes in the array.
@@ -37,52 +39,52 @@
3739
//! length bytestrings (similar to `string`, see below) but identifies data
3840
//! which is inherently binary in nature, as opposed to text.
3941
//!
40-
//! #### `boolean`
41-
//!
42-
//! A boolean value is stored as a single byte.
42+
//!
43+
//! ### `boolean`: boolean value stored as a single byte
44+
//! #### [`Decode`]/[`Encode`] trait impls: `bool`
4345
//!
4446
//! The value 0 represents FALSE, and the value 1 represents TRUE. All non-zero
4547
//! values MUST be interpreted as TRUE; however, applications MUST NOT
4648
//! store values other than 0 and 1.
47-
//!
48-
//! #### `uint32`
49-
//!
50-
//! Represents a 32-bit unsigned integer.
49+
//!
50+
//! ### `uint32`: 32-bit unsigned integer
51+
//! #### [`Decode`]/[`Encode`] trait impls: `u32`, `usize`
5152
//!
5253
//! Stored as four bytes in the order of decreasing significance (network byte order).
5354
//!
5455
//! For example: the value `699921578` (`0x29b7f4aa`) is stored as
5556
//! `29 b7 f4 aa`.
5657
//!
57-
//! #### `uint64`
58-
//!
59-
//! Represents a 64-bit unsigned integer.
58+
//! ### `uint64`: 64-bit unsigned integer
59+
//! #### [`Decode`]/[`Encode`] trait impls: `u64`
6060
//!
6161
//! Stored as eight bytes in the order of decreasing significance (network byte order).
6262
//!
63-
//! #### `string`
63+
//! ### `string`: arbitrary length *binary* string
64+
//! #### [`Decode`]/[`Encode`] trait impls: `Vec<u8>`, `String`, `bytes::Bytes` (requires `bytes` crate feature)
6465
//!
65-
//! Arbitrary length binary string.
66+
//! *NOTE: `string` is effectively equivalent to `byte[]`, however the latter is not defined in
67+
//! [RFC4251] and so trait impls in this crate for bytestring types like `[u8; N]` and `Vec<u8>`
68+
//! are described as being impls of `string`*.
6669
//!
6770
//! Strings are allowed to contain arbitrary binary data, including null characters and 8-bit
6871
//! characters.
6972
//!
70-
//! They are stored as a `uint32` containing its length
71-
//! (number of bytes that follow) and zero (= empty string) or more
72-
//! bytes that are the value of the string. Terminating null
73+
//! They are stored as a `uint32` containing its length (number of bytes that follow) and
74+
//! zero (= empty string) or more bytes that are the value of the string. Terminating null
7375
//! characters are not used.
7476
//!
75-
//! Strings are also used to store text. In that case, US-ASCII is
76-
//! used for internal names, and ISO-10646 UTF-8 for text that might
77-
//! be displayed to the user. The terminating null character SHOULD
78-
//! NOT normally be stored in the string. For example: the US-ASCII
79-
//! string "testing" is represented as `00 00 00 07 t e s t i n g`. The
80-
//! UTF-8 mapping does not alter the encoding of US-ASCII characters.
77+
//! Strings are also used to store text. In that case, US-ASCII is used for internal names, and
78+
//! ISO-10646 UTF-8 for text that might be displayed to the user.
79+
//!
80+
//! The terminating null character SHOULD NOT normally be stored in the string.
8181
//!
82-
//! #### `mpint`
82+
//! For example: the US-ASCII string "testing" is represented as `00 00 00 07 t e s t i n g`.
83+
//! The UTF-8 mapping does not alter the encoding of US-ASCII characters.
8384
//!
84-
//! Represents multiple precision integers in two's complement format,
85-
//! stored as a string, 8 bits per byte, MSB first.
85+
//! ### `mpint`: multiple precision integers in two's complement format
86+
//!
87+
//! Stored as a byte string, 8 bits per byte, MSB first (a.k.a. big endian).
8688
//!
8789
//! Negative numbers have the value 1 as the most significant bit of the first byte of
8890
//! the data partition. If the most significant bit would be set for
@@ -94,7 +96,7 @@
9496
//! By convention, a number that is used in modular computations in
9597
//! `Z_n` SHOULD be represented in the range `0 <= x < n`.
9698
//!
97-
//! Examples:
99+
//! #### Examples:
98100
//!
99101
//! value (hex) | representation (hex)
100102
//! -------------------|---------------------
@@ -104,9 +106,8 @@
104106
//! `-1234` | `00 00 00 02 ed cc`
105107
//! `-deadbeef` | `00 00 00 05 ff 21 52 41 11`
106108
//!
107-
//! #### `name-list`
108-
//!
109-
//! A string containing a comma-separated list of names.
109+
//! ### `name-list`: string containing a comma-separated list of names
110+
//! #### [`Decode`]/[`Encode`] trait impls: `Vec<String>`
110111
//!
111112
//! A `name-list` is represented as a `uint32` containing its length
112113
//! (number of bytes that follow) followed by a comma-separated list of zero or more
@@ -125,7 +126,7 @@
125126
//! Terminating null characters MUST NOT be used, neither
126127
//! for the individual names, nor for the list as a whole.
127128
//!
128-
//! Examples:
129+
//! #### Examples:
129130
//!
130131
//! value | representation (hex)
131132
//! ---------------------------|---------------------
@@ -134,6 +135,7 @@
134135
//! `("zlib,none")` | `00 00 00 09 7a 6c 69 62 2c 6e 6f 6e 65`
135136
//!
136137
//! [RFC3066]: https://datatracker.ietf.org/doc/html/rfc3066
138+
//! [RFC4251]: https://datatracker.ietf.org/doc/html/rfc4251
137139
//! [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
138140
//!
139141
//! ## Deriving [`Decode`] and [`Encode`]
@@ -247,3 +249,6 @@ pub use crate::pem::{DecodePem, EncodePem};
247249

248250
#[cfg(feature = "derive")]
249251
pub use ssh_derive::{Decode, Encode};
252+
253+
#[cfg(all(doc, feature = "alloc"))]
254+
use alloc::vec::Vec;

0 commit comments

Comments
 (0)