enum_derived_macro/
lib.rs

1// #![doc = include_str!("../README.md")]
2#![forbid(unsafe_code)]
3extern crate proc_macro;
4
5mod rand;
6
7use proc_macro::TokenStream;
8
9use quote::quote;
10use syn::{parse_macro_input, DeriveInput};
11
12#[proc_macro_derive(Rand, attributes(custom_rand, weight))]
13pub fn derive_rand(input: TokenStream) -> TokenStream {
14    let mut input = parse_macro_input!(input as DeriveInput);
15
16    rand::expand_derive_rand(&mut input).unwrap_or_else(to_compile_errors)
17}
18
19fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro::TokenStream {
20    let compile_errors = errors.iter().map(syn::Error::to_compile_error);
21    TokenStream::from(quote!(#(#compile_errors)*))
22}