bigml_derive/lib.rs
1// `proc_macro` is built into the compiler.
2extern crate proc_macro;
3// `proc_macro2` wraps `proc_macro` in a way that should be compatible with the
4// upcoming `proc_macro` additions, so that we can use the old or new
5// `proc_macro` APIs with minimal tweaking.
6extern crate proc_macro2;
7#[macro_use]
8extern crate quote;
9extern crate syn;
10
11// In this file, we want `proc_macro::TokenStream` to interface with the outside
12// world.
13use proc_macro::TokenStream;
14
15mod resource;
16mod updatable;
17
18/// Derive boilerplate code for `Resource`.
19#[proc_macro_derive(Resource, attributes(api_name))]
20pub fn resource_derive(input: TokenStream) -> TokenStream {
21 // Rust procedural macros are really limited right now:
22 //
23 // - We can only parse `TokenStream` using the `syn` library. This is
24 // because `TokenStream` hasn't been fully standardized.
25 // - We can only report errors via a panic.
26 let input = syn::parse(input).unwrap();
27 let gen = resource::derive(&input);
28 gen.into()
29}
30
31/// Derive boilerplate code for `Updatable`.
32#[proc_macro_derive(Updatable, attributes(updatable))]
33pub fn updatable_derive(input: TokenStream) -> TokenStream {
34 let input = syn::parse(input).unwrap();
35 let gen = updatable::derive(&input);
36 gen.into()
37}