cynic_codegen/use_schema/
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
mod fields;
mod input_object;
mod interface;
mod named_type;
mod object;
mod params;
mod schema_roots;
mod subtype_markers;

pub use params::UseSchemaParams;

use {
    proc_macro2::TokenStream,
    quote::{quote, ToTokens},
};

use crate::{
    error::Errors,
    schema::{types::Type, Schema, SchemaInput, Validated},
};

use self::{
    input_object::InputObjectOutput, interface::InterfaceOutput, named_type::NamedType,
    object::ObjectOutput, subtype_markers::SubtypeMarkers,
};

pub fn use_schema(input: UseSchemaParams) -> Result<TokenStream, Errors> {
    let input = SchemaInput::from_schema_path(input.schema_filename)
        .map_err(|e| e.into_syn_error(proc_macro2::Span::call_site()))?;

    let schema = Schema::new(input).validate()?;
    use_schema_impl(&schema)
}

pub(crate) fn use_schema_impl(schema: &Schema<'_, Validated>) -> Result<TokenStream, Errors> {
    use quote::TokenStreamExt;

    let mut output = TokenStream::new();
    let mut field_module = TokenStream::new();

    let root_types = schema.root_types()?;
    output.append_all(quote! {
        #root_types
    });

    let mut subtype_markers = Vec::new();
    let mut named_types = Vec::new();

    for definition in schema.iter() {
        named_types.extend(NamedType::from_def(&definition));

        match definition {
            Type::Scalar(def) if !def.builtin => {
                let name = proc_macro2::Literal::string(def.name.as_ref());
                let ident = def.marker_ident().to_rust_ident();
                output.append_all(quote! {
                    pub struct #ident {}
                    impl cynic::schema::NamedType for #ident {
                        const NAME: &'static ::core::primitive::str = #name;
                    }
                });
            }
            Type::Scalar(_) => {}
            Type::Object(def) => {
                subtype_markers.extend(SubtypeMarkers::from_object(&def));

                let object = ObjectOutput::new(def);
                object.to_tokens(&mut output);
                object.append_fields(&mut field_module);
            }
            Type::Interface(def) => {
                subtype_markers.push(SubtypeMarkers::from_interface(&def));

                let iface = InterfaceOutput::new(def);
                iface.to_tokens(&mut output);
                iface.append_fields(&mut field_module);
            }
            Type::Union(def) => {
                subtype_markers.extend(SubtypeMarkers::from_union(&def));

                let ident = def.marker_ident().to_rust_ident();
                output.append_all(quote! {
                    pub struct #ident {}
                });
            }
            Type::Enum(def) => {
                let ident = def.marker_ident().to_rust_ident();
                output.append_all(quote! {
                    pub struct #ident {}
                });
            }
            Type::InputObject(def) => {
                let object = InputObjectOutput::new(def);
                object.to_tokens(&mut output);
                object.append_fields(&mut field_module);
            }
        }
    }

    output.append_all(quote! {
        #(#subtype_markers)*
        #(#named_types)*

        #[allow(non_snake_case, non_camel_case_types)]
        pub mod __fields {
            #field_module
        }

        pub type Boolean = bool;
        pub type String = std::string::String;
        pub type Float = f64;
        pub type Int = i32;
        pub type ID = cynic::Id;

        pub mod variable {
            use cynic::variables::VariableType;

            /// Used to determine the type of a given variable that
            /// appears in an argument struct.
            pub trait Variable {
                const TYPE: VariableType;
            }

            impl<T> Variable for &T
            where
                T: ?::core::marker::Sized + Variable,
            {
                const TYPE: VariableType = T::TYPE;
            }

            impl<T> Variable for Option<T>
            where
                T: Variable
            {
                const TYPE: VariableType = VariableType::Nullable(&T::TYPE);
            }

            impl<T> Variable for [T]
            where
                T: Variable,
            {
                const TYPE: VariableType = VariableType::List(&T::TYPE);
            }

            impl<T> Variable for Vec<T>
            where
                T: Variable,
            {
                const TYPE: VariableType = VariableType::List(&T::TYPE);
            }

            impl<T> Variable for Box<T>
            where
                T: Variable,
            {
                const TYPE: VariableType = T::TYPE;
            }

            impl<T> Variable for std::rc::Rc<T>
            where
                T: Variable,
            {
                const TYPE: VariableType = T::TYPE;
            }

            impl<T> Variable for std::sync::Arc<T>
            where
                T: Variable,
            {
                const TYPE: VariableType = T::TYPE;
            }

            impl<T> Variable for std::borrow::Cow<'_, T>
            where
                T: ?::core::marker::Sized + Variable + ToOwned,
            {
                const TYPE: VariableType = T::TYPE;
            }

            impl Variable for bool {
                const TYPE: VariableType = VariableType::Named("Boolean");
            }

            impl Variable for str {
                const TYPE: VariableType = VariableType::Named("String");
            }

            impl Variable for String {
                const TYPE: VariableType = <str as Variable>::TYPE;
            }

            impl Variable for f64 {
                const TYPE: VariableType = VariableType::Named("Float");
            }

            impl Variable for i32 {
                const TYPE: VariableType = VariableType::Named("Int");
            }

            impl Variable for cynic::Id {
                const TYPE: VariableType = VariableType::Named("ID");
            }
        }
    });

    Ok(output)
}