Skip to content

Commit e910fa4

Browse files
committed
Solidify CLI codegen params into a struct
Also remove no-op options in the process.
1 parent 55fdc16 commit e910fa4

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
lines changed

graphql_client_cli/src/generate.rs

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,60 @@ use graphql_client_codegen::{
44
};
55
use std::fs::File;
66
use std::io::Write as IoWrite;
7-
use std::path::{Path, PathBuf};
7+
use std::path::PathBuf;
88
use syn;
99

10-
#[allow(clippy::too_many_arguments)]
11-
pub fn generate_code(
12-
query_path: PathBuf,
13-
schema_path: &Path,
14-
module_name: String,
15-
selected_operation: Option<String>,
16-
additional_derives: Option<String>,
17-
deprecation_strategy: Option<&str>,
18-
no_formatting: bool,
19-
module_visibility: Option<&str>,
20-
output: &Path,
21-
) -> Result<(), failure::Error> {
22-
let deprecation_strategy = match deprecation_strategy {
10+
pub(crate) struct CliCodegenParams {
11+
pub query_path: PathBuf,
12+
pub schema_path: PathBuf,
13+
pub selected_operation: Option<String>,
14+
pub additional_derives: Option<String>,
15+
pub deprecation_strategy: Option<String>,
16+
pub no_formatting: bool,
17+
pub module_visibility: Option<String>,
18+
pub output: PathBuf,
19+
}
20+
21+
pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Error> {
22+
let deprecation_strategy = match params.deprecation_strategy.as_ref().map(|s| s.as_str()) {
2323
Some("allow") => Some(deprecation::DeprecationStrategy::Allow),
2424
Some("deny") => Some(deprecation::DeprecationStrategy::Deny),
2525
Some("warn") => Some(deprecation::DeprecationStrategy::Warn),
2626
_ => None,
2727
};
2828

29-
let module_visibility = match module_visibility {
30-
Some("pub") => syn::VisPublic {
31-
pub_token: <Token![pub]>::default(),
32-
}
33-
.into(),
34-
Some("private") => syn::Visibility::Inherited {},
35-
_ => syn::VisPublic {
29+
let mut options = GraphQLClientCodegenOptions::new_default();
30+
31+
// options.set_module_name(module_name);
32+
options.set_module_visibility(
33+
syn::VisPublic {
3634
pub_token: <Token![pub]>::default(),
3735
}
3836
.into(),
39-
};
40-
41-
let mut options = GraphQLClientCodegenOptions::new_default();
42-
43-
options.set_module_name(module_name);
44-
options.set_module_visibility(module_visibility);
37+
);
4538

46-
if let Some(selected_operation) = selected_operation {
39+
if let Some(selected_operation) = params.selected_operation {
4740
options.set_operation_name(selected_operation);
4841
}
4942

50-
if let Some(additional_derives) = additional_derives {
43+
if let Some(additional_derives) = params.additional_derives {
5144
options.set_additional_derives(additional_derives);
5245
}
5346

5447
if let Some(deprecation_strategy) = deprecation_strategy {
5548
options.set_deprecation_strategy(deprecation_strategy);
5649
}
5750

58-
let gen = generate_module_token_stream(query_path, &schema_path, options)?;
51+
let gen = generate_module_token_stream(params.query_path, &params.schema_path, options)?;
5952

6053
let generated_code = gen.to_string();
61-
let generated_code = if cfg!(feature = "rustfmt") && !no_formatting {
54+
let generated_code = if cfg!(feature = "rustfmt") && !params.no_formatting {
6255
format(&generated_code)
6356
} else {
6457
generated_code
6558
};
6659

67-
let mut file = File::create(output)?;
60+
let mut file = File::create(params.output)?;
6861

6962
write!(file, "{}", generated_code)?;
7063

graphql_client_cli/src/main.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ enum Cli {
4747
/// Path to graphql schema file.
4848
#[structopt(parse(from_os_str))]
4949
schema_path: PathBuf,
50-
/// Name of module.
51-
module_name: String,
5250
/// Name of target query. If you don't set this parameter, cli generate all queries in query file.
5351
#[structopt(short = "o", long = "selected-operation")]
5452
selected_operation: Option<String>,
@@ -87,24 +85,22 @@ fn main() -> Result<(), failure::Error> {
8785
Cli::Generate {
8886
query_path,
8987
schema_path,
90-
module_name,
9188
selected_operation,
9289
additional_derives,
9390
deprecation_strategy,
9491
no_formatting,
9592
module_visibility,
9693
output,
97-
} => generate::generate_code(
94+
} => generate::generate_code(generate::CliCodegenParams {
9895
query_path,
99-
&schema_path,
100-
module_name,
96+
schema_path,
10197
selected_operation,
10298
additional_derives,
103-
deprecation_strategy.as_ref().map(String::as_str),
99+
deprecation_strategy,
104100
no_formatting,
105-
module_visibility.as_ref().map(String::as_str),
106-
&output,
107-
),
101+
module_visibility,
102+
output,
103+
}),
108104
}
109105
}
110106

0 commit comments

Comments
 (0)