From 4cd6cb77ead0c560abdc3cd6aa706933947a601a Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Sun, 21 Oct 2018 18:23:50 +0900 Subject: [PATCH 1/2] Add syn to graphql_client_cli --- graphql_client_cli/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/graphql_client_cli/Cargo.toml b/graphql_client_cli/Cargo.toml index 7b43ea3fd..36e4cd01e 100644 --- a/graphql_client_cli/Cargo.toml +++ b/graphql_client_cli/Cargo.toml @@ -18,6 +18,7 @@ structopt = "0.2" serde = "1.0" serde_derive = "1.0" serde_json = "1.0" +syn = "0.15" rustfmt-nightly = { version = "0.99" , optional = true } From 2bd409f92d92bf995b165a98fa371d1a79b4f8ca Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Sun, 21 Oct 2018 18:24:24 +0900 Subject: [PATCH 2/2] Add visibility option --- graphql_client/examples/github/src/main.rs | 2 -- graphql_client_cli/src/generate.rs | 15 +++++++++++++++ graphql_client_cli/src/main.rs | 10 ++++++++-- graphql_client_codegen/src/lib.rs | 6 +++++- graphql_query_derive/src/lib.rs | 3 ++- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/graphql_client/examples/github/src/main.rs b/graphql_client/examples/github/src/main.rs index 92d4e898a..fb99a5999 100644 --- a/graphql_client/examples/github/src/main.rs +++ b/graphql_client/examples/github/src/main.rs @@ -2,7 +2,6 @@ extern crate dotenv; extern crate envy; #[macro_use] extern crate failure; -#[macro_use] extern crate graphql_client; #[macro_use] extern crate log; @@ -12,7 +11,6 @@ extern crate serde; extern crate serde_json; #[macro_use] extern crate serde_derive; -#[macro_use] extern crate structopt; #[macro_use] extern crate prettytable; diff --git a/graphql_client_cli/src/generate.rs b/graphql_client_cli/src/generate.rs index 537bcc9cf..4cfa40d0c 100644 --- a/graphql_client_cli/src/generate.rs +++ b/graphql_client_cli/src/generate.rs @@ -3,7 +3,9 @@ use graphql_client_codegen::*; use std::fs::File; use std::io::Write as IoWrite; use std::path::PathBuf; +use syn; +#[allow(too_many_arguments)] pub fn generate_code( query_path: PathBuf, schema_path: PathBuf, @@ -11,6 +13,7 @@ pub fn generate_code( additional_derives: Option, deprecation_strategy: &Option, no_formatting: bool, + module_visibility: &Option, output: &PathBuf, ) -> Result<(), failure::Error> { let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str()); @@ -21,10 +24,22 @@ pub fn generate_code( _ => None, }; + let module_visibility = module_visibility.as_ref().map(|s| s.as_str()); + let module_visibility = match module_visibility { + Some("pub") => syn::VisPublic { + pub_token: ::default(), + }.into(), + Some("private") => syn::Visibility::Inherited {}, + _ => syn::VisPublic { + pub_token: ::default(), + }.into(), + }; + let options = GraphQLClientDeriveOptions { struct_name: selected_operation, additional_derives, deprecation_strategy, + module_visibility, }; let gen = generate_module_token_stream(query_path, schema_path, Some(options))?; diff --git a/graphql_client_cli/src/main.rs b/graphql_client_cli/src/main.rs index 8ac55492a..bc5b7e33f 100644 --- a/graphql_client_cli/src/main.rs +++ b/graphql_client_cli/src/main.rs @@ -1,7 +1,5 @@ extern crate failure; extern crate reqwest; - -#[macro_use] extern crate structopt; #[macro_use] extern crate graphql_client; @@ -10,6 +8,8 @@ extern crate graphql_client_codegen; extern crate serde_derive; extern crate serde; extern crate serde_json; +#[macro_use] +extern crate syn; #[cfg(feature = "rustfmt")] extern crate rustfmt_nightly as rustfmt; @@ -57,6 +57,10 @@ enum Cli { /// Formating feature is disabled as default installation. #[structopt(long = "no-formatting")] no_formatting: bool, + /// You can choose module and target struct visibility from pub and private. + /// Default value is pub. + #[structopt(short = "m", long = "module_visibility")] + module_visibility: Option, #[structopt(parse(from_os_str))] output: PathBuf, }, @@ -77,6 +81,7 @@ fn main() -> Result<(), failure::Error> { additional_derives, deprecation_strategy, no_formatting, + module_visibility, output, } => generate::generate_code( query_path, @@ -85,6 +90,7 @@ fn main() -> Result<(), failure::Error> { additional_derives, &deprecation_strategy, no_formatting, + &module_visibility, &output, ), } diff --git a/graphql_client_codegen/src/lib.rs b/graphql_client_codegen/src/lib.rs index dee658c24..2791d89c9 100644 --- a/graphql_client_codegen/src/lib.rs +++ b/graphql_client_codegen/src/lib.rs @@ -23,6 +23,7 @@ extern crate syn; extern crate quote; use proc_macro2::TokenStream; +use syn::Visibility; /// Derive-related code. This will be moved into graphql_query_derive. pub mod attributes; @@ -71,6 +72,8 @@ pub struct GraphQLClientDeriveOptions { pub additional_derives: Option, /// The deprecation strategy to adopt. pub deprecation_strategy: Option, + /// target struct visibility. + pub module_visibility: Visibility, } /// Generates the code for a Rust module given a query, a schema and options. @@ -81,6 +84,7 @@ pub fn generate_module_token_stream( ) -> Result { let options = options.unwrap(); + let module_visibility = options.module_visibility; let response_derives = options.additional_derives; // The user can determine what to do about deprecations. @@ -155,7 +159,7 @@ pub fn generate_module_token_stream( )?; let result = quote!( - pub mod #module_name { + #module_visibility mod #module_name { #![allow(non_camel_case_types)] #![allow(non_snake_case)] #![allow(dead_code)] diff --git a/graphql_query_derive/src/lib.rs b/graphql_query_derive/src/lib.rs index fc828db04..a552f68ce 100644 --- a/graphql_query_derive/src/lib.rs +++ b/graphql_query_derive/src/lib.rs @@ -43,8 +43,9 @@ fn build_graphql_client_derive_options(input: &syn::DeriveInput) -> GraphQLClien let deprecation_strategy = deprecation::extract_deprecation_strategy(input).unwrap_or_default(); GraphQLClientDeriveOptions { - struct_name: input.ident.to_string(), + struct_name: input.clone().ident.to_string(), additional_derives: response_derives, deprecation_strategy: Some(deprecation_strategy), + module_visibility: input.clone().vis, } }