ssh_derive/
lib.rs

1#![doc = include_str!("../README.md")]
2
3//! ## About
4//! Custom derive support for the [`ssh-encoding`] crate.
5//!
6//! Note that this crate shouldn't be used directly, but instead accessed
7//! by using the `derive` feature of the [`ssh-encoding`] crate, which re-exports this crate's
8//! macros from the toplevel.
9//!
10//! [`ssh-encoding`]: ../ssh-encoding
11
12#![crate_type = "proc-macro"]
13#![forbid(unsafe_code)]
14#![warn(
15    clippy::unwrap_used,
16    rust_2018_idioms,
17    trivial_casts,
18    unused_qualifications
19)]
20
21macro_rules! abort {
22    ( $tokens:expr, $message:expr $(,)? ) => {
23        return Err(syn::Error::new_spanned($tokens, $message))
24    };
25}
26
27mod attributes;
28mod decode;
29mod encode;
30
31use proc_macro::TokenStream;
32use syn::{DeriveInput, parse_macro_input};
33
34/// Derive the [`Decode`][1] trait on a `struct`.
35///
36/// [1]: https://docs.rs/ssh-derive/latest/ssh-derive/trait.Decode.html
37#[proc_macro_derive(Decode, attributes(ssh))]
38pub fn derive_decode(input: TokenStream) -> TokenStream {
39    let input = parse_macro_input!(input as DeriveInput);
40    match decode::try_derive_decode(input) {
41        Ok(t) => t.into(),
42        Err(e) => e.to_compile_error().into(),
43    }
44}
45
46/// Derive the [`Encode`][1] trait on a `struct`.
47///
48/// [1]: https://docs.rs/ssh-derive/latest/ssh-derive/trait.Encode.html
49#[proc_macro_derive(Encode, attributes(ssh))]
50pub fn derive_encode(input: TokenStream) -> TokenStream {
51    let input = parse_macro_input!(input as DeriveInput);
52    match encode::try_derive_encode(input) {
53        Ok(t) => t.into(),
54        Err(e) => e.to_compile_error().into(),
55    }
56}