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(crate = path)]` on the type to select the path to `ssh-encoding`.
29/// The default is `::ssh_encoding`.
30///
31/// Use `#[ssh(error = crate::Error)]` on the type to override the default
32/// `ssh_encoding::Error`. The chosen type must implement `From<ssh_encoding::Error>`
33/// and `From` for each field's decoding error.
34///
35/// [1]: https://docs.rs/ssh-encoding/latest/ssh_encoding/trait.Decode.html
36#[proc_macro_derive(Decode, attributes(ssh))]
37pub fn derive_decode(input: TokenStream) -> TokenStream {
38    let input = parse_macro_input!(input as DeriveInput);
39    match decode::try_derive_decode(input) {
40        Ok(t) => t.into(),
41        Err(e) => e.to_compile_error().into(),
42    }
43}
44
45/// Derive the [`Encode`][1] trait for a struct or enum.
46///
47/// Use `#[ssh(crate = path)]` on the type to select the path to `ssh-encoding`.
48/// The default is `::ssh_encoding`.
49///
50/// [1]: https://docs.rs/ssh-encoding/latest/ssh_encoding/trait.Encode.html
51#[proc_macro_derive(Encode, attributes(ssh))]
52pub fn derive_encode(input: TokenStream) -> TokenStream {
53    let input = parse_macro_input!(input as DeriveInput);
54    match encode::try_derive_encode(input) {
55        Ok(t) => t.into(),
56        Err(e) => e.to_compile_error().into(),
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::{decode, encode};
63    use quote::quote;
64
65    #[test]
66    fn reject_ssh_attributes_on_variants() {
67        for derive in [encode::try_derive_encode, decode::try_derive_decode] {
68            for option in [
69                quote!(length_prefixed),
70                quote!(error = crate::Error),
71                quote!(unknown),
72            ] {
73                let input = syn::parse_quote! {
74                    #[repr(u8)]
75                    enum Message {
76                        #[ssh(#option)]
77                        Data(u8) = 1,
78                    }
79                };
80                let err = derive(input).expect_err("variant attribute should be rejected");
81                assert_eq!(
82                    err.to_string(),
83                    "`#[ssh(...)]` is unsupported on enum variants"
84                );
85            }
86        }
87    }
88
89    #[test]
90    fn reject_duplicate_error_type() {
91        let input = syn::parse_quote! {
92            #[ssh(error = First)]
93            #[ssh(error = Second)]
94            struct Message;
95        };
96        let err = decode::try_derive_decode(input).expect_err("duplicate error type");
97        assert_eq!(err.to_string(), "duplicate `error` attribute");
98    }
99
100    #[test]
101    fn reject_duplicate_crate_path() {
102        let input = syn::parse_quote! {
103            #[ssh(crate = first, crate = second)]
104            struct Message;
105        };
106        let err = encode::try_derive_encode(input).expect_err("duplicate crate path");
107        assert_eq!(err.to_string(), "duplicate `crate` attribute");
108    }
109}