Skip to content

Commit 63bd4ad

Browse files
committed
Add visibility option
1 parent 9ec26b9 commit 63bd4ad

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

graphql_client_cli/src/generate.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use graphql_client_codegen::*;
33
use std::fs::File;
44
use std::io::Write as IoWrite;
55
use std::path::PathBuf;
6+
use syn;
67

78
pub fn generate_code(
89
query_path: PathBuf,
@@ -11,6 +12,7 @@ pub fn generate_code(
1112
additional_derives: Option<String>,
1213
deprecation_strategy: Option<String>,
1314
no_formatting: bool,
15+
module_visibility: Option<String>,
1416
output: PathBuf,
1517
) -> Result<(), failure::Error> {
1618
let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str());
@@ -21,10 +23,22 @@ pub fn generate_code(
2123
_ => None,
2224
};
2325

26+
let module_visibility = module_visibility.as_ref().map(|s| s.as_str());
27+
let module_visibility = match module_visibility {
28+
Some("pub") => syn::VisPublic {
29+
pub_token: <Token![pub]>::default(),
30+
}.into(),
31+
Some("private") => syn::Visibility::Inherited {},
32+
_ => syn::VisPublic {
33+
pub_token: <Token![pub]>::default(),
34+
}.into(),
35+
};
36+
2437
let options = GraphQLClientDeriveOptions {
2538
struct_name: selected_operation,
2639
additional_derives,
2740
deprecation_strategy,
41+
module_visibility,
2842
};
2943

3044
let gen = generate_module_token_stream(query_path, schema_path, Some(options))?;

graphql_client_cli/src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ extern crate graphql_client_codegen;
1010
extern crate serde_derive;
1111
extern crate serde;
1212
extern crate serde_json;
13+
#[macro_use]
14+
extern crate syn;
1315

1416
#[cfg(feature = "rustfmt")]
1517
extern crate rustfmt_nightly as rustfmt;
@@ -57,6 +59,10 @@ enum Cli {
5759
/// Formating feature is disabled as default installation.
5860
#[structopt(long = "no-formatting")]
5961
no_formatting: bool,
62+
/// You can choose module and target struct visibility from pub and private.
63+
/// Default value is pub.
64+
#[structopt(short = "m", long = "module_visibility")]
65+
module_visibility: Option<String>,
6066
#[structopt(parse(from_os_str))]
6167
output: PathBuf,
6268
},
@@ -77,6 +83,7 @@ fn main() -> Result<(), failure::Error> {
7783
additional_derives,
7884
deprecation_strategy,
7985
no_formatting,
86+
module_visibility,
8087
output,
8188
} => generate::generate_code(
8289
query_path,
@@ -85,6 +92,7 @@ fn main() -> Result<(), failure::Error> {
8592
additional_derives,
8693
deprecation_strategy,
8794
no_formatting,
95+
module_visibility,
8896
output,
8997
),
9098
}

graphql_client_codegen/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern crate syn;
2323
extern crate quote;
2424

2525
use proc_macro2::TokenStream;
26+
use syn::Visibility;
2627

2728
/// Derive-related code. This will be moved into graphql_query_derive.
2829
pub mod attributes;
@@ -71,6 +72,8 @@ pub struct GraphQLClientDeriveOptions {
7172
pub additional_derives: Option<String>,
7273
/// The deprecation strategy to adopt.
7374
pub deprecation_strategy: Option<deprecation::DeprecationStrategy>,
75+
/// target struct visibility.
76+
pub module_visibility: Visibility,
7477
}
7578

7679
/// Generates the code for a Rust module given a query, a schema and options.
@@ -81,6 +84,7 @@ pub fn generate_module_token_stream(
8184
) -> Result<TokenStream, failure::Error> {
8285
let options = options.unwrap();
8386

87+
let module_visibility = options.module_visibility;
8488
let response_derives = options.additional_derives;
8589

8690
// The user can determine what to do about deprecations.
@@ -157,7 +161,7 @@ pub fn generate_module_token_stream(
157161
)?;
158162

159163
let result = quote!(
160-
pub mod #module_name {
164+
#module_visibility mod #module_name {
161165
#![allow(non_camel_case_types)]
162166
#![allow(non_snake_case)]
163167
#![allow(dead_code)]

graphql_query_derive/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ fn build_graphql_client_derive_options(input: &syn::DeriveInput) -> GraphQLClien
4444
.unwrap_or(deprecation::DeprecationStrategy::default());
4545

4646
GraphQLClientDeriveOptions {
47-
struct_name: input.ident.to_string(),
47+
struct_name: input.clone().ident.to_string(),
4848
additional_derives: response_derives,
4949
deprecation_strategy: Some(deprecation_strategy),
50+
module_visibility: input.clone().vis,
5051
}
5152
}

0 commit comments

Comments
 (0)