cynic_codegen/query_variable_literals_derive/
mod.rs

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
use {
    proc_macro2::TokenStream,
    quote::{format_ident, quote},
    syn::visit_mut::{self, VisitMut},
};

mod input;

use crate::generics_for_serde;

use self::input::QueryVariableLiteralsInput;

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

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

pub fn inlineable_variables_derive_impl(
    input: QueryVariableLiteralsInput,
) -> Result<TokenStream, syn::Error> {
    let ident = &input.ident;

    let (_, ty_generics, _) = input.generics.split_for_impl();
    let generics_with_ser = generics_for_serde::with_serialize_bounds(&input.generics);
    let (impl_generics_with_ser, _, where_clause_with_ser) = generics_with_ser.split_for_impl();

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

    let mut match_arms = Vec::new();

    for f in input_fields {
        let name = f.ident.as_ref().unwrap();

        let name_str =
            proc_macro2::Literal::string(&f.graphql_ident(input.rename_all).graphql_name());

        let mut match_arm_rhs =
            quote! { Some(cynic::queries::to_input_literal(&self.#name).ok()?) };

        if let Some(skip_check_fn) = f.skip_serializing_if {
            let skip_check_fn = &*skip_check_fn;
            match_arm_rhs = quote! {
                if #skip_check_fn(&self.#name) {
                    None
                } else {
                    #match_arm_rhs
                }
            }
        }

        match_arms.push(quote! {
            #name_str => #match_arm_rhs,
        })
    }

    Ok(quote! {
        #[automatically_derived]
        impl #impl_generics_with_ser cynic::QueryVariableLiterals for #ident #ty_generics #where_clause_with_ser {
            fn get(&self, variable_name: &str) -> Option<cynic::queries::InputLiteral> {
                match variable_name {
                    #(#match_arms)*
                    _ => None
                }
            }
        }
    })
}

struct TurnLifetimesToStatic;
impl VisitMut for TurnLifetimesToStatic {
    fn visit_lifetime_mut(&mut self, i: &mut syn::Lifetime) {
        i.ident = format_ident!("static");
        visit_mut::visit_lifetime_mut(self, i)
    }
}