ssh-encoding: add derive feature + extend derive functionallity#348
Merged
tarcieri merged 4 commits intoApr 19, 2025
Merged
Conversation
Extend the derive macros to support all struct and enum types, not just
simple structs.
This also introduces some custom attributes to control the derived code.
Supported attributes:
- `#[ssh(length_prefixed)]`: will use length-prefix encoding/decoding
for the struct/enum as a whole, or per field, depending on where the
attribute is placed.
- `#[repr(u8)]`, `#[repr(u32)]` etc.: Enum discriminants will be
encoded/decoded depending on the specified repr used.
This is used in derived `Decode` implementations for enums. It holds a u128 because discriminants may be as large as u128/i128. Storing the value isn't strictly necessary, but allows a better error message.
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.
db0b490 to
2583df4
Compare
tarcieri
reviewed
Mar 30, 2025
tarcieri
reviewed
Apr 19, 2025
Comment on lines
+39
to
+40
| /// Invalid discriminant value in message. | ||
| InvalidDiscriminant(u128), |
Member
There was a problem hiding this comment.
It seems weird to me to conflate custom derive errors with this error type. Perhaps you could just define an error type in ssh-derive?
Member
|
Going to go ahead and merge this with one nit |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I wanted to be able to derive
EncodeandDecodefor various message types in RFC 4253 and related RFCs. As the current implementation has some limitations (and I wanted to get some more macro-writing practice), I've gone ahead and extendedssh-derivequite a bit. The result is a rather substantial refactor, but it now supports all types of structs as well as enums. I realize this might be a bit much to review, but seeing asssh-deriveis0.0.1-alphaI figured it might be alright.This PR is really in two parts (let me know if it is better merged as two separate things):
ssh-derive:Encode/Decodeon all kinds of structs and enums.#[ssh(length_prefixed)]to use length prefixing when encoding/decoding. Can be applies to the struct/enum itself, or to individual fields.#[repr(u8)]and friends: Enum discriminants will encode/decode with the type specified by thereprattribute.ssh-encoding:derivefeature that re-exports the derive macros fromssh-derive.ssh_encoding::Errorto handle unexpected discriminant values when decoding.derivefeature.