cynic_codegen/
generics_for_serde.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
use syn::parse_quote;

pub(crate) fn with_de_and_deserialize_bounds(generics: &syn::Generics) -> syn::Generics {
    let mut generics_with_de_and_deserialize_bounds = generics.clone();
    generics_with_de_and_deserialize_bounds
        .params
        .push(parse_quote!('de));
    for generic in &generics.params {
        match generic {
            syn::GenericParam::Type(type_) => {
                let ident = &type_.ident;
                generics_with_de_and_deserialize_bounds
                    .make_where_clause()
                    .predicates
                    .push(parse_quote! { #ident: cynic::serde::Deserialize<'de> })
            }
            syn::GenericParam::Lifetime(_) | syn::GenericParam::Const(_) => {}
        }
    }
    generics_with_de_and_deserialize_bounds
}

pub(crate) fn with_serialize_bounds(generics: &syn::Generics) -> syn::Generics {
    let mut generics_with_serialize_bounds = generics.clone();
    for generic in &generics.params {
        match generic {
            syn::GenericParam::Type(type_) => {
                let ident = &type_.ident;
                generics_with_serialize_bounds
                    .make_where_clause()
                    .predicates
                    .push(parse_quote! { #ident: cynic::serde::Serialize })
            }
            syn::GenericParam::Lifetime(_) | syn::GenericParam::Const(_) => {}
        }
    }
    generics_with_serialize_bounds
}