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 `der` 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 decode;
28mod encode;
29mod field_ir;
30
31use crate::{decode::DeriveDecode, encode::DeriveEncode, field_ir::FieldIr};
32use proc_macro::TokenStream;
33use syn::{parse_macro_input, DeriveInput};
34
35/// Derive the [`Decode`][1] trait on a `struct`.
36///
37/// [1]: https://docs.rs/ssh-derive/latest/ssh-derive/trait.Decode.html
38#[proc_macro_derive(Decode, attributes(ssh))]
39pub fn derive_decode(input: TokenStream) -> TokenStream {
40    let input = parse_macro_input!(input as DeriveInput);
41    match DeriveDecode::new(input) {
42        Ok(t) => t.to_tokens().into(),
43        Err(e) => e.to_compile_error().into(),
44    }
45}
46
47/// Derive the [`Encode`][1] trait on a `struct`.
48///
49/// [1]: https://docs.rs/ssh-derive/latest/ssh-derive/trait.Encode.html
50#[proc_macro_derive(Encode, attributes(ssh))]
51pub fn derive_encode(input: TokenStream) -> TokenStream {
52    let input = parse_macro_input!(input as DeriveInput);
53    match DeriveEncode::new(input) {
54        Ok(t) => t.to_tokens().into(),
55        Err(e) => e.to_compile_error().into(),
56    }
57}