diff --git a/.travis.yml b/.travis.yml index d4e894244..46612115d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ before_script: - if [ "$TRAVIS_RUST_VERSION" = "beta" ]; then (sudo apt-get update) fi - if [ "$TRAVIS_RUST_VERSION" = "beta" ]; then (sudo apt-get install -y nodejs) fi - if [ "$TRAVIS_RUST_VERSION" = "beta" ]; then (npm i -g prettier) fi - - if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (rustup component add rustfmt-preview clippy-preview) fi + - if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (rustup component add rustfmt clippy) fi - if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (rustup target add wasm32-unknown-unknown) fi - if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (cargo install -f wasm-bindgen-cli) fi script: @@ -23,6 +23,6 @@ script: - if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (cargo clippy -- -D warnings) fi - if [ "$TRAVIS_RUST_VERSION" = "beta" ]; then (prettier --debug-check -l './**/*.json' './**/*.graphql') fi - cargo test --all - # - cargo build --manifest-path=./examples/github/Cargo.toml - # - cargo build --manifest-path=./examples/web/Cargo.toml + - cargo build --manifest-path=./examples/github/Cargo.toml + - cargo build --manifest-path=./examples/web/Cargo.toml - if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (xvfb-run cargo test --manifest-path=./graphql_client_web/Cargo.toml --target wasm32-unknown-unknown) fi diff --git a/CHANGELOG.md b/CHANGELOG.md index dd0179c38..da0ffbaa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## Unreleased -This release sees the switch to the Rust 2018 edition and is only compatible with Rust 1.31.0 and later. +This release sees the switch to the Rust 2018 edition - it is +only compatible with Rust 1.31.0 and later. ## 0.7.1 diff --git a/README.md b/README.md index cd8d12969..c94c1d211 100644 --- a/README.md +++ b/README.md @@ -33,12 +33,8 @@ A typed GraphQL client library for Rust. - We now have everything we need to derive Rust types for our query. This is achieved through a procedural macro, as in the following snippet: - ```rust,skt-empty-main - extern crate serde; - #[macro_use] - extern crate serde_derive; - #[macro_use] - extern crate graphql_client; + ```rust + use graphql_client::GraphQLQuery; // The paths are relative to the directory where your `Cargo.toml` is located. // Both json and the GraphQL schema language are supported as sources for the schema @@ -60,12 +56,7 @@ A typed GraphQL client library for Rust. * We now need to create the complete payload that we are going to send to the server. For convenience, the [GraphQLQuery trait](https://docs.rs/graphql_client/latest/graphql_client/trait.GraphQLQuery.html), is implemented for the struct under derive, so a complete query body can be created this way: - ```rust,skt-empty-main - extern crate failure; - #[macro_use] - extern crate graphql_client; - extern crate reqwest; - + ```rust use graphql_client::{GraphQLQuery, Response}; #[derive(GraphQLQuery)] @@ -95,9 +86,8 @@ A typed GraphQL client library for Rust. The generated response types always derive `serde::Deserialize` but you may want to print them (`Debug`), compare them (`PartialEq`) or derive any other trait on it. You can achieve this with the `response_derives` option of the `graphql` attribute. Example: -```rust,skt-empty-main -#[macro_use] -extern crate graphql_client; +```rust +use graphql_client::GraphQLQuery; #[derive(GraphQLQuery)] #[graphql( @@ -117,9 +107,8 @@ The generated code will reference the scalar types as defined in the server sche The generated code has support for [`@deprecated`](http://facebook.github.io/graphql/June2018/#sec-Field-Deprecation) field annotations. You can configure how deprecations are handled via the `deprecated` argument in the `GraphQLQuery` derive: -```rust,skt-empty-main -#[macro_use] -extern crate graphql_client; +```rust +use graphql_client::GraphQLQuery; #[derive(GraphQLQuery)] #[graphql( @@ -145,15 +134,13 @@ You can write multiple operations in one query document (one `.graphql` file). Y Note that the struct and the operation in the GraphQL file *must* have the same name. We enforce this to make the generated code more predictable. -```rust,skt-empty-main -#[macro_use] -extern crate graphql_client; +```rust +use graphql_client::GraphQLQuery; #[derive(GraphQLQuery)] #[graphql( schema_path = "tests/unions/union_schema.graphql", query_path = "tests/unions/union_query.graphql", - selected_operation = "SearchQuery" )] pub struct UnionQuery; ``` diff --git a/examples/example_module/Cargo.toml b/examples/example_module/Cargo.toml deleted file mode 100644 index f4026fb65..000000000 --- a/examples/example_module/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "example_module" -version = "0.1.0" -authors = ["Tom Houlé "] - -[dependencies] -serde = "1.0.69" -serde_derive = "1.0.69" -graphql_client = { path = ".." } diff --git a/examples/example_module/src/custom_scalars.rs b/examples/example_module/src/custom_scalars.rs deleted file mode 100644 index dfcdf6585..000000000 --- a/examples/example_module/src/custom_scalars.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! We define the custom scalars present in the GitHub query. More precise types could be provided here (see tests), as long as they are deserializable. - -pub type URI = String; diff --git a/examples/example_module/src/lib.rs b/examples/example_module/src/lib.rs deleted file mode 100644 index 016ee45e8..000000000 --- a/examples/example_module/src/lib.rs +++ /dev/null @@ -1,17 +0,0 @@ -extern crate serde; -#[macro_use] -extern crate serde_derive; -#[macro_use] -extern crate graphql_client; - -pub mod custom_scalars; - -use custom_scalars::*; - -#[derive(GraphQLQuery)] -#[graphql( - schema_path = "../github/src/schema.graphql", - query_path = "../github/src/query_1.graphql", - response_derives = "Debug" -)] -pub struct ExampleModule; diff --git a/examples/github/Cargo.toml b/examples/github/Cargo.toml index 6b746481b..6f27baddd 100644 --- a/examples/github/Cargo.toml +++ b/examples/github/Cargo.toml @@ -2,6 +2,7 @@ name = "graphql_query_github_example" version = "0.1.0" authors = ["Tom Houlé "] +edition = "2018" [dependencies] failure = "*" diff --git a/examples/github/src/main.rs b/examples/github/src/main.rs index c81adf7d4..f99823773 100644 --- a/examples/github/src/main.rs +++ b/examples/github/src/main.rs @@ -1,21 +1,8 @@ -extern crate dotenv; -extern crate envy; -#[macro_use] -extern crate failure; -extern crate graphql_client; -#[macro_use] -extern crate log; -extern crate env_logger; -extern crate reqwest; -extern crate serde; -extern crate serde_json; -#[macro_use] -extern crate serde_derive; -extern crate structopt; -#[macro_use] -extern crate prettytable; - +use failure::*; use graphql_client::*; +use log::*; +use prettytable::*; +use serde::*; use structopt::StructOpt; type URI = String; @@ -51,7 +38,7 @@ fn main() -> Result<(), failure::Error> { dotenv::dotenv().ok(); env_logger::init(); - let config: Env = envy::from_env()?; + let config: Env = envy::from_env().context("while reading from environment")?; let args = Command::from_args(); diff --git a/examples/web/Cargo.toml b/examples/web/Cargo.toml index 1e1701ba7..cc16ad000 100644 --- a/examples/web/Cargo.toml +++ b/examples/web/Cargo.toml @@ -11,10 +11,10 @@ lto = "thin" crate-type = ["cdylib"] [dependencies] +graphql_client = { path = "../../graphql_client" } graphql_client_web = { path = "../../graphql_client_web" } wasm-bindgen = "0.2.12" -serde = "1.0.67" -serde_derive = "1.0.67" +serde = { version = "1.0.67", features = ["derive"] } serde_json = "1.0.22" lazy_static = "1.0.1" js-sys = "0.3.6" diff --git a/examples/web/src/lib.rs b/examples/web/src/lib.rs index 7bed003f9..6ecfa59c7 100644 --- a/examples/web/src/lib.rs +++ b/examples/web/src/lib.rs @@ -1,13 +1,11 @@ use futures::Future; +use graphql_client_web::*; use lazy_static::*; use std::cell::RefCell; use std::sync::Mutex; - use wasm_bindgen::prelude::*; use wasm_bindgen_futures::future_to_promise; -use graphql_client_web::*; - #[derive(GraphQLQuery)] #[graphql( schema_path = "schema.json", diff --git a/graphql_client/Cargo.toml b/graphql_client/Cargo.toml index bc171533a..6b3c8a43a 100644 --- a/graphql_client/Cargo.toml +++ b/graphql_client/Cargo.toml @@ -11,13 +11,10 @@ edition = "2018" [dependencies] failure = "0.1" -graphql_query_derive = {path = "../graphql_query_derive", version = "0.7.1" } -serde = "^1.0.78" -serde_derive = "1.0" +graphql_query_derive = { path = "../graphql_query_derive", version = "0.7.1" } +serde = { version = "^1.0.78", features = ["derive"] } serde_json = "1.0" - -[build-dependencies] -skeptic = "0.13.4" +doc-comment = "0.3.1" [dev-dependencies] -skeptic = "0.13.4" +reqwest = "*" diff --git a/graphql_client/build.rs b/graphql_client/build.rs deleted file mode 100644 index 1fc7a4624..000000000 --- a/graphql_client/build.rs +++ /dev/null @@ -1,6 +0,0 @@ -extern crate skeptic; - -fn main() { - // Generates doc tests for the readme. - skeptic::generate_doc_tests(&["../README.md"]); -} diff --git a/graphql_client/src/lib.rs b/graphql_client/src/lib.rs index db1a82953..1b119962f 100644 --- a/graphql_client/src/lib.rs +++ b/graphql_client/src/lib.rs @@ -2,13 +2,18 @@ //! //! The main interface to this library is the custom derive that generates modules from a GraphQL query and schema. See the docs for the [`GraphQLQuery`] trait for a full example. -#![deny(warnings)] #![deny(missing_docs)] +#![deny(rust_2018_idioms)] +#![deny(warnings)] -use serde; +#[allow(unused_imports)] #[macro_use] -extern crate serde_derive; -pub use graphql_query_derive; +extern crate graphql_query_derive; + +#[doc(hidden)] +pub use graphql_query_derive::*; + +use serde::*; #[cfg(test)] use serde_json::json; @@ -19,6 +24,8 @@ pub use graphql_query_derive::*; use std::collections::HashMap; use std::fmt::{self, Display}; +doc_comment::doctest!("../../README.md"); + /// A convenience trait that can be used to build a GraphQL request body. /// /// This will be implemented for you by codegen in the normal case. It is implemented on the struct you place the derive on. @@ -26,14 +33,8 @@ use std::fmt::{self, Display}; /// Example: /// /// ``` -/// extern crate failure; -/// #[macro_use] -/// extern crate graphql_client; -/// #[macro_use] -/// extern crate serde_derive; -/// #[macro_use] -/// extern crate serde_json; -/// extern crate serde; +/// use graphql_client::*; +/// use serde_json::json; /// /// #[derive(GraphQLQuery)] /// #[graphql( @@ -127,12 +128,9 @@ impl Display for PathFragment { /// /// /// ``` -/// # extern crate failure; -/// # #[macro_use] -/// # extern crate serde_json; -/// # extern crate graphql_client; -/// # #[macro_use] -/// # extern crate serde_derive; +/// # use serde_json::json; +/// # use serde::Deserialize; +/// # use graphql_client::GraphQLQuery; /// # /// # #[derive(Debug, Deserialize, PartialEq)] /// # struct ResponseData { @@ -236,12 +234,9 @@ impl Display for Error { /// [Spec](https://github.com/facebook/graphql/blob/master/spec/Section%207%20--%20Response.md) /// /// ``` -/// # extern crate failure; -/// # #[macro_use] -/// # extern crate serde_json; -/// # extern crate graphql_client; -/// # #[macro_use] -/// # extern crate serde_derive; +/// # use serde_json::json; +/// # use serde::Deserialize; +/// # use graphql_client::GraphQLQuery; /// # /// # #[derive(Debug, Deserialize, PartialEq)] /// # struct User { diff --git a/graphql_client/tests/alias.rs b/graphql_client/tests/alias.rs index c4fc0d5a6..5f57667af 100644 --- a/graphql_client/tests/alias.rs +++ b/graphql_client/tests/alias.rs @@ -1,10 +1,5 @@ -#[macro_use] -extern crate graphql_client; - -#[macro_use] -extern crate serde_derive; -#[macro_use] -extern crate serde_json; +use graphql_client::*; +use serde_json::*; #[derive(GraphQLQuery)] #[graphql( diff --git a/graphql_client/tests/custom_scalars.rs b/graphql_client/tests/custom_scalars.rs index edc29e907..9205ef4d7 100644 --- a/graphql_client/tests/custom_scalars.rs +++ b/graphql_client/tests/custom_scalars.rs @@ -1,10 +1,5 @@ -#[macro_use] -extern crate graphql_client; -extern crate serde; -#[macro_use] -extern crate serde_derive; -#[macro_use] -extern crate serde_json; +use graphql_client::*; +use serde_json::json; use std::net::Ipv4Addr; diff --git a/graphql_client/tests/deprecation.rs b/graphql_client/tests/deprecation.rs index 1e2802af0..ed539483c 100644 --- a/graphql_client/tests/deprecation.rs +++ b/graphql_client/tests/deprecation.rs @@ -1,8 +1,4 @@ -#[macro_use] -extern crate graphql_client; -extern crate serde; -#[macro_use] -extern crate serde_derive; +use graphql_client::*; #[derive(GraphQLQuery)] #[graphql( diff --git a/graphql_client/tests/fragments.rs b/graphql_client/tests/fragments.rs index 112b78762..28572b0e7 100644 --- a/graphql_client/tests/fragments.rs +++ b/graphql_client/tests/fragments.rs @@ -1,10 +1,5 @@ -#[macro_use] -extern crate graphql_client; -extern crate serde; -#[macro_use] -extern crate serde_derive; -#[macro_use] -extern crate serde_json; +use graphql_client::*; +use serde_json::json; #[derive(GraphQLQuery)] #[graphql( diff --git a/graphql_client/tests/input_object_variables.rs b/graphql_client/tests/input_object_variables.rs index 835c67674..c31826ac6 100644 --- a/graphql_client/tests/input_object_variables.rs +++ b/graphql_client/tests/input_object_variables.rs @@ -1,9 +1,4 @@ -#[macro_use] -extern crate graphql_client; -#[macro_use] -extern crate serde_derive; -extern crate serde; -extern crate serde_json; +use graphql_client::*; #[derive(GraphQLQuery)] #[graphql( diff --git a/graphql_client/tests/interfaces.rs b/graphql_client/tests/interfaces.rs index c73387acb..7bb97f511 100644 --- a/graphql_client/tests/interfaces.rs +++ b/graphql_client/tests/interfaces.rs @@ -1,9 +1,4 @@ -#[macro_use] -extern crate graphql_client; -#[macro_use] -extern crate serde_derive; -extern crate serde; -extern crate serde_json; +use graphql_client::*; const RESPONSE: &'static str = include_str!("interfaces/interface_response.json"); diff --git a/graphql_client/tests/introspection.rs b/graphql_client/tests/introspection.rs index a4b448ff7..a261c2f25 100644 --- a/graphql_client/tests/introspection.rs +++ b/graphql_client/tests/introspection.rs @@ -1,9 +1,4 @@ -#[macro_use] -extern crate graphql_client; -#[macro_use] -extern crate serde_derive; -extern crate serde; -extern crate serde_json; +use graphql_client::*; #[derive(GraphQLQuery)] #[graphql( diff --git a/graphql_client/tests/json_schema.rs b/graphql_client/tests/json_schema.rs index ec803aae8..0da662570 100644 --- a/graphql_client/tests/json_schema.rs +++ b/graphql_client/tests/json_schema.rs @@ -1,10 +1,5 @@ -#[macro_use] -extern crate graphql_client; -#[macro_use] -extern crate serde_derive; -extern crate serde; -#[macro_use] -extern crate serde_json; +use graphql_client::*; +use serde_json::json; type Uuid = String; diff --git a/graphql_client/tests/more_derives.rs b/graphql_client/tests/more_derives.rs index 6b26d494c..f1ed1d7be 100644 --- a/graphql_client/tests/more_derives.rs +++ b/graphql_client/tests/more_derives.rs @@ -1,8 +1,4 @@ -#[macro_use] -extern crate graphql_client; -extern crate serde; -#[macro_use] -extern crate serde_derive; +use graphql_client::*; #[derive(GraphQLQuery)] #[graphql( diff --git a/graphql_client/tests/operation_selection.rs b/graphql_client/tests/operation_selection.rs index 0bd60247d..52ef6ef24 100644 --- a/graphql_client/tests/operation_selection.rs +++ b/graphql_client/tests/operation_selection.rs @@ -1,10 +1,3 @@ -#[macro_use] -extern crate graphql_client; -#[macro_use] -extern crate serde_derive; -extern crate serde; -extern crate serde_json; - use graphql_client::GraphQLQuery; #[derive(GraphQLQuery)] diff --git a/graphql_client/tests/scalar_variables.rs b/graphql_client/tests/scalar_variables.rs index 63aeaf0c2..4de4825be 100644 --- a/graphql_client/tests/scalar_variables.rs +++ b/graphql_client/tests/scalar_variables.rs @@ -1,9 +1,4 @@ -#[macro_use] -extern crate graphql_client; -#[macro_use] -extern crate serde_derive; - -use serde_json; +use graphql_client::*; #[derive(GraphQLQuery)] #[graphql( diff --git a/graphql_client/tests/skeptic.rs b/graphql_client/tests/skeptic.rs deleted file mode 100644 index ff46c9c01..000000000 --- a/graphql_client/tests/skeptic.rs +++ /dev/null @@ -1 +0,0 @@ -include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs")); diff --git a/graphql_client/tests/subscriptions.rs b/graphql_client/tests/subscriptions.rs index 6df0583e1..4335a9d7f 100644 --- a/graphql_client/tests/subscriptions.rs +++ b/graphql_client/tests/subscriptions.rs @@ -1,9 +1,4 @@ -#[macro_use] -extern crate graphql_client; -#[macro_use] -extern crate serde_derive; -extern crate serde; -extern crate serde_json; +use graphql_client::*; const RESPONSE: &str = include_str!("subscription/subscription_query_response.json"); diff --git a/graphql_client/tests/type_refining_fragments.rs b/graphql_client/tests/type_refining_fragments.rs index 2e2818abf..8ecae78c5 100644 --- a/graphql_client/tests/type_refining_fragments.rs +++ b/graphql_client/tests/type_refining_fragments.rs @@ -1,9 +1,4 @@ -#[macro_use] -extern crate graphql_client; -#[macro_use] -extern crate serde_derive; -extern crate serde; -extern crate serde_json; +use graphql_client::*; #[derive(GraphQLQuery)] #[graphql( diff --git a/graphql_client/tests/union_query.rs b/graphql_client/tests/union_query.rs index f65bbbb9d..ab1e3cfd6 100644 --- a/graphql_client/tests/union_query.rs +++ b/graphql_client/tests/union_query.rs @@ -1,9 +1,4 @@ -#[macro_use] -extern crate graphql_client; -#[macro_use] -extern crate serde_derive; -extern crate serde; -extern crate serde_json; +use graphql_client::*; const RESPONSE: &'static str = include_str!("unions/union_query_response.json"); diff --git a/graphql_client_cli/Cargo.toml b/graphql_client_cli/Cargo.toml index e7e9181d0..13d56447f 100644 --- a/graphql_client_cli/Cargo.toml +++ b/graphql_client_cli/Cargo.toml @@ -17,8 +17,7 @@ reqwest = "^0.9.0" graphql_client = { version = "0.7.1", path = "../graphql_client" } graphql_client_codegen = { path = "../graphql_client_codegen/", version = "0.7.1" } structopt = "0.2" -serde = "1.0" -serde_derive = "1.0" +serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" syn = "0.15" log = "0.4.0" diff --git a/graphql_client_cli/src/generate.rs b/graphql_client_cli/src/generate.rs index 06c37e651..2d8c65fd4 100644 --- a/graphql_client_cli/src/generate.rs +++ b/graphql_client_cli/src/generate.rs @@ -1,11 +1,11 @@ -use failure; +use failure::*; use graphql_client_codegen::{ generate_module_token_stream, CodegenMode, GraphQLClientCodegenOptions, }; use std::fs::File; use std::io::Write as _; use std::path::PathBuf; -use syn; +use syn::Token; pub(crate) struct CliCodegenParams { pub query_path: PathBuf, @@ -64,7 +64,7 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Err let query_file_name: ::std::ffi::OsString = query_path .file_name() - .map(|s| s.to_owned()) + .map(ToOwned::to_owned) .ok_or_else(|| format_err!("Failed to find a file name in the provided query path."))?; let dest_file_path: PathBuf = output_directory diff --git a/graphql_client_cli/src/introspect_schema.rs b/graphql_client_cli/src/introspect_schema.rs index afabdfcd2..f9045d5c5 100644 --- a/graphql_client_cli/src/introspect_schema.rs +++ b/graphql_client_cli/src/introspect_schema.rs @@ -1,8 +1,6 @@ -use failure; -use graphql_client; -use reqwest; +use failure::format_err; +use graphql_client::GraphQLQuery; use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE}; -use serde_json; use std::path::PathBuf; use std::str::FromStr; @@ -79,7 +77,7 @@ impl FromStr for Header { fn from_str(input: &str) -> Result { // error: colon required for name/value pair - if !input.contains(":") { + if !input.contains(':') { return Err(format_err!( "Invalid header input. A colon is required to separate the name and value. [{}]", input @@ -92,7 +90,7 @@ impl FromStr for Header { let value = name_value[1].trim(); // error: field name must be - if name.len() == 0 { + if name.is_empty() { return Err(format_err!( "Invalid header input. Field name is required before colon. [{}]", input diff --git a/graphql_client_cli/src/main.rs b/graphql_client_cli/src/main.rs index 258a96e5f..f6dba1f8e 100644 --- a/graphql_client_cli/src/main.rs +++ b/graphql_client_cli/src/main.rs @@ -1,22 +1,6 @@ -extern crate env_logger; -extern crate log; use env_logger::fmt::{Color, Style, StyledValue}; use log::Level; -#[macro_use] -extern crate failure; -extern crate reqwest; -extern crate structopt; -#[macro_use] -extern crate graphql_client; -extern crate graphql_client_codegen; -#[macro_use] -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; diff --git a/graphql_client_codegen/Cargo.toml b/graphql_client_codegen/Cargo.toml index eb87a6f58..d77b4672b 100644 --- a/graphql_client_codegen/Cargo.toml +++ b/graphql_client_codegen/Cargo.toml @@ -13,8 +13,7 @@ lazy_static = "1.0" quote = "0.6" syn = "0.15.20" proc-macro2 = { version = "0.4", features = [] } -serde = "^1.0.78" -serde_derive = "1.0" +serde = { version = "^1.0.78", features = ["derive"] } serde_json = "1.0" heck = "0.3" graphql-parser = "0.2.2" diff --git a/graphql_client_codegen/src/codegen.rs b/graphql_client_codegen/src/codegen.rs index 4b4e735b9..fa8ad3d1e 100644 --- a/graphql_client_codegen/src/codegen.rs +++ b/graphql_client_codegen/src/codegen.rs @@ -3,9 +3,10 @@ use crate::operations::Operation; use crate::query::QueryContext; use crate::schema; use crate::selection::Selection; -use failure; +use failure::*; use graphql_parser::query; use proc_macro2::TokenStream; +use quote::*; /// Selects the first operation matching `struct_name`. Returns `None` when the query document defines no operation, or when the selected operation does not match any defined operation. pub(crate) fn select_operation<'query>( @@ -17,7 +18,7 @@ pub(crate) fn select_operation<'query>( operations .iter() .find(|op| op.name == struct_name) - .map(|i| i.to_owned()) + .map(ToOwned::to_owned) } pub(crate) fn all_operations(query: &query::Document) -> Vec> { @@ -147,7 +148,7 @@ pub(crate) fn response_for_query( let response_derives = context.response_derives(); Ok(quote! { - use serde_derive::*; + use serde::{Serialize, Deserialize}; #[allow(dead_code)] type Boolean = bool; diff --git a/graphql_client_codegen/src/codegen_options.rs b/graphql_client_codegen/src/codegen_options.rs index 2bc43c42b..5c31ca28a 100644 --- a/graphql_client_codegen/src/codegen_options.rs +++ b/graphql_client_codegen/src/codegen_options.rs @@ -40,8 +40,6 @@ pub struct GraphQLClientCodegenOptions { impl GraphQLClientCodegenOptions { /// Creates an empty options object with default params. It probably wants to be configured. pub fn new(mode: CodegenMode) -> GraphQLClientCodegenOptions { - use std::default::Default; - GraphQLClientCodegenOptions { mode, additional_derives: Default::default(), diff --git a/graphql_client_codegen/src/enums.rs b/graphql_client_codegen/src/enums.rs index d882b8eb6..d294cc8c0 100644 --- a/graphql_client_codegen/src/enums.rs +++ b/graphql_client_codegen/src/enums.rs @@ -1,4 +1,5 @@ use proc_macro2::{Ident, Span, TokenStream}; +use quote::quote; use std::cell::Cell; pub const ENUMS_PREFIX: &str = ""; diff --git a/graphql_client_codegen/src/field_type.rs b/graphql_client_codegen/src/field_type.rs index 62c4efadd..4d95f9486 100644 --- a/graphql_client_codegen/src/field_type.rs +++ b/graphql_client_codegen/src/field_type.rs @@ -4,6 +4,7 @@ use crate::query::QueryContext; use crate::schema::DEFAULT_SCALARS; use graphql_parser; use proc_macro2::{Ident, Span, TokenStream}; +use quote::quote; #[derive(Clone, Debug, PartialEq, Hash)] pub enum FieldType<'a> { diff --git a/graphql_client_codegen/src/generated_module.rs b/graphql_client_codegen/src/generated_module.rs index 36182d207..5e6e9dce4 100644 --- a/graphql_client_codegen/src/generated_module.rs +++ b/graphql_client_codegen/src/generated_module.rs @@ -1,6 +1,7 @@ use crate::codegen_options::*; use heck::*; use proc_macro2::{Ident, Span, TokenStream}; +use quote::quote; /// This struct contains the parameters necessary to generate code for a given operation. pub(crate) struct GeneratedModule<'a> { @@ -54,31 +55,27 @@ impl<'a> GeneratedModule<'a> { #struct_declaration #module_visibility mod #module_name { - #![allow(non_camel_case_types)] - #![allow(non_snake_case)] #![allow(dead_code)] - use serde; - pub const OPERATION_NAME: &'static str = #operation_name_literal; pub const QUERY: &'static str = #query_string; #query_include #impls + } - impl ::graphql_client::GraphQLQuery for super::#operation_name_ident { - type Variables = Variables; - type ResponseData = ResponseData; - - fn build_query(variables: Self::Variables) -> ::graphql_client::QueryBody { - ::graphql_client::QueryBody { - variables, - query: QUERY, - operation_name: OPERATION_NAME, - } + impl graphql_client::GraphQLQuery for #operation_name_ident { + type Variables = #module_name::Variables; + type ResponseData = #module_name::ResponseData; + fn build_query(variables: Self::Variables) -> ::graphql_client::QueryBody { + graphql_client::QueryBody { + variables, + query: #module_name::QUERY, + operation_name: #module_name::OPERATION_NAME, } + } } )) diff --git a/graphql_client_codegen/src/inputs.rs b/graphql_client_codegen/src/inputs.rs index d99c0f96f..e0e109d19 100644 --- a/graphql_client_codegen/src/inputs.rs +++ b/graphql_client_codegen/src/inputs.rs @@ -7,6 +7,7 @@ use failure; use graphql_parser; use heck::SnakeCase; use proc_macro2::{Ident, Span, TokenStream}; +use quote::quote; use std::cell::Cell; use std::collections::HashMap; @@ -109,7 +110,7 @@ impl<'schema> ::std::convert::From<&'schema graphql_parser::schema::InputObjectT { fn from(schema_input: &'schema graphql_parser::schema::InputObjectType) -> GqlInput<'schema> { GqlInput { - description: schema_input.description.as_ref().map(|s| s.as_str()), + description: schema_input.description.as_ref().map(String::as_str), name: &schema_input.name, fields: schema_input .fields @@ -146,7 +147,7 @@ impl<'schema> ::std::convert::From<&'schema introspection_response::FullType> .as_ref() .expect("fields on input object") .iter() - .filter_map(|a| a.as_ref()) + .filter_map(Option::as_ref) .map(|f| { let name = f .input_value diff --git a/graphql_client_codegen/src/interfaces.rs b/graphql_client_codegen/src/interfaces.rs index ad63ce5c4..e2fe8799d 100644 --- a/graphql_client_codegen/src/interfaces.rs +++ b/graphql_client_codegen/src/interfaces.rs @@ -4,8 +4,9 @@ use crate::query::QueryContext; use crate::selection::{Selection, SelectionField, SelectionFragmentSpread, SelectionItem}; use crate::shared::*; use crate::unions::union_variants; -use failure; +use failure::*; use proc_macro2::{Ident, Span, TokenStream}; +use quote::quote; use std::cell::Cell; use std::collections::HashSet; diff --git a/graphql_client_codegen/src/introspection_response.rs b/graphql_client_codegen/src/introspection_response.rs index fc362acd7..b65272916 100644 --- a/graphql_client_codegen/src/introspection_response.rs +++ b/graphql_client_codegen/src/introspection_response.rs @@ -1,6 +1,6 @@ #![allow(non_camel_case_types)] -use serde; +use serde::*; type Boolean = bool; diff --git a/graphql_client_codegen/src/lib.rs b/graphql_client_codegen/src/lib.rs index f49e0f82b..6b6e35cae 100644 --- a/graphql_client_codegen/src/lib.rs +++ b/graphql_client_codegen/src/lib.rs @@ -1,25 +1,18 @@ #![recursion_limit = "512"] #![deny(missing_docs)] +#![deny(rust_2018_idioms)] +#![deny(warnings)] //! Crate for internal use by other graphql-client crates, for code generation. //! //! It is not meant to be used directly by users of the library. -#[macro_use] -extern crate failure; +use failure::*; use graphql_parser; - -#[macro_use] -extern crate lazy_static; - +use lazy_static::*; use proc_macro2; -#[macro_use] -extern crate serde_derive; - -#[macro_use] -extern crate quote; - use proc_macro2::TokenStream; +use quote::*; mod codegen; mod codegen_options; @@ -100,7 +93,7 @@ pub fn generate_module_token_stream( let schema_extension = schema_path .extension() - .and_then(|e| e.to_str()) + .and_then(std::ffi::OsStr::to_str) .unwrap_or("INVALID"); // Check the schema cache. @@ -175,7 +168,7 @@ fn derive_operation_not_found_error( ) -> failure::Error { use graphql_parser::query::*; - let operation_name = ident.map(|i| i.to_string()); + let operation_name = ident.map(ToString::to_string); let struct_ident = operation_name.as_ref().map(String::as_str).unwrap_or(""); let available_operations = query diff --git a/graphql_client_codegen/src/objects.rs b/graphql_client_codegen/src/objects.rs index 3e93cd786..d6b3288a9 100644 --- a/graphql_client_codegen/src/objects.rs +++ b/graphql_client_codegen/src/objects.rs @@ -8,6 +8,7 @@ use crate::shared::{field_impls_for_selection, response_fields_for_selection}; use failure; use graphql_parser::schema; use proc_macro2::{Ident, Span, TokenStream}; +use quote::quote; use std::cell::Cell; #[derive(Debug, Clone, PartialEq)] @@ -67,7 +68,7 @@ impl<'schema> GqlObject<'schema> { } pub fn from_graphql_parser_object(obj: &'schema schema::ObjectType) -> Self { - let description = obj.description.as_ref().map(|s| s.as_str()); + let description = obj.description.as_ref().map(String::as_str); let mut item = GqlObject::new(&obj.name, description); item.fields.extend(obj.fields.iter().map(|f| { let deprecation = parse_deprecation_info(&f); @@ -84,7 +85,7 @@ impl<'schema> GqlObject<'schema> { pub fn from_introspected_schema_json( obj: &'schema crate::introspection_response::FullType, ) -> Self { - let description = obj.description.as_ref().map(|s| s.as_str()); + let description = obj.description.as_ref().map(String::as_str); let mut item = GqlObject::new(obj.name.as_ref().expect("missing object name"), description); let fields = obj.fields.as_ref().unwrap().iter().filter_map(|t| { t.as_ref().map(|t| { diff --git a/graphql_client_codegen/src/operations.rs b/graphql_client_codegen/src/operations.rs index cb65dd484..23dc477ec 100644 --- a/graphql_client_codegen/src/operations.rs +++ b/graphql_client_codegen/src/operations.rs @@ -5,6 +5,7 @@ use crate::variables::Variable; use graphql_parser::query::OperationDefinition; use heck::SnakeCase; use proc_macro2::{Span, TokenStream}; +use quote::quote; use syn::Ident; #[derive(Debug, Clone)] diff --git a/graphql_client_codegen/src/query.rs b/graphql_client_codegen/src/query.rs index 6b0e7b13c..341d41744 100644 --- a/graphql_client_codegen/src/query.rs +++ b/graphql_client_codegen/src/query.rs @@ -2,9 +2,10 @@ use crate::deprecation::DeprecationStrategy; use crate::fragments::GqlFragment; use crate::schema::Schema; use crate::selection::Selection; -use failure; +use failure::*; use proc_macro2::Span; use proc_macro2::TokenStream; +use quote::quote; use std::collections::{BTreeMap, BTreeSet}; use syn::Ident; @@ -90,20 +91,19 @@ impl<'query, 'schema> QueryContext<'query, 'schema> { self.variables_derives.extend( attribute_value .split(',') - .map(|s| s.trim()) + .map(str::trim) .map(|s| Ident::new(s, Span::call_site())), ); self.response_derives.extend( attribute_value .split(',') - .map(|s| s.trim()) + .map(str::trim) .map(|s| Ident::new(s, Span::call_site())), ); Ok(()) } pub(crate) fn variables_derives(&self) -> TokenStream { - use std::collections::BTreeSet; let derives: BTreeSet<&Ident> = self.variables_derives.iter().collect(); let derives = derives.iter(); @@ -113,7 +113,6 @@ impl<'query, 'schema> QueryContext<'query, 'schema> { } pub(crate) fn response_derives(&self) -> TokenStream { - use std::collections::BTreeSet; let derives: BTreeSet<&Ident> = self.response_derives.iter().collect(); let derives = derives.iter(); diff --git a/graphql_client_codegen/src/scalars.rs b/graphql_client_codegen/src/scalars.rs index 6d082b8e1..8dee829fe 100644 --- a/graphql_client_codegen/src/scalars.rs +++ b/graphql_client_codegen/src/scalars.rs @@ -1,4 +1,4 @@ -use proc_macro2; +use quote::quote; use std::cell::Cell; #[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq)] diff --git a/graphql_client_codegen/src/schema.rs b/graphql_client_codegen/src/schema.rs index dd64aa4c4..540b0a40a 100644 --- a/graphql_client_codegen/src/schema.rs +++ b/graphql_client_codegen/src/schema.rs @@ -6,7 +6,7 @@ use crate::interfaces::GqlInterface; use crate::objects::{GqlObject, GqlObjectField}; use crate::scalars::Scalar; use crate::unions::GqlUnion; -use failure; +use failure::*; use graphql_parser::{self, schema}; use std::collections::{BTreeMap, BTreeSet}; @@ -159,7 +159,7 @@ impl<'schema> ::std::convert::From<&'schema graphql_parser::schema::Document> fo } schema::TypeDefinition::Union(union) => { let variants: BTreeSet<&str> = - union.types.iter().map(|s| s.as_str()).collect(); + union.types.iter().map(String::as_str).collect(); schema.unions.insert( &union.name, GqlUnion { @@ -173,12 +173,12 @@ impl<'schema> ::std::convert::From<&'schema graphql_parser::schema::Document> fo schema::TypeDefinition::Interface(interface) => { let mut iface = GqlInterface::new( &interface.name, - interface.description.as_ref().map(|d| d.as_str()), + interface.description.as_ref().map(String::as_str), ); iface .fields .extend(interface.fields.iter().map(|f| GqlObjectField { - description: f.description.as_ref().map(|s| s.as_str()), + description: f.description.as_ref().map(String::as_str), name: f.name.as_str(), type_: FieldType::from(&f.field_type), deprecation: DeprecationStatus::Current, @@ -192,9 +192,9 @@ impl<'schema> ::std::convert::From<&'schema graphql_parser::schema::Document> fo schema::Definition::DirectiveDefinition(_) => (), schema::Definition::TypeExtension(_extension) => (), schema::Definition::SchemaDefinition(definition) => { - schema.query_type = definition.query.as_ref().map(|s| s.as_str()); - schema.mutation_type = definition.mutation.as_ref().map(|s| s.as_str()); - schema.subscription_type = definition.subscription.as_ref().map(|s| s.as_str()); + schema.query_type = definition.query.as_ref().map(String::as_str); + schema.mutation_type = definition.mutation.as_ref().map(String::as_str); + schema.subscription_type = definition.subscription.as_ref().map(String::as_str); } } } @@ -224,17 +224,17 @@ impl<'schema> ::std::convert::From<&'schema crate::introspection_response::Intro .query_type .as_ref() .and_then(|ty| ty.name.as_ref()) - .map(|s| s.as_str()); + .map(String::as_str); schema.mutation_type = root .mutation_type .as_ref() .and_then(|ty| ty.name.as_ref()) - .map(|s| s.as_str()); + .map(String::as_str); schema.subscription_type = root .subscription_type .as_ref() .and_then(|ty| ty.name.as_ref()) - .map(|s| s.as_str()); + .map(String::as_str); // Holds which objects implement which interfaces so we can populate GqlInterface#implemented_by later. // It maps interface names to a vec of implementation names. @@ -274,7 +274,7 @@ impl<'schema> ::std::convert::From<&'schema crate::introspection_response::Intro .collect(); let enm = GqlEnum { name, - description: ty.description.as_ref().map(|s| s.as_str()), + description: ty.description.as_ref().map(String::as_str), variants, is_required: false.into(), }; @@ -317,10 +317,10 @@ impl<'schema> ::std::convert::From<&'schema crate::introspection_response::Intro for implementing in ty .interfaces .as_ref() - .map(|s| s.as_slice()) + .map(Vec::as_slice) .unwrap_or_else(|| &[]) .iter() - .filter_map(|t| t.as_ref()) + .filter_map(Option::as_ref) .map(|t| &t.type_ref.name) { interface_implementations @@ -340,15 +340,15 @@ impl<'schema> ::std::convert::From<&'schema crate::introspection_response::Intro } Some(__TypeKind::INTERFACE) => { let mut iface = - GqlInterface::new(name, ty.description.as_ref().map(|t| t.as_str())); + GqlInterface::new(name, ty.description.as_ref().map(String::as_str)); iface.fields.extend( ty.fields .as_ref() .expect("interface fields") .iter() - .filter_map(|f| f.as_ref()) + .filter_map(Option::as_ref) .map(|f| GqlObjectField { - description: f.description.as_ref().map(|s| s.as_str()), + description: f.description.as_ref().map(String::as_str), name: f.name.as_ref().expect("field name").as_str(), type_: FieldType::from(f.type_.as_ref().expect("field type")), deprecation: DeprecationStatus::Current, diff --git a/graphql_client_codegen/src/selection.rs b/graphql_client_codegen/src/selection.rs index da73ea8b1..c2417ec0d 100644 --- a/graphql_client_codegen/src/selection.rs +++ b/graphql_client_codegen/src/selection.rs @@ -1,4 +1,5 @@ use crate::constants::*; +use failure::*; use graphql_parser::query::SelectionSet; use std::collections::BTreeMap; @@ -51,7 +52,7 @@ impl<'query> Selection<'query> { context: &'context crate::query::QueryContext<'_, '_>, ) -> Option<&SelectionField<'_>> { // __typename is selected directly - if let Some(field) = self.0.iter().filter_map(|f| f.as_typename()).next() { + if let Some(field) = self.0.iter().filter_map(SelectionItem::as_typename).next() { return Some(field); }; @@ -177,7 +178,7 @@ impl<'query> ::std::convert::From<&'query SelectionSet> for Selection<'query> { for item in &selection_set.items { let converted = match item { Selection::Field(f) => SelectionItem::Field(SelectionField { - alias: f.alias.as_ref().map(|s| s.as_str()), + alias: f.alias.as_ref().map(String::as_str), name: &f.name, fields: (&f.selection_set).into(), }), diff --git a/graphql_client_codegen/src/shared.rs b/graphql_client_codegen/src/shared.rs index cd5daea2e..6641c4728 100644 --- a/graphql_client_codegen/src/shared.rs +++ b/graphql_client_codegen/src/shared.rs @@ -2,9 +2,10 @@ use crate::deprecation::{DeprecationStatus, DeprecationStrategy}; use crate::objects::GqlObjectField; use crate::query::QueryContext; use crate::selection::*; -use failure; +use failure::*; use heck::{CamelCase, SnakeCase}; use proc_macro2::{Ident, Span, TokenStream}; +use quote::quote; pub(crate) fn render_object_field( field_name: &str, diff --git a/graphql_client_codegen/src/unions.rs b/graphql_client_codegen/src/unions.rs index e41cebd18..58adf1c70 100644 --- a/graphql_client_codegen/src/unions.rs +++ b/graphql_client_codegen/src/unions.rs @@ -1,7 +1,8 @@ use crate::query::QueryContext; use crate::selection::Selection; -use failure; +use failure::*; use proc_macro2::{Ident, Span, TokenStream}; +use quote::quote; use std::cell::Cell; use std::collections::BTreeSet; diff --git a/graphql_client_codegen/src/variables.rs b/graphql_client_codegen/src/variables.rs index 66d134132..ba5b61dc1 100644 --- a/graphql_client_codegen/src/variables.rs +++ b/graphql_client_codegen/src/variables.rs @@ -2,6 +2,7 @@ use crate::field_type::FieldType; use crate::query::QueryContext; use graphql_parser; use proc_macro2::{Ident, Span, TokenStream}; +use quote::quote; use std::collections::BTreeMap; #[derive(Debug, Clone)] diff --git a/graphql_client_web/Cargo.toml b/graphql_client_web/Cargo.toml index 0db087232..1419e3cae 100644 --- a/graphql_client_web/Cargo.toml +++ b/graphql_client_web/Cargo.toml @@ -33,6 +33,5 @@ features = [ ] [dev-dependencies] -serde = "1" -serde_derive = "1" +serde = { version = "1", features = ["derive"] } wasm-bindgen-test = "0.2.25" diff --git a/graphql_client_web/src/lib.rs b/graphql_client_web/src/lib.rs index a42de740e..603d437da 100644 --- a/graphql_client_web/src/lib.rs +++ b/graphql_client_web/src/lib.rs @@ -4,6 +4,7 @@ #![deny(warnings)] #![deny(missing_docs)] +#![deny(rust_2018_idioms)] use failure::*; use futures::{Future, IntoFuture}; diff --git a/graphql_client_web/tests/web.rs b/graphql_client_web/tests/web.rs index 37a915df9..d767528e7 100644 --- a/graphql_client_web/tests/web.rs +++ b/graphql_client_web/tests/web.rs @@ -1,15 +1,9 @@ -#[macro_use] -extern crate graphql_client_web; - -#[macro_use] -extern crate serde_derive; - use futures::Future; use graphql_client_web::Client; +use graphql_client_web::*; use wasm_bindgen::JsValue; -use wasm_bindgen_test::*; - use wasm_bindgen_test::wasm_bindgen_test_configure; +use wasm_bindgen_test::*; wasm_bindgen_test_configure!(run_in_browser); diff --git a/graphql_query_derive/src/attributes.rs b/graphql_query_derive/src/attributes.rs index bb49c004a..91a8b38ee 100644 --- a/graphql_query_derive/src/attributes.rs +++ b/graphql_query_derive/src/attributes.rs @@ -1,4 +1,4 @@ -use failure; +use failure::*; use graphql_client_codegen::deprecation::DeprecationStrategy; use syn; diff --git a/graphql_query_derive/src/lib.rs b/graphql_query_derive/src/lib.rs index 0f885c290..1aadae7c2 100644 --- a/graphql_query_derive/src/lib.rs +++ b/graphql_query_derive/src/lib.rs @@ -1,10 +1,5 @@ -#[macro_use] -extern crate failure; - extern crate proc_macro; -use syn; - /// Derive-related code. This will be moved into graphql_query_derive. mod attributes; @@ -17,7 +12,7 @@ use std::path::{Path, PathBuf}; use proc_macro2::TokenStream; #[proc_macro_derive(GraphQLQuery, attributes(graphql))] -pub fn graphql_query_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { +pub fn derive_graphql_query(input: proc_macro::TokenStream) -> proc_macro::TokenStream { match graphql_query_derive_inner(input) { Ok(ts) => ts, Err(err) => panic!( @@ -41,7 +36,7 @@ fn graphql_query_derive_inner( let options = build_graphql_client_derive_options(&ast, query_path.to_path_buf())?; Ok( generate_module_token_stream(query_path, &schema_path, options) - .map(|module| module.into()) + .map(Into::into) .context("Code generation failed.")?, ) }