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 on a `struct`.
27///
28/// [1]: https://docs.rs/ssh-derive/latest/ssh-derive/trait.Decode.html
29#[proc_macro_derive(Decode, attributes(ssh))]
30pub fn derive_decode(input: TokenStream) -> TokenStream {
31    let input = parse_macro_input!(input as DeriveInput);
32    match decode::try_derive_decode(input) {
33        Ok(t) => t.into(),
34        Err(e) => e.to_compile_error().into(),
35    }
36}
37
38/// Derive the [`Encode`][1] trait on a `struct`.
39///
40/// [1]: https://docs.rs/ssh-derive/latest/ssh-derive/trait.Encode.html
41#[proc_macro_derive(Encode, attributes(ssh))]
42pub fn derive_encode(input: TokenStream) -> TokenStream {
43    let input = parse_macro_input!(input as DeriveInput);
44    match encode::try_derive_encode(input) {
45        Ok(t) => t.into(),
46        Err(e) => e.to_compile_error().into(),
47    }
48}