rsfbclient_derive/
lib.rs

1//! Macros of rsfbclient
2
3extern crate proc_macro;
4
5use proc_macro::TokenStream;
6use quote::quote;
7use syn::{Data, DataStruct, DeriveInput, Fields};
8
9/// Derive an [IntoParams<T>](../trait.IntoParams.html) implementation for structs.
10///
11/// This enables passing an instance of such a struct in places where named parameters
12/// are expected, using the field labels to associate field values with parameter names.
13///
14/// The fields' types must implement the [IntoParam<T>](../trait.IntoParam.html) trait.
15///
16/// Note that `Option<T>` may be used as a field type to indicate a nullable parameter.
17///
18/// Providing an instance of the struct with value `None` for such a field corresponds to
19/// passing a `null` value for that field.
20#[proc_macro_derive(IntoParams)]
21pub fn into_params_derive(input: TokenStream) -> TokenStream {
22    let input = syn::parse_macro_input!(input as DeriveInput);
23
24    let st_name = &input.ident;
25    let st_fields = match &input.data {
26        Data::Struct(DataStruct {
27            fields: Fields::Named(fields),
28            ..
29        }) => &fields.named,
30        _ => panic!("expected a struct with named fields"),
31    };
32    let st_fields_params = st_fields
33        .iter()
34        .map(|field| field.ident.as_ref().expect("Field name required"))
35        .map(|field| {
36            let field_str = field.to_string();
37            quote! { #field_str.to_string(), self.#field.into_param() }
38        });
39
40    let st_impl = quote! {
41        impl rsfbclient::IntoParams for #st_name {
42            fn to_params(self) -> rsfbclient::ParamsType {
43                use std::collections::HashMap;
44                use rsfbclient::{IntoParams, IntoParam, ParamsType};
45
46                let mut params = HashMap::new();
47
48                #(params.insert(#st_fields_params));*;
49
50                ParamsType::Named(params)
51            }
52        }
53    };
54
55    TokenStream::from(st_impl)
56}