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
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;
use quote::{quote, ToTokens};
use crate::{
error::Errors,
schema::{types::Type, Schema},
};
use self::{
input_object::InputObjectOutput, interface::InterfaceOutput, named_type::NamedType,
object::ObjectOutput, subtype_markers::SubtypeMarkers,
};
pub fn use_schema(input: UseSchemaParams) -> Result<TokenStream, Errors> {
use quote::TokenStreamExt;
let document = crate::schema::load_schema(input.schema_filename)
.map_err(|e| e.into_syn_error(proc_macro2::Span::call_site()))?;
let schema = Schema::new(&document).validate()?;
let mut output = TokenStream::new();
let root_types = schema_roots::RootTypes::from_definitions(&document.definitions, &schema)?;
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);
let ident = proc_macro2::Ident::from(def.marker_ident());
output.append_all(quote! {
pub struct #ident {}
impl ::cynic::schema::NamedType for #ident {
const NAME: &'static str = #name;
}
});
}
Type::Scalar(_) => {}
Type::Object(def) => {
subtype_markers.extend(SubtypeMarkers::from_object(&def));
ObjectOutput::new(def).to_tokens(&mut output);
}
Type::Interface(def) => {
subtype_markers.push(SubtypeMarkers::from_interface(&def));
InterfaceOutput::new(def).to_tokens(&mut output);
}
Type::Union(def) => {
subtype_markers.extend(SubtypeMarkers::from_union(&def));
let ident = proc_macro2::Ident::from(def.marker_ident());
output.append_all(quote! {
pub struct #ident {}
});
}
Type::Enum(def) => {
let ident = proc_macro2::Ident::from(def.marker_ident());
output.append_all(quote! {
pub struct #ident {}
});
}
Type::InputObject(def) => {
InputObjectOutput::new(def).to_tokens(&mut output);
}
}
}
output.append_all(quote! {
#(#subtype_markers)*
#(#named_types)*
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;
pub trait Variable {
const TYPE: VariableType;
}
impl<T> Variable for &T where T: 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 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 Variable for bool {
const TYPE: VariableType = VariableType::Named("Boolean");
}
impl Variable for String {
const TYPE: VariableType = VariableType::Named("String");
}
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)
}