Skip to main content

ssh_derive/
lib.rs

1#![crate_type = "proc-macro"]
2#![doc = include_str!("../README.md")]
3
4//! ## About
5//! Custom derive support for the [`ssh-encoding`] crate.
6//!
7//! Note that this crate shouldn't be used directly, but instead accessed
8//! by using the `derive` feature of the [`ssh-encoding`] crate, which re-exports this crate's
9//! macros from the toplevel.
10//!
11//! [`ssh-encoding`]: ../ssh-encoding
12
13macro_rules! abort {
14    ( $tokens:expr, $message:expr $(,)? ) => {
15        return Err(syn::Error::new_spanned($tokens, $message))
16    };
17}
18
19mod attributes;
20mod decode;
21mod encode;
22
23use proc_macro::TokenStream;
24use syn::{DeriveInput, parse_macro_input};
25
26/// Derive the [`Decode`][1] trait for a struct or enum.
27///
28/// Use `#[ssh(error = crate::Error)]` on the type to override the default
29/// `ssh_encoding::Error`. The chosen type must implement `From<ssh_encoding::Error>`
30/// and `From` for each field's decoding error.
31///
32/// [1]: https://docs.rs/ssh-encoding/latest/ssh_encoding/trait.Decode.html
33#[proc_macro_derive(Decode, attributes(ssh))]
34pub fn derive_decode(input: TokenStream) -> TokenStream {
35    let input = parse_macro_input!(input as DeriveInput);
36    match decode::try_derive_decode(input) {
37        Ok(t) => t.into(),
38        Err(e) => e.to_compile_error().into(),
39    }
40}
41
42/// Derive the [`Encode`][1] trait on a `struct`.
43///
44/// [1]: https://docs.rs/ssh-derive/latest/ssh-derive/trait.Encode.html
45#[proc_macro_derive(Encode, attributes(ssh))]
46pub fn derive_encode(input: TokenStream) -> TokenStream {
47    let input = parse_macro_input!(input as DeriveInput);
48    match encode::try_derive_encode(input) {
49        Ok(t) => t.into(),
50        Err(e) => e.to_compile_error().into(),
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::{decode, encode};
57    use quote::quote;
58
59    #[test]
60    fn reject_ssh_attributes_on_variants() {
61        for derive in [encode::try_derive_encode, decode::try_derive_decode] {
62            for option in [
63                quote!(length_prefixed),
64                quote!(error = crate::Error),
65                quote!(unknown),
66            ] {
67                let input = syn::parse_quote! {
68                    #[repr(u8)]
69                    enum Message {
70                        #[ssh(#option)]
71                        Data(u8) = 1,
72                    }
73                };
74                let err = derive(input).expect_err("variant attribute should be rejected");
75                assert_eq!(
76                    err.to_string(),
77                    "`#[ssh(...)]` is unsupported on enum variants"
78                );
79            }
80        }
81    }
82
83    #[test]
84    fn reject_duplicate_error_type() {
85        let input = syn::parse_quote! {
86            #[ssh(error = First)]
87            #[ssh(error = Second)]
88            struct Message;
89        };
90        let err = decode::try_derive_decode(input).expect_err("duplicate error type");
91        assert_eq!(err.to_string(), "duplicate `error` attribute");
92    }
93}