1extern crate proc_macro2;
2
3use proc_macro::TokenStream;
4use quote::quote;
5use syn::Data;
6
7#[proc_macro_derive(ToRow)]
8pub fn derive_into_hash_map(item: TokenStream) -> TokenStream {
9 let input = syn::parse_macro_input!(item as syn::DeriveInput);
10
11 let struct_identifier = &input.ident;
12
13 match &input.data {
14 Data::Struct(syn::DataStruct { fields, .. }) => {
15 let mut to_row_impl = quote! {
16 let mut v = vec![];
17 };
18 let mut to_label_impl = quote! {
19 let mut v = vec![];
20 };
21 for field in fields {
22 let identifier = field.ident.as_ref().unwrap();
23 to_row_impl.extend(quote! {
24 v.push(self.#identifier.ref_to_cell());
25 });
26 to_label_impl.extend(quote! {
27 v.push(stringify!(#identifier).to_string());
28 })
29 }
30 quote! {
31 impl ToRow for #struct_identifier {
32 fn to_row(&self) -> Vec<Cell> {
33 #to_row_impl
34 v
35 }
36 fn labels(&self) -> Vec<String> {
37 #to_label_impl
38 v
39 }
40 }
41 }
42 }
43 _ => unimplemented!(),
44 }
45 .into()
46}