Skip to content

Commit 1836410

Browse files
committed
Merge remote-tracking branch 'upstream/master' into code-gen
2 parents a630e4f + a0b2f1e commit 1836410

File tree

7 files changed

+37
-5
lines changed

7 files changed

+37
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515

1616
- (BREAKING) GraphQLQuery does not take a lifetime parameter anymore. This makes it easier to work with futures in async client, since futures expect everything they capture to have the 'static lifetime.
1717

18+
### Fixed
19+
20+
- When using edition 2018, you no longer need to add `#[macro_use] extern crate serde_derive` to your crate for the generated modules to compile (thanks @aergonaut!)
21+
1822
## 0.5.1 (2018-10-07)
1923

2024
### Added

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/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ structopt = "0.2"
1818
serde = "1.0"
1919
serde_derive = "1.0"
2020
serde_json = "1.0"
21+
syn = "0.15"
2122

2223
rustfmt-nightly = { version = "0.99" , optional = true }
2324

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
#[allow(too_many_arguments)]
89
pub fn generate_code(
@@ -13,6 +14,7 @@ pub fn generate_code(
1314
additional_derives: Option<String>,
1415
deprecation_strategy: &Option<String>,
1516
no_formatting: bool,
17+
module_visibility: &Option<String>,
1618
output: &PathBuf,
1719
) -> Result<(), failure::Error> {
1820
let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str());
@@ -23,12 +25,24 @@ pub fn generate_code(
2325
_ => None,
2426
};
2527

28+
let module_visibility = module_visibility.as_ref().map(|s| s.as_str());
29+
let module_visibility = match module_visibility {
30+
Some("pub") => syn::VisPublic {
31+
pub_token: <Token![pub]>::default(),
32+
}.into(),
33+
Some("private") => syn::Visibility::Inherited {},
34+
_ => syn::VisPublic {
35+
pub_token: <Token![pub]>::default(),
36+
}.into(),
37+
};
38+
2639
let options = GraphQLClientDeriveOptions {
2740
operation_name: selected_operation,
2841
struct_name: None,
2942
module_name: Some(module_name),
3043
additional_derives,
3144
deprecation_strategy,
45+
module_visibility,
3246
};
3347

3448
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;
@@ -60,6 +60,10 @@ enum Cli {
6060
/// Formating feature is disabled as default installation.
6161
#[structopt(long = "no-formatting")]
6262
no_formatting: bool,
63+
/// You can choose module and target struct visibility from pub and private.
64+
/// Default value is pub.
65+
#[structopt(short = "m", long = "module_visibility")]
66+
module_visibility: Option<String>,
6367
#[structopt(parse(from_os_str))]
6468
output: PathBuf,
6569
},
@@ -81,6 +85,7 @@ fn main() -> Result<(), failure::Error> {
8185
additional_derives,
8286
deprecation_strategy,
8387
no_formatting,
88+
module_visibility,
8489
output,
8590
} => generate::generate_code(
8691
query_path,
@@ -90,6 +95,7 @@ fn main() -> Result<(), failure::Error> {
9095
additional_derives,
9196
&deprecation_strategy,
9297
no_formatting,
98+
&module_visibility,
9399
&output,
94100
),
95101
}

graphql_client_codegen/src/lib.rs

Lines changed: 9 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;
@@ -76,6 +77,8 @@ pub struct GraphQLClientDeriveOptions {
7677
pub additional_derives: Option<String>,
7778
/// The deprecation strategy to adopt.
7879
pub deprecation_strategy: Option<deprecation::DeprecationStrategy>,
80+
/// target struct visibility.
81+
pub module_visibility: Visibility,
7982
}
8083

8184
/// Generates the code for a Rust module given a query, a schema and options.
@@ -86,6 +89,7 @@ pub fn generate_module_token_stream(
8689
) -> Result<TokenStream, failure::Error> {
8790
let options = options.unwrap();
8891

92+
let module_visibility = options.module_visibility.clone();
8993
let response_derives = options.additional_derives.clone();
9094

9195
// The user can determine what to do about deprecations.
@@ -187,6 +191,7 @@ pub fn generate_module_token_stream(
187191
}
188192

189193
let result = build_module_token_stream(
194+
module_visibility,
190195
&module_name,
191196
&struct_name,
192197
&query_string,
@@ -197,6 +202,7 @@ pub fn generate_module_token_stream(
197202
}
198203

199204
fn build_module_token_stream(
205+
module_visibility: syn::Visibility,
200206
module_name: &Ident,
201207
struct_name: &Option<Ident>,
202208
query_string: &str,
@@ -219,6 +225,7 @@ fn build_module_token_stream(
219225
}
220226

221227
merge_with_common_token_stream(
228+
module_visibility,
222229
&module_name,
223230
query_string,
224231
schema_token_streams,
@@ -227,13 +234,14 @@ fn build_module_token_stream(
227234
}
228235

229236
fn merge_with_common_token_stream(
237+
module_visibility: syn::Visibility,
230238
module_name: &Ident,
231239
query_string: &str,
232240
schema_token_streams: Vec<TokenStream>,
233241
trait_token_streams: Vec<TokenStream>,
234242
) -> TokenStream {
235243
quote!(
236-
pub mod #module_name {
244+
#module_visibility mod #module_name {
237245
#![allow(non_camel_case_types)]
238246
#![allow(non_snake_case)]
239247
#![allow(dead_code)]

graphql_query_derive/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ fn build_graphql_client_derive_options(input: &syn::DeriveInput) -> GraphQLClien
5656
module_name: Some(input.ident.to_string()),
5757
additional_derives: response_derives,
5858
deprecation_strategy: Some(deprecation_strategy),
59+
module_visibility: input.clone().vis,
5960
}
6061
}

0 commit comments

Comments
 (0)