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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned};

mod input;

use self::input::QueryVariablesDeriveInput;

pub fn query_variables_derive(ast: &syn::DeriveInput) -> Result<TokenStream, syn::Error> {
    use darling::FromDeriveInput;

    match QueryVariablesDeriveInput::from_derive_input(ast) {
        Ok(input) => query_variables_derive_impl(input),
        Err(e) => Ok(e.write_errors()),
    }
}

pub fn query_variables_derive_impl(
    input: QueryVariablesDeriveInput,
) -> Result<TokenStream, syn::Error> {
    let ident = &input.ident;
    let vis = &input.vis;
    let schema_module = &input.schema_module();
    let fields_struct_ident = format_ident!("{}Fields", ident);

    let input_fields = input.data.take_struct().unwrap().fields;

    let mut field_funcs = Vec::new();
    let mut variables = Vec::new();
    let mut field_inserts = Vec::new();

    for f in input_fields {
        let name = f.ident.as_ref().unwrap();
        let ty = &f.ty;
        let name_str =
            proc_macro2::Literal::string(&f.graphql_ident(input.rename_all).graphql_name());

        field_funcs.push(quote! {
            #vis fn #name() -> ::cynic::variables::VariableDefinition<#ident, #ty> {
                ::cynic::variables::VariableDefinition::new(#name_str)
            }
        });

        variables.push(quote! {
            (#name_str, <#ty as #schema_module::variable::Variable>::TYPE)
        });

        field_inserts.push(quote! {
            map_serializer.serialize_entry(#name_str, &self.#name)?;
        })
    }

    let map_len = field_inserts.len();

    let ident_span = ident.span();
    let fields_struct = quote_spanned! { ident_span =>
        #vis struct #fields_struct_ident;

        impl #fields_struct_ident {
            #(
                #field_funcs
            )*
        }
    };

    Ok(quote! {

        #[automatically_derived]
        impl ::cynic::QueryVariables for #ident {
            type Fields = #fields_struct_ident;

            const VARIABLES: &'static [(&'static str, ::cynic::variables::VariableType)]
                = &[#(#variables),*];
        }

        #[automatically_derived]
        impl ::cynic::serde::Serialize for #ident {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: ::cynic::serde::Serializer,
            {
                use ::cynic::serde::ser::SerializeMap;

                let mut map_serializer = serializer.serialize_map(Some(#map_len))?;

                #(#field_inserts)*

                map_serializer.end()
            }
        }

        impl ::cynic::queries::VariableMatch<#ident> for #ident {}

        #fields_struct
    })
}