Skip to content

Let struct visibility to be same as module #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions graphql_client/examples/github/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions graphql_client_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
15 changes: 15 additions & 0 deletions graphql_client_cli/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ 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,
selected_operation: String,
additional_derives: Option<String>,
deprecation_strategy: &Option<String>,
no_formatting: bool,
module_visibility: &Option<String>,
output: &PathBuf,
) -> Result<(), failure::Error> {
let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str());
Expand All @@ -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: <Token![pub]>::default(),
}.into(),
Some("private") => syn::Visibility::Inherited {},
_ => syn::VisPublic {
pub_token: <Token![pub]>::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))?;
Expand Down
10 changes: 8 additions & 2 deletions graphql_client_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
extern crate failure;
extern crate reqwest;

#[macro_use]
extern crate structopt;
#[macro_use]
extern crate graphql_client;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String>,
#[structopt(parse(from_os_str))]
output: PathBuf,
},
Expand All @@ -77,6 +81,7 @@ fn main() -> Result<(), failure::Error> {
additional_derives,
deprecation_strategy,
no_formatting,
module_visibility,
output,
} => generate::generate_code(
query_path,
Expand All @@ -85,6 +90,7 @@ fn main() -> Result<(), failure::Error> {
additional_derives,
&deprecation_strategy,
no_formatting,
&module_visibility,
&output,
),
}
Expand Down
6 changes: 5 additions & 1 deletion graphql_client_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,6 +72,8 @@ pub struct GraphQLClientDeriveOptions {
pub additional_derives: Option<String>,
/// The deprecation strategy to adopt.
pub deprecation_strategy: Option<deprecation::DeprecationStrategy>,
/// target struct visibility.
pub module_visibility: Visibility,
}

/// Generates the code for a Rust module given a query, a schema and options.
Expand All @@ -81,6 +84,7 @@ pub fn generate_module_token_stream(
) -> Result<TokenStream, failure::Error> {
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.
Expand Down Expand Up @@ -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)]
Expand Down
3 changes: 2 additions & 1 deletion graphql_query_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}