From 06a5924257ca665882aeb27eac555b3b40497a09 Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Tue, 19 Sep 2023 12:01:24 +0530 Subject: [PATCH] style(*): clean-up imports --- .rustfmt.toml | 5 ++++- assets/jsonplaceholder.graphql | 4 +++- lint.sh | 2 +- src/blueprint/blueprint.rs | 10 ++++------ src/blueprint/from_config.rs | 16 +++++----------- src/blueprint/into_schema.rs | 9 ++++----- src/blueprint/mod.rs | 1 - src/blueprint/timeout.rs | 10 ++++------ src/cli/error.rs | 3 ++- src/cli/fmt.rs | 4 ++-- src/cli/tc.rs | 6 +++--- src/config/config.rs | 5 ++--- src/config/from_document.rs | 3 +-- src/config/into_document.rs | 3 +-- src/directive.rs | 3 +-- src/endpoint.rs | 14 ++++++-------- src/http/client.rs | 5 +---- src/http/data_loader.rs | 14 ++++---------- src/http/memo_client.rs | 6 ++++-- src/http/mod.rs | 1 - src/http/request_context.rs | 7 ++++--- src/http/response.rs | 4 +--- src/http/scheme.rs | 3 ++- src/http/server.rs | 3 --- src/http/server_context.rs | 4 +++- src/inet_address.rs | 3 ++- src/json/json_like.rs | 3 ++- src/json/json_schema.rs | 11 +++++++---- src/lambda/expression.rs | 3 +-- src/lambda/lambda.rs | 10 +++------- src/path.rs | 3 ++- src/print_schema.rs | 3 +-- src/valid/cause.rs | 3 ++- src/valid/error.rs | 4 ++-- src/valid/valid.rs | 8 ++------ tests/graphql_mock/mod.rs | 3 ++- tests/graphql_spec.rs | 7 ++----- 37 files changed, 90 insertions(+), 116 deletions(-) diff --git a/.rustfmt.toml b/.rustfmt.toml index 1dcadac3c1..4bab087d20 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -1,3 +1,6 @@ +unstable_features = true max_width = 120 struct_lit_width = 120 -tab_spaces = 2 \ No newline at end of file +tab_spaces = 2 +imports_granularity = "Module" +group_imports = "StdExternalCrate" diff --git a/assets/jsonplaceholder.graphql b/assets/jsonplaceholder.graphql index 6906c98b7d..ea371e8c7f 100644 --- a/assets/jsonplaceholder.graphql +++ b/assets/jsonplaceholder.graphql @@ -27,5 +27,7 @@ type Post { userId: Int! title: String! body: String! - user: User @http(path: "/users/{{value.userId}}") + user: User + @http(path: "/users", query: {id: "{{parent.value.userId}}"}) + @batch(key: "userId", path: ["id"]) } diff --git a/lint.sh b/lint.sh index 6eeb6431db..495fa915ae 100755 --- a/lint.sh +++ b/lint.sh @@ -1,2 +1,2 @@ -cargo fmt +cargo +nightly fmt cargo clippy --all-targets --all-features --fix --allow-staged --allow-dirty diff --git a/src/blueprint/blueprint.rs b/src/blueprint/blueprint.rs index f7d3989ada..24cf3b2a8a 100644 --- a/src/blueprint/blueprint.rs +++ b/src/blueprint/blueprint.rs @@ -1,16 +1,14 @@ use std::collections::HashMap; use async_graphql::dynamic::{Schema, SchemaBuilder}; +use async_graphql::extensions::ApolloTracing; +use async_graphql::*; use derive_setters::Setters; use serde_json::Value; -use crate::config; - use super::GlobalTimeout; -use crate::lambda::Expression; -use crate::lambda::Lambda; -use async_graphql::extensions::ApolloTracing; -use async_graphql::*; +use crate::config; +use crate::lambda::{Expression, Lambda}; /// Blueprint is an intermediary representation that allows us to generate graphQL APIs. /// It can only be generated from a valid Config. diff --git a/src/blueprint/from_config.rs b/src/blueprint/from_config.rs index 25fb60b33c..ecff3d8d1a 100644 --- a/src/blueprint/from_config.rs +++ b/src/blueprint/from_config.rs @@ -7,24 +7,18 @@ use async_graphql::parser::types::ConstDirective; use async_graphql::InputType; use regex::Regex; -use crate::blueprint; +use super::UnionTypeDefinition; use crate::blueprint::Type::ListType; use crate::blueprint::*; -use crate::config; -use crate::config::{Arg, Field}; -use crate::config::{Config, InlineType}; +use crate::config::{Arg, Config, Field, InlineType}; use crate::directive::DirectiveCodec; use crate::endpoint::Endpoint; -use crate::http::Method; -use crate::http::Scheme; +use crate::http::{Method, Scheme}; use crate::inet_address::InetAddress; use crate::json::JsonSchema; use crate::lambda::Lambda; -use crate::valid::OptionExtension; -use crate::valid::Valid as ValidDefault; -use crate::valid::{ValidExtensions, ValidationError, VectorExtension}; - -use super::UnionTypeDefinition; +use crate::valid::{OptionExtension, Valid as ValidDefault, ValidExtensions, ValidationError, VectorExtension}; +use crate::{blueprint, config}; type Valid = ValidDefault; diff --git a/src/blueprint/into_schema.rs b/src/blueprint/into_schema.rs index ec2e409f73..dcdb214b82 100644 --- a/src/blueprint/into_schema.rs +++ b/src/blueprint/into_schema.rs @@ -1,12 +1,11 @@ use std::borrow::Cow; - use std::sync::Arc; -use async_graphql::dynamic::{self, FieldFuture, SchemaBuilder}; - -use crate::blueprint::Definition; -use crate::blueprint::{Blueprint, Type}; +use async_graphql::dynamic::{ + FieldFuture, SchemaBuilder, {self}, +}; +use crate::blueprint::{Blueprint, Definition, Type}; use crate::http::RequestContext; use crate::lambda::EvaluationContext; diff --git a/src/blueprint/mod.rs b/src/blueprint/mod.rs index 14a6cd3202..a43d289fc2 100644 --- a/src/blueprint/mod.rs +++ b/src/blueprint/mod.rs @@ -5,5 +5,4 @@ mod into_schema; mod timeout; pub use blueprint::*; - pub use timeout::GlobalTimeout; diff --git a/src/blueprint/timeout.rs b/src/blueprint/timeout.rs index 58c5d370ef..4e18a47e72 100644 --- a/src/blueprint/timeout.rs +++ b/src/blueprint/timeout.rs @@ -1,11 +1,9 @@ -use async_graphql::ServerError; -use async_graphql::{ - extensions::{Extension, ExtensionContext, ExtensionFactory, NextExecute}, - Response, -}; -use async_graphql_value::ConstValue; use std::sync::Arc; use std::time::Duration; + +use async_graphql::extensions::{Extension, ExtensionContext, ExtensionFactory, NextExecute}; +use async_graphql::{Response, ServerError}; +use async_graphql_value::ConstValue; use tokio::time::timeout; pub struct GlobalTimeout; diff --git a/src/cli/error.rs b/src/cli/error.rs index aadc5afe3b..0cf42023d6 100644 --- a/src/cli/error.rs +++ b/src/cli/error.rs @@ -185,10 +185,11 @@ impl From> for CLIError { #[cfg(test)] mod tests { - use super::*; use pretty_assertions::assert_eq; use stripmargin::StripMargin; + use super::*; + #[test] fn test_no_newline() { let input = "Hello"; diff --git a/src/cli/fmt.rs b/src/cli/fmt.rs index 86e7273cd3..47d8820ac3 100644 --- a/src/cli/fmt.rs +++ b/src/cli/fmt.rs @@ -1,7 +1,7 @@ -use crate::config::Config; - use colored::*; +use crate::config::Config; + pub struct Fmt {} impl Fmt { diff --git a/src/cli/tc.rs b/src/cli/tc.rs index 295fae1e92..a2478e9925 100644 --- a/src/cli/tc.rs +++ b/src/cli/tc.rs @@ -5,12 +5,12 @@ use std::fs; use anyhow::Result; use clap::Parser; +use super::command::{Cli, Command}; +use crate::blueprint::Blueprint; use crate::cli::fmt::Fmt; +use crate::config::Config; use crate::http::start_server; use crate::print_schema; -use crate::{blueprint::Blueprint, config::Config}; - -use super::command::{Cli, Command}; pub async fn run() -> Result<()> { let cli = Cli::parse(); diff --git a/src/config/config.rs b/src/config/config.rs index 919e6e478b..a853edfb14 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -1,18 +1,17 @@ use std::collections::{BTreeMap, HashSet}; +use anyhow::Result; use async_graphql::parser::types::ServiceDocument; use derive_setters::Setters; use serde::{Deserialize, Serialize}; use serde_json::Value; +use super::{Proxy, Server}; use crate::batch::Batch; use crate::http::Method; use crate::json::JsonSchema; use crate::mustache::Mustache; use crate::path::{path_deserialize, path_serialize, Path}; -use anyhow::Result; - -use super::{Proxy, Server}; #[derive(Serialize, Deserialize, Clone, Debug, Default, Setters)] #[serde(rename_all = "camelCase")] diff --git a/src/config/from_document.rs b/src/config/from_document.rs index ecfda154d6..e3c16a2161 100644 --- a/src/config/from_document.rs +++ b/src/config/from_document.rs @@ -11,8 +11,7 @@ use async_graphql::Name; use crate::batch::Batch; use crate::config; -use crate::config::Config; -use crate::config::{GraphQL, RootSchema, Server, Union}; +use crate::config::{Config, GraphQL, RootSchema, Server, Union}; use crate::directive::DirectiveCodec; fn from_document(doc: ServiceDocument) -> Config { diff --git a/src/config/into_document.rs b/src/config/into_document.rs index 647e0d19ec..1f40609d94 100644 --- a/src/config/into_document.rs +++ b/src/config/into_document.rs @@ -2,9 +2,8 @@ use async_graphql::parser::types::*; use async_graphql::{Pos, Positioned}; use async_graphql_value::{ConstValue, Name}; -use crate::directive::DirectiveCodec; - use super::Config; +use crate::directive::DirectiveCodec; fn pos(a: A) -> Positioned { Positioned::new(a, Pos::default()) diff --git a/src/directive.rs b/src/directive.rs index 4a2074569a..8d754bf220 100644 --- a/src/directive.rs +++ b/src/directive.rs @@ -1,11 +1,10 @@ +use anyhow::Result; use async_graphql::parser::types::ConstDirective; use async_graphql::{Name, Pos, Positioned}; use serde::{Deserialize, Serialize}; use serde_json::{Map, Value}; use serde_path_to_error::deserialize; -use anyhow::Result; - use crate::valid::ValidationError; fn pos(a: A) -> Positioned { diff --git a/src/endpoint.rs b/src/endpoint.rs index 99ed4777ec..db56545018 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -1,21 +1,19 @@ #![allow(clippy::too_many_arguments)] -use serde_json::json; use std::collections::BTreeMap; use std::sync::Arc; + +use anyhow::Result; +use derive_setters::Setters; +use serde_json::json; use url::Url; use crate::batch::Batch; -use crate::http::Method; -use crate::http::Scheme; +use crate::http::{Method, Scheme}; use crate::inet_address::InetAddress; -use crate::json::JsonLike; -use crate::json::JsonSchema; +use crate::json::{JsonLike, JsonSchema}; use crate::mustache::Mustache; use crate::path::{Path, Segment}; -use anyhow::Result; - -use derive_setters::Setters; const EMPTY_VEC: &Vec = &vec![]; diff --git a/src/http/client.rs b/src/http/client.rs index dd783335d4..a1b5236d3c 100644 --- a/src/http/client.rs +++ b/src/http/client.rs @@ -1,14 +1,11 @@ use std::time::Duration; use http_cache_reqwest::{Cache, CacheMode, HttpCache, HttpCacheOptions, MokaManager}; - use reqwest::Client; - use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; -use crate::config::Server; - use super::Response; +use crate::config::Server; #[derive(Clone)] pub struct HttpClient { diff --git a/src/http/data_loader.rs b/src/http/data_loader.rs index 09dbc8d663..6d61d64a5e 100644 --- a/src/http/data_loader.rs +++ b/src/http/data_loader.rs @@ -1,24 +1,18 @@ -use std::collections::BTreeMap; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; +use std::hash::{Hash, Hasher}; use std::sync::Arc; use std::time::Duration; +use anyhow::Result; use async_graphql::async_trait; use async_graphql::dataloader::{DataLoader, HashMapCache, Loader}; use async_graphql::futures_util::future::join_all; use async_graphql_value::ConstValue; use derive_setters::Setters; - use url::Url; -use crate::http::Method; -use crate::http::Response; - -use crate::http::HttpClient; +use crate::http::{HttpClient, Method, Response}; use crate::json::JsonLike; -use std::hash::{Hash, Hasher}; - -use anyhow::Result; #[derive(Clone, Debug, Eq, PartialEq)] pub struct EndpointKey { pub url: Url, diff --git a/src/http/memo_client.rs b/src/http/memo_client.rs index ee529155b1..90ffdc1c8a 100644 --- a/src/http/memo_client.rs +++ b/src/http/memo_client.rs @@ -1,11 +1,13 @@ -use std::{collections::HashMap, sync::Mutex}; +use std::collections::HashMap; +use std::sync::Mutex; -use super::HttpClient; use anyhow::Result; use http_cache_semantics::RequestLike; use hyper::Uri; use reqwest::Method; +use super::HttpClient; + // TODO: drop MemoClient #[allow(dead_code)] pub struct MemoClient { diff --git a/src/http/mod.rs b/src/http/mod.rs index 786d2123b3..1a6590f283 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -14,7 +14,6 @@ use std::time::Duration; use cache_control::CacheControl; pub use client::*; pub use data_loader::*; - use hyper::header::CACHE_CONTROL; pub use method::Method; pub use request_context::RequestContext; diff --git a/src/http/request_context.rs b/src/http/request_context.rs index 6145fe6013..a21b62630e 100644 --- a/src/http/request_context.rs +++ b/src/http/request_context.rs @@ -1,13 +1,14 @@ -use std::{collections::HashMap, sync::Mutex}; +use std::collections::HashMap; +use std::sync::Mutex; use async_graphql::dataloader::{DataLoader, HashMapCache}; use derive_setters::Setters; use hyper::{HeaderMap, Uri}; +use super::memo_client::MemoClient; +use super::{EndpointKey, HttpClient, HttpDataLoader, Response, ServerContext}; use crate::config::Server; -use super::{memo_client::MemoClient, EndpointKey, HttpClient, HttpDataLoader, Response, ServerContext}; - #[derive(Setters)] pub struct RequestContext { pub data_loader: DataLoader, diff --git a/src/http/response.rs b/src/http/response.rs index e0d819774b..d4a09dac06 100644 --- a/src/http/response.rs +++ b/src/http/response.rs @@ -1,9 +1,7 @@ +use anyhow::Result; use derive_setters::Setters; - use http_cache_semantics::ResponseLike; -use anyhow::Result; - #[derive(Clone, Debug, Default, Setters)] pub struct Response { pub status: reqwest::StatusCode, diff --git a/src/http/scheme.rs b/src/http/scheme.rs index 5acce7231d..18d9b28692 100644 --- a/src/http/scheme.rs +++ b/src/http/scheme.rs @@ -1,5 +1,6 @@ -use serde::{Deserialize, Serialize}; use std::fmt::Display; + +use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub enum Scheme { Http, diff --git a/src/http/server.rs b/src/http/server.rs index ac15b8dc35..fc2547ab80 100644 --- a/src/http/server.rs +++ b/src/http/server.rs @@ -3,9 +3,7 @@ use std::fs; use std::sync::Arc; use anyhow::Result; - use async_graphql::http::GraphiQLSource; - use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, HeaderMap, Request, Response, StatusCode}; @@ -13,7 +11,6 @@ use super::request_context::RequestContext; use super::ServerContext; use crate::async_graphql_hyper; use crate::blueprint::Blueprint; - use crate::cli::CLIError; use crate::config::Config; diff --git a/src/http/server_context.rs b/src/http/server_context.rs index eff80517eb..ccabe06784 100644 --- a/src/http/server_context.rs +++ b/src/http/server_context.rs @@ -1,7 +1,9 @@ use async_graphql::dynamic; use derive_setters::Setters; -use crate::{blueprint::Blueprint, config::Server, http::HttpClient}; +use crate::blueprint::Blueprint; +use crate::config::Server; +use crate::http::HttpClient; #[derive(Clone, Setters)] pub struct ServerContext { diff --git a/src/inet_address.rs b/src/inet_address.rs index 409eb34af3..c930337f9d 100644 --- a/src/inet_address.rs +++ b/src/inet_address.rs @@ -1,6 +1,7 @@ +use std::fmt::Display; + use derive_setters::Setters; use serde::{Deserialize, Serialize}; -use std::fmt::Display; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Setters)] pub struct InetAddress { pub host: String, diff --git a/src/json/json_like.rs b/src/json/json_like.rs index c63652cf87..0b51729f7e 100644 --- a/src/json/json_like.rs +++ b/src/json/json_like.rs @@ -236,7 +236,8 @@ mod tests { use pretty_assertions::assert_eq; use serde_json::json; - use crate::json::{group_by_key, json_like::gather_path_matches}; + use crate::json::group_by_key; + use crate::json::json_like::gather_path_matches; #[test] fn test_gather_path_matches() { diff --git a/src/json/json_schema.rs b/src/json/json_schema.rs index 2e81227f6d..69bc9eae36 100644 --- a/src/json/json_schema.rs +++ b/src/json/json_schema.rs @@ -1,7 +1,9 @@ -use crate::valid::{Valid, ValidExtensions, VectorExtension}; +use std::collections::HashMap; + use async_graphql::Name; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; + +use crate::valid::{Valid, ValidExtensions, VectorExtension}; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[serde(rename = "schema")] @@ -86,11 +88,12 @@ impl JsonSchema { #[cfg(test)] mod tests { - use crate::valid::Valid; - use crate::{json::JsonSchema, valid::ValidExtensions}; use async_graphql::Name; use indexmap::IndexMap; + use crate::json::JsonSchema; + use crate::valid::{Valid, ValidExtensions}; + #[test] fn test_validate_string() { let schema = JsonSchema::Str; diff --git a/src/lambda/expression.rs b/src/lambda/expression.rs index 11597fd4ae..ad8d11145b 100644 --- a/src/lambda/expression.rs +++ b/src/lambda/expression.rs @@ -8,8 +8,7 @@ use serde_json::Value; use thiserror::Error; use crate::endpoint::Endpoint; -use crate::http::EndpointKey; -use crate::http::Method; +use crate::http::{EndpointKey, Method}; #[cfg(feature = "unsafe-js")] use crate::javascript; use crate::json::JsonLike; diff --git a/src/lambda/lambda.rs b/src/lambda/lambda.rs index 2974246ab3..18b38da5a2 100644 --- a/src/lambda/lambda.rs +++ b/src/lambda/lambda.rs @@ -1,11 +1,9 @@ use std::marker::PhantomData; use super::expression; -use super::expression::Operation; +use super::expression::{Context, Expression, Operation}; use crate::endpoint::Endpoint; -use super::expression::{Context, Expression}; - #[derive(Clone, Debug)] pub struct Lambda { _output: PhantomData A>, @@ -64,19 +62,17 @@ where #[cfg(test)] mod tests { + use anyhow::Result; use httpmock::Method::GET; use httpmock::MockServer; use serde::de::DeserializeOwned; use serde_json::json; use crate::endpoint::Endpoint; - use crate::http::RequestContext; use crate::inet_address::InetAddress; - use crate::lambda::EvaluationContext; - use crate::lambda::Lambda; + use crate::lambda::{EvaluationContext, Lambda}; use crate::path::{Path, Segment}; - use anyhow::Result; impl Lambda where diff --git a/src/path.rs b/src/path.rs index ef4394899c..716fc8d98b 100644 --- a/src/path.rs +++ b/src/path.rs @@ -1,7 +1,8 @@ +use std::fmt::{Display, Formatter}; + use lazy_static::lazy_static; use regex::Regex; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use std::fmt::{Display, Formatter}; lazy_static! { pub static ref RE: Regex = Regex::new(r"\{\{(?P[\w\.]+)\}\}").unwrap(); diff --git a/src/print_schema.rs b/src/print_schema.rs index 35e5a373ec..8f92f1ddda 100644 --- a/src/print_schema.rs +++ b/src/print_schema.rs @@ -1,6 +1,5 @@ -use async_graphql::SDLExportOptions; - use async_graphql::dynamic::Schema; +use async_graphql::SDLExportOptions; /// SDL returned from AsyncSchemaInner isn't standard /// We clean it up before returning. diff --git a/src/valid/cause.rs b/src/valid/cause.rs index f9e848e232..aa5474c8f8 100644 --- a/src/valid/cause.rs +++ b/src/valid/cause.rs @@ -1,4 +1,5 @@ -use std::{collections::VecDeque, fmt::Display}; +use std::collections::VecDeque; +use std::fmt::Display; use derive_setters::Setters; use thiserror::Error; diff --git a/src/valid/error.rs b/src/valid/error.rs index 02b150ee2f..28f5e71776 100644 --- a/src/valid/error.rs +++ b/src/valid/error.rs @@ -112,10 +112,10 @@ impl From> for ValidationError = Result>; @@ -96,10 +95,7 @@ impl OptionExtension for Option { #[cfg(test)] mod tests { - use crate::{ - valid::Cause, - valid::{OptionExtension, Valid, ValidExtensions, VectorExtension}, - }; + use crate::valid::{Cause, OptionExtension, Valid, ValidExtensions, VectorExtension}; #[test] fn test_ok() { diff --git a/tests/graphql_mock/mod.rs b/tests/graphql_mock/mod.rs index d0089483eb..86a7491699 100644 --- a/tests/graphql_mock/mod.rs +++ b/tests/graphql_mock/mod.rs @@ -1,6 +1,7 @@ +use std::fs; + use regex::Regex; use serde_json::Value; -use std::fs; pub fn start_mock_server() -> mockito::Server { mockito::Server::new_with_port(3000) diff --git a/tests/graphql_spec.rs b/tests/graphql_spec.rs index 488bcd724f..0310096eb3 100644 --- a/tests/graphql_spec.rs +++ b/tests/graphql_spec.rs @@ -8,20 +8,17 @@ use std::sync::Arc; use async_graphql::parser::types::TypeSystemDefinition; use async_graphql::Request; use derive_setters::Setters; +use pretty_assertions::assert_eq; use regex::Regex; use serde::{Deserialize, Serialize}; use serde_json::Value; - use tailcall::blueprint::Blueprint; use tailcall::config::Config; use tailcall::directive::DirectiveCodec; +use tailcall::http::{HttpDataLoader, RequestContext}; use tailcall::print_schema; use tailcall::valid::Cause; -use tailcall::http::{HttpDataLoader, RequestContext}; - -use pretty_assertions::assert_eq; - mod graphql_mock; #[derive(Debug, Default, Setters)]