|
| 1 | +use crate::deprecation::DeprecationStrategy; |
| 2 | +use heck::SnakeCase; |
| 3 | +use proc_macro2::{Ident, Span}; |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | +use syn::Visibility; |
| 6 | + |
| 7 | +/// Used to configure code generation. |
| 8 | +#[derive(Debug, Default)] |
| 9 | +pub struct GraphQLClientCodegenOptions { |
| 10 | + /// Name of the operation we want to generate code for. If it does not match, we use all queries. |
| 11 | + pub operation_name: Option<String>, |
| 12 | + /// The name of implemention target struct. |
| 13 | + pub struct_name: Option<String>, |
| 14 | + /// The name of the module that will contains queries. |
| 15 | + pub module_name: Option<String>, |
| 16 | + /// Comma-separated list of additional traits we want to derive. |
| 17 | + additional_derives: Option<String>, |
| 18 | + /// The deprecation strategy to adopt. |
| 19 | + deprecation_strategy: Option<DeprecationStrategy>, |
| 20 | + /// Target module visibility. |
| 21 | + module_visibility: Option<Visibility>, |
| 22 | + /// A path to a file to include in the module to force Cargo to take into account changes in |
| 23 | + /// the query files when recompiling. |
| 24 | + query_file: Option<PathBuf>, |
| 25 | + /// A path to a file to include in the module to force Cargo to take into account changes in |
| 26 | + /// the schema files when recompiling. |
| 27 | + schema_file: Option<PathBuf>, |
| 28 | +} |
| 29 | + |
| 30 | +impl GraphQLClientCodegenOptions { |
| 31 | + /// Creates an empty options object with default params. It probably wants to be configured. |
| 32 | + pub fn new_default() -> GraphQLClientCodegenOptions { |
| 33 | + std::default::Default::default() |
| 34 | + } |
| 35 | + |
| 36 | + /// The module name, either one that was set explicitly, or the operation name, as snake case. |
| 37 | + pub(crate) fn module_name_ident(&self) -> Option<Ident> { |
| 38 | + self.module_name |
| 39 | + .as_ref() |
| 40 | + .or_else(|| self.operation_name.as_ref()) |
| 41 | + .map(|s| s.to_snake_case()) |
| 42 | + .map(|module_name| Ident::new(&module_name, Span::call_site())) |
| 43 | + } |
| 44 | + |
| 45 | + /// The visibility (public/private) to apply to the target module. |
| 46 | + pub(crate) fn module_visibility(&self) -> &Visibility { |
| 47 | + self.module_visibility |
| 48 | + .as_ref() |
| 49 | + .unwrap_or(&Visibility::Inherited) |
| 50 | + } |
| 51 | + |
| 52 | + /// The deprecation strategy to adopt. |
| 53 | + pub(crate) fn deprecation_strategy(&self) -> DeprecationStrategy { |
| 54 | + self.deprecation_strategy.clone().unwrap_or_default() |
| 55 | + } |
| 56 | + |
| 57 | + /// A path to a file to include in the module to force Cargo to take into account changes in |
| 58 | + /// the query files when recompiling. |
| 59 | + pub fn set_query_file(&mut self, path: PathBuf) { |
| 60 | + self.query_file = Some(path); |
| 61 | + } |
| 62 | + |
| 63 | + /// Comma-separated list of additional traits we want to derive. |
| 64 | + pub fn additional_derives(&self) -> Option<&str> { |
| 65 | + self.additional_derives.as_ref().map(String::as_str) |
| 66 | + } |
| 67 | + |
| 68 | + /// Comma-separated list of additional traits we want to derive. |
| 69 | + pub fn set_additional_derives(&mut self, additional_derives: String) { |
| 70 | + self.additional_derives = Some(additional_derives); |
| 71 | + } |
| 72 | + |
| 73 | + /// The deprecation strategy to adopt. |
| 74 | + pub fn set_deprecation_strategy(&mut self, deprecation_strategy: DeprecationStrategy) { |
| 75 | + self.deprecation_strategy = Some(deprecation_strategy); |
| 76 | + } |
| 77 | + |
| 78 | + /// The name of the module that will contains queries. |
| 79 | + pub fn set_module_name(&mut self, module_name: String) { |
| 80 | + self.module_name = Some(module_name); |
| 81 | + } |
| 82 | + |
| 83 | + /// Target module visibility. |
| 84 | + pub fn set_module_visibility(&mut self, visibility: Visibility) { |
| 85 | + self.module_visibility = Some(visibility); |
| 86 | + } |
| 87 | + |
| 88 | + /// The name of implemention target struct. |
| 89 | + pub fn set_struct_name(&mut self, struct_name: String) { |
| 90 | + self.struct_name = Some(struct_name); |
| 91 | + } |
| 92 | + |
| 93 | + /// Name of the operation we want to generate code for. If none is selected, it means all |
| 94 | + /// operations. |
| 95 | + pub fn set_operation_name(&mut self, operation_name: String) { |
| 96 | + self.operation_name = Some(operation_name); |
| 97 | + } |
| 98 | + |
| 99 | + /// A path to a file to include in the module to force Cargo to take into account changes in |
| 100 | + /// the schema files when recompiling. |
| 101 | + pub fn schema_file(&self) -> Option<&Path> { |
| 102 | + self.schema_file.as_ref().map(PathBuf::as_path) |
| 103 | + } |
| 104 | + |
| 105 | + /// A path to a file to include in the module to force Cargo to take into account changes in |
| 106 | + /// the query files when recompiling. |
| 107 | + pub fn query_file(&self) -> Option<&Path> { |
| 108 | + self.query_file.as_ref().map(PathBuf::as_path) |
| 109 | + } |
| 110 | +} |
0 commit comments