graphql_client_codegen/
codegen_options.rs1use crate::deprecation::DeprecationStrategy;
2use crate::normalization::Normalization;
3use proc_macro2::Ident;
4use std::path::{Path, PathBuf};
5use syn::{self, Visibility};
6
7#[derive(Debug)]
9pub enum CodegenMode {
10 Cli,
12 Derive,
14}
15
16pub struct GraphQLClientCodegenOptions {
18 pub mode: CodegenMode,
20 pub operation_name: Option<String>,
22 pub struct_name: Option<String>,
24 struct_ident: Option<Ident>,
26 variables_derives: Option<String>,
28 response_derives: Option<String>,
30 deprecation_strategy: Option<DeprecationStrategy>,
32 module_visibility: Option<Visibility>,
34 query_file: Option<PathBuf>,
37 schema_file: Option<PathBuf>,
40 normalization: Normalization,
42 custom_scalars_module: Option<syn::Path>,
44 extern_enums: Vec<String>,
46 fragments_other_variant: bool,
48 skip_serializing_none: bool,
50}
51
52impl GraphQLClientCodegenOptions {
53 pub fn new(mode: CodegenMode) -> GraphQLClientCodegenOptions {
55 GraphQLClientCodegenOptions {
56 mode,
57 variables_derives: Default::default(),
58 response_derives: Default::default(),
59 deprecation_strategy: Default::default(),
60 module_visibility: Default::default(),
61 operation_name: Default::default(),
62 struct_ident: Default::default(),
63 struct_name: Default::default(),
64 query_file: Default::default(),
65 schema_file: Default::default(),
66 normalization: Normalization::None,
67 custom_scalars_module: Default::default(),
68 extern_enums: Default::default(),
69 fragments_other_variant: Default::default(),
70 skip_serializing_none: Default::default(),
71 }
72 }
73
74 pub(crate) fn module_visibility(&self) -> &Visibility {
76 self.module_visibility
77 .as_ref()
78 .unwrap_or(&Visibility::Inherited)
79 }
80
81 pub(crate) fn deprecation_strategy(&self) -> DeprecationStrategy {
83 self.deprecation_strategy.clone().unwrap_or_default()
84 }
85
86 pub fn set_query_file(&mut self, path: PathBuf) {
89 self.query_file = Some(path);
90 }
91
92 pub fn variables_derives(&self) -> Option<&str> {
94 self.variables_derives.as_deref()
95 }
96
97 pub fn set_variables_derives(&mut self, variables_derives: String) {
99 self.variables_derives = Some(variables_derives);
100 }
101
102 pub fn all_variable_derives(&self) -> impl Iterator<Item = &str> {
104 let additional = self
105 .variables_derives
106 .as_deref()
107 .into_iter()
108 .flat_map(|s| s.split(','))
109 .map(|s| s.trim());
110
111 std::iter::once("Serialize").chain(additional)
112 }
113
114 pub fn all_response_derives(&self) -> impl Iterator<Item = &str> {
116 let base_derives = std::iter::once("Deserialize");
117
118 base_derives.chain(
119 self.additional_response_derives()
120 .filter(|additional| additional != &"Deserialize"),
121 )
122 }
123
124 pub fn additional_response_derives(&self) -> impl Iterator<Item = &str> {
126 self.response_derives
127 .as_deref()
128 .into_iter()
129 .flat_map(|s| s.split(','))
130 .map(|s| s.trim())
131 }
132
133 pub fn set_response_derives(&mut self, response_derives: String) {
135 self.response_derives = Some(response_derives);
136 }
137
138 pub fn set_deprecation_strategy(&mut self, deprecation_strategy: DeprecationStrategy) {
140 self.deprecation_strategy = Some(deprecation_strategy);
141 }
142
143 pub fn set_module_visibility(&mut self, visibility: Visibility) {
145 self.module_visibility = Some(visibility);
146 }
147
148 pub fn set_struct_name(&mut self, struct_name: String) {
150 self.struct_name = Some(struct_name);
151 }
152
153 pub fn set_operation_name(&mut self, operation_name: String) {
156 self.operation_name = Some(operation_name);
157 }
158
159 pub fn schema_file(&self) -> Option<&Path> {
162 self.schema_file.as_deref()
163 }
164
165 pub fn query_file(&self) -> Option<&Path> {
168 self.query_file.as_deref()
169 }
170
171 pub fn set_struct_ident(&mut self, ident: Ident) {
173 self.struct_ident = Some(ident);
174 }
175
176 pub fn struct_ident(&self) -> Option<&proc_macro2::Ident> {
178 self.struct_ident.as_ref()
179 }
180
181 pub fn set_normalization(&mut self, norm: Normalization) {
183 self.normalization = norm;
184 }
185
186 pub fn normalization(&self) -> &Normalization {
188 &self.normalization
189 }
190
191 pub fn custom_scalars_module(&self) -> Option<&syn::Path> {
193 self.custom_scalars_module.as_ref()
194 }
195
196 pub fn set_custom_scalars_module(&mut self, module: syn::Path) {
198 self.custom_scalars_module = Some(module)
199 }
200
201 pub fn extern_enums(&self) -> &[String] {
203 &self.extern_enums
204 }
205
206 pub fn set_extern_enums(&mut self, enums: Vec<String>) {
208 self.extern_enums = enums;
209 }
210
211 pub fn set_fragments_other_variant(&mut self, fragments_other_variant: bool) {
213 self.fragments_other_variant = fragments_other_variant;
214 }
215
216 pub fn fragments_other_variant(&self) -> &bool {
218 &self.fragments_other_variant
219 }
220
221 pub fn set_skip_serializing_none(&mut self, skip_serializing_none: bool) {
223 self.skip_serializing_none = skip_serializing_none
224 }
225
226 pub fn skip_serializing_none(&self) -> &bool {
228 &self.skip_serializing_none
229 }
230}