Skip to content

Commit 2bd409f

Browse files
committed
Add visibility option
1 parent 4cd6cb7 commit 2bd409f

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

graphql_client/examples/github/src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ extern crate dotenv;
22
extern crate envy;
33
#[macro_use]
44
extern crate failure;
5-
#[macro_use]
65
extern crate graphql_client;
76
#[macro_use]
87
extern crate log;
@@ -12,7 +11,6 @@ extern crate serde;
1211
extern crate serde_json;
1312
#[macro_use]
1413
extern crate serde_derive;
15-
#[macro_use]
1614
extern crate structopt;
1715
#[macro_use]
1816
extern crate prettytable;

graphql_client_cli/src/generate.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ 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

8+
#[allow(too_many_arguments)]
79
pub fn generate_code(
810
query_path: PathBuf,
911
schema_path: PathBuf,
1012
selected_operation: String,
1113
additional_derives: Option<String>,
1214
deprecation_strategy: &Option<String>,
1315
no_formatting: bool,
16+
module_visibility: &Option<String>,
1417
output: &PathBuf,
1518
) -> Result<(), failure::Error> {
1619
let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str());
@@ -21,10 +24,22 @@ pub fn generate_code(
2124
_ => None,
2225
};
2326

27+
let module_visibility = module_visibility.as_ref().map(|s| s.as_str());
28+
let module_visibility = match module_visibility {
29+
Some("pub") => syn::VisPublic {
30+
pub_token: <Token![pub]>::default(),
31+
}.into(),
32+
Some("private") => syn::Visibility::Inherited {},
33+
_ => syn::VisPublic {
34+
pub_token: <Token![pub]>::default(),
35+
}.into(),
36+
};
37+
2438
let options = GraphQLClientDeriveOptions {
2539
struct_name: selected_operation,
2640
additional_derives,
2741
deprecation_strategy,
42+
module_visibility,
2843
};
2944

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

graphql_client_cli/src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
extern crate failure;
22
extern crate reqwest;
3-
4-
#[macro_use]
53
extern crate structopt;
64
#[macro_use]
75
extern crate graphql_client;
@@ -10,6 +8,8 @@ extern crate graphql_client_codegen;
108
extern crate serde_derive;
119
extern crate serde;
1210
extern crate serde_json;
11+
#[macro_use]
12+
extern crate syn;
1313

1414
#[cfg(feature = "rustfmt")]
1515
extern crate rustfmt_nightly as rustfmt;
@@ -57,6 +57,10 @@ enum Cli {
5757
/// Formating feature is disabled as default installation.
5858
#[structopt(long = "no-formatting")]
5959
no_formatting: bool,
60+
/// You can choose module and target struct visibility from pub and private.
61+
/// Default value is pub.
62+
#[structopt(short = "m", long = "module_visibility")]
63+
module_visibility: Option<String>,
6064
#[structopt(parse(from_os_str))]
6165
output: PathBuf,
6266
},
@@ -77,6 +81,7 @@ fn main() -> Result<(), failure::Error> {
7781
additional_derives,
7882
deprecation_strategy,
7983
no_formatting,
84+
module_visibility,
8085
output,
8186
} => generate::generate_code(
8287
query_path,
@@ -85,6 +90,7 @@ fn main() -> Result<(), failure::Error> {
8590
additional_derives,
8691
&deprecation_strategy,
8792
no_formatting,
93+
&module_visibility,
8894
&output,
8995
),
9096
}

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.
@@ -155,7 +159,7 @@ pub fn generate_module_token_stream(
155159
)?;
156160

157161
let result = quote!(
158-
pub mod #module_name {
162+
#module_visibility mod #module_name {
159163
#![allow(non_camel_case_types)]
160164
#![allow(non_snake_case)]
161165
#![allow(dead_code)]

graphql_query_derive/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ fn build_graphql_client_derive_options(input: &syn::DeriveInput) -> GraphQLClien
4343
let deprecation_strategy = deprecation::extract_deprecation_strategy(input).unwrap_or_default();
4444

4545
GraphQLClientDeriveOptions {
46-
struct_name: input.ident.to_string(),
46+
struct_name: input.clone().ident.to_string(),
4747
additional_derives: response_derives,
4848
deprecation_strategy: Some(deprecation_strategy),
49+
module_visibility: input.clone().vis,
4950
}
5051
}

0 commit comments

Comments
 (0)