1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! This crate implements derive macros for the `gotham_formdata` crate.
#![warn(missing_docs, rust_2018_idioms)]
#![deny(unreachable_pub)]
#![forbid(unsafe_code)]

#[macro_use]
extern crate quote;

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use syn::{parse_macro_input, DeriveInput, Result};

mod form_data;
mod util;

#[inline]
fn print_tokens(tokens: TokenStream2) -> TokenStream {
	//eprintln!("{}", tokens);
	tokens.into()
}

#[inline]
fn expand_derive<F>(input: TokenStream, expand: F) -> TokenStream
where
	F: FnOnce(DeriveInput) -> Result<TokenStream2>
{
	print_tokens(expand(parse_macro_input!(input)).unwrap_or_else(|err| err.to_compile_error()))
}

/// This derive macro implements `FormData` for the struct it is invoked on. Enums, unions and
/// tuple structs are not supported.
#[proc_macro_derive(FormData, attributes(validate))]
pub fn derive_form_data(input: TokenStream) -> TokenStream {
	expand_derive(input, form_data::expand)
}