Skip to content

Commit db0b490

Browse files
committed
ssh-encoding: add derive feature
Adds an optional feature that re-exports `ssh-derive` macros and exposes a documentation module with examples. The examples also serve as tests. Also adds tests for the derive feature in the form of declaring various structs and enums and asserting that encoding/decoding works as expected.
1 parent fbf14e2 commit db0b490

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ssh-derive/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! Custom derive support for the [`ssh-encoding`] crate.
55
//!
66
//! Note that this crate shouldn't be used directly, but instead accessed
7-
//! by using the `derive` feature of the `der` crate, which re-exports this crate's
7+
//! by using the `derive` feature of the [`ssh-encoding`] crate, which re-exports this crate's
88
//! macros from the toplevel.
99
//!
1010
//! [`ssh-encoding`]: ../ssh-encoding

ssh-encoding/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ base64ct = { version = "1.7", optional = true }
2020
bytes = { version = "1", optional = true, default-features = false }
2121
digest = { version = "=0.11.0-pre.9", optional = true, default-features = false }
2222
pem-rfc7468 = { version = "1.0.0-rc.2", optional = true }
23+
ssh-derive = { version = "0.0.1-alpha", optional = true, path = "../ssh-derive" }
2324

2425
[dev-dependencies]
2526
hex-literal = "0.4.1"
@@ -30,6 +31,7 @@ alloc = ["base64ct?/alloc", "pem-rfc7468?/alloc"]
3031
base64 = ["dep:base64ct"]
3132
bytes = ["alloc", "dep:bytes"]
3233
pem = ["base64", "dep:pem-rfc7468"]
34+
derive = ["ssh-derive"]
3335

3436
[package.metadata.docs.rs]
3537
all-features = true

ssh-encoding/src/derive.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//! # Deriving [`Encode`] and [`Decode`]
2+
//!
3+
//! The traits [`Encode`] and [`Decode`] can be derived for any struct or enum where all its fields
4+
//! implement [`Encode`] and [`Decode`].
5+
//!
6+
//! [`Encode`]: [crate::Encode]
7+
//! [`Decode`]: [crate::Decode]
8+
//! ## Example
9+
//!
10+
//! Here is an example of how you could define a handful of the SSH message types.
11+
//! ```
12+
//! use ssh_encoding::{Decode, Encode};
13+
//!
14+
//! #[derive(Debug, PartialEq, Encode, Decode)]
15+
//! #[repr(u8)]
16+
//! enum Message {
17+
//! Disconnect {
18+
//! reason_code: u32,
19+
//! description: String,
20+
//! language_tag: String,
21+
//! } = 1,
22+
//! EcdhInit {
23+
//! client_public_key: Vec<u8>,
24+
//! } = 30,
25+
//! EcdhReply {
26+
//! host_key: HostKey,
27+
//! server_public_key: Vec<u8>,
28+
//! #[ssh(length_prefixed)]
29+
//! host_signature: HostSignature,
30+
//! } = 31,
31+
//! }
32+
//!
33+
//! #[derive(Debug, PartialEq, Encode, Decode)]
34+
//! #[ssh(length_prefixed)]
35+
//! struct HostKey {
36+
//! key_type: String,
37+
//! ecdsa_curve_identifier: String,
38+
//! ecdsa_public_key: Vec<u8>,
39+
//! }
40+
//!
41+
//! #[derive(Debug, PartialEq, Encode, Decode)]
42+
//! struct HostSignature {
43+
//! signature_type: String,
44+
//! signature: Vec<u8>,
45+
//! }
46+
//!
47+
//! let message = Message::EcdhReply {
48+
//! host_key: HostKey {
49+
//! key_type: "ecdsa-sha2-nistp256".into(),
50+
//! ecdsa_curve_identifier: "nistp256".into(),
51+
//! ecdsa_public_key: vec![0x01, 0x02, 0x03],
52+
//! },
53+
//! server_public_key: vec![0x04, 0x05, 0x06],
54+
//! host_signature: HostSignature {
55+
//! signature_type: "ecdsa-sha2-nistp256".into(),
56+
//! signature: vec![0x07, 0x08, 0x09],
57+
//! },
58+
//! };
59+
//!
60+
//! let encoded = message.encode_vec().unwrap();
61+
//! assert_eq!(&encoded[..13], &[31, 0, 0, 0, 42, 0, 0, 0, 19, 101, 99, 100, 115]);
62+
//! let decoded = Message::decode(&mut &encoded[..]).unwrap();
63+
//! assert_eq!(message, decoded);
64+
//! ```

ssh-encoding/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,8 @@ pub use digest;
6262

6363
#[cfg(feature = "pem")]
6464
pub use crate::pem::{DecodePem, EncodePem};
65+
66+
#[cfg(feature = "derive")]
67+
pub use ssh_derive::{Decode, Encode};
68+
#[cfg(feature = "derive")]
69+
pub mod derive;

0 commit comments

Comments
 (0)