From 4429fced3b7b088ca29b82c065b6169a34992998 Mon Sep 17 00:00:00 2001 From: Dominic Date: Sun, 13 Oct 2019 23:36:10 +0200 Subject: [PATCH] apply clippy suggestions --- gotham_restful/src/openapi/router.rs | 35 ++++++++++---------- gotham_restful/src/openapi/types.rs | 39 +++++++++++------------ gotham_restful/src/result.rs | 23 ++++++------- gotham_restful/src/routing.rs | 1 + gotham_restful_derive/src/lib.rs | 24 +++++--------- gotham_restful_derive/src/method.rs | 12 ++----- gotham_restful_derive/src/openapi_type.rs | 6 ++-- 7 files changed, 62 insertions(+), 78 deletions(-) diff --git a/gotham_restful/src/openapi/router.rs b/gotham_restful/src/openapi/router.rs index 5413838..86d0cd5 100644 --- a/gotham_restful/src/openapi/router.rs +++ b/gotham_restful/src/openapi/router.rs @@ -60,11 +60,10 @@ impl OpenapiRouter /// modify the path and add it back after the modification fn remove_path(&mut self, path : &str) -> PathItem { - if let Some(Item(item)) = self.0.paths.swap_remove(path) - { - return item; + match self.0.paths.swap_remove(path) { + Some(Item(item)) => item, + _ => PathItem::default() } - return PathItem::default() } fn add_path(&mut self, path : Path, item : PathItem) @@ -78,11 +77,11 @@ impl OpenapiRouter match &mut self.0.components { Some(comp) => { - comp.schemas.insert(name, Item(schema.to_schema())); + comp.schemas.insert(name, Item(schema.into_schema())); }, None => { let mut comp = Components::default(); - comp.schemas.insert(name, Item(schema.to_schema())); + comp.schemas.insert(name, Item(schema.into_schema())); self.0.components = Some(comp); } }; @@ -103,17 +102,17 @@ impl OpenapiRouter fn add_schema(&mut self) -> ReferenceOr { - let mut schema = T::to_schema(); - if let Some(name) = schema.name.clone() - { - let reference = Reference { reference: format!("#/components/schemas/{}", name) }; - self.add_schema_impl(name, schema); - reference - } - else - { - self.add_schema_dependencies(&mut schema.dependencies); - Item(schema.to_schema()) + let mut schema = T::schema(); + match schema.name.clone() { + Some(name) => { + let reference = Reference { reference: format!("#/components/schemas/{}", name) }; + self.add_schema_impl(name, schema); + reference + }, + None => { + self.add_schema_dependencies(&mut schema.dependencies); + Item(schema.into_schema()) + } } } } @@ -211,7 +210,7 @@ impl<'a> OperationParams<'a> description: None, required: true, deprecated: None, - format: ParameterSchemaOrContent::Schema(Item(String::to_schema().to_schema())), + format: ParameterSchemaOrContent::Schema(Item(String::schema().into_schema())), example: None, examples: IndexMap::new() }, diff --git a/gotham_restful/src/openapi/types.rs b/gotham_restful/src/openapi/types.rs index e380454..fc321ea 100644 --- a/gotham_restful/src/openapi/types.rs +++ b/gotham_restful/src/openapi/types.rs @@ -32,7 +32,7 @@ impl OpenapiSchema } } - pub fn to_schema(self) -> Schema + pub fn into_schema(self) -> Schema { Schema { schema_data: SchemaData { @@ -54,12 +54,12 @@ impl OpenapiSchema pub trait OpenapiType { - fn to_schema() -> OpenapiSchema; + fn schema() -> OpenapiSchema; } impl OpenapiType for () { - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { OpenapiSchema::new(SchemaKind::Type(Type::Object(ObjectType::default()))) } @@ -67,7 +67,7 @@ impl OpenapiType for () impl OpenapiType for bool { - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { OpenapiSchema::new(SchemaKind::Type(Type::Boolean{})) } @@ -77,7 +77,7 @@ macro_rules! int_types { ($($int_ty:ty),*) => {$( impl OpenapiType for $int_ty { - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { OpenapiSchema::new(SchemaKind::Type(Type::Integer(IntegerType::default()))) } @@ -91,7 +91,7 @@ macro_rules! num_types { ($($num_ty:ty),*) => {$( impl OpenapiType for $num_ty { - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { OpenapiSchema::new(SchemaKind::Type(Type::Number(NumberType::default()))) } @@ -105,7 +105,7 @@ macro_rules! str_types { ($($str_ty:ty),*) => {$( impl OpenapiType for $str_ty { - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { OpenapiSchema::new(SchemaKind::Type(Type::String(StringType::default()))) } @@ -131,9 +131,9 @@ str_types!(String, &str); impl OpenapiType for Option { - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { - let schema = T::to_schema(); + let schema = T::schema(); let mut dependencies = schema.dependencies.clone(); let schema = match schema.name.clone() { Some(name) => { @@ -155,20 +155,19 @@ impl OpenapiType for Option impl OpenapiType for Vec { - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { - let schema = T::to_schema(); + let schema = T::schema(); let mut dependencies = schema.dependencies.clone(); - - let items = if let Some(name) = schema.name.clone() + + let items = match schema.name.clone() { - let reference = Reference { reference: format!("#/components/schemas/{}", name) }; - dependencies.insert(name, schema); - reference - } - else - { - Item(Box::new(schema.to_schema())) + Some(name) => { + let reference = Reference { reference: format!("#/components/schemas/{}", name) }; + dependencies.insert(name, schema); + reference + }, + None => Item(Box::new(schema.into_schema())) }; OpenapiSchema { diff --git a/gotham_restful/src/result.rs b/gotham_restful/src/result.rs index b0f8331..d0b6229 100644 --- a/gotham_restful/src/result.rs +++ b/gotham_restful/src/result.rs @@ -11,7 +11,7 @@ pub trait ResourceResult fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>; #[cfg(feature = "openapi")] - fn to_schema() -> OpenapiSchema; + fn schema() -> OpenapiSchema; #[cfg(feature = "openapi")] fn default_status() -> StatusCode @@ -23,9 +23,9 @@ pub trait ResourceResult #[cfg(feature = "openapi")] impl crate::OpenapiType for Res { - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { - Self::to_schema() + Self::schema() } } @@ -62,9 +62,9 @@ impl ResourceResult for Result } #[cfg(feature = "openapi")] - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { - R::to_schema() + R::schema() } } @@ -87,13 +87,14 @@ impl ResourceResult for Success } #[cfg(feature = "openapi")] - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { - T::to_schema() + T::schema() } } /// This can be returned from a resource when there is no content to send. +#[derive(Default)] pub struct NoContent; impl From<()> for NoContent @@ -112,9 +113,9 @@ impl ResourceResult for NoContent } #[cfg(feature = "openapi")] - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { - <()>::to_schema() + <()>::schema() } #[cfg(feature = "openapi")] @@ -138,9 +139,9 @@ impl ResourceResult for Result } #[cfg(feature = "openapi")] - fn to_schema() -> OpenapiSchema + fn schema() -> OpenapiSchema { - <()>::to_schema() + <()>::schema() } #[cfg(feature = "openapi")] diff --git a/gotham_restful/src/routing.rs b/gotham_restful/src/routing.rs index 12ef4f0..8907541 100644 --- a/gotham_restful/src/routing.rs +++ b/gotham_restful/src/routing.rs @@ -278,6 +278,7 @@ macro_rules! implDrawResourceRoutes { } } + #[allow(clippy::redundant_closure)] // doesn't work because of type parameters impl<'a, C, P> DrawResourceRoutes for (&mut $implType<'a, C, P>, String) where C : PipelineHandleChain

+ Copy + Send + Sync + 'static, diff --git a/gotham_restful_derive/src/lib.rs b/gotham_restful_derive/src/lib.rs index 5a25f32..3e595de 100644 --- a/gotham_restful_derive/src/lib.rs +++ b/gotham_restful_derive/src/lib.rs @@ -25,55 +25,47 @@ pub fn derive_resource(tokens : TokenStream) -> TokenStream #[proc_macro_attribute] pub fn rest_read_all(attr : TokenStream, item : TokenStream) -> TokenStream { - let output = expand_method(Method::ReadAll, attr, item); - output + expand_method(Method::ReadAll, attr, item) } #[proc_macro_attribute] pub fn rest_read(attr : TokenStream, item : TokenStream) -> TokenStream { - let output = expand_method(Method::Read, attr, item); - output + expand_method(Method::Read, attr, item) } #[proc_macro_attribute] pub fn rest_search(attr : TokenStream, item : TokenStream) -> TokenStream { - let output = expand_method(Method::Search, attr, item); - output + expand_method(Method::Search, attr, item) } #[proc_macro_attribute] pub fn rest_create(attr : TokenStream, item : TokenStream) -> TokenStream { - let output = expand_method(Method::Create, attr, item); - output + expand_method(Method::Create, attr, item) } #[proc_macro_attribute] pub fn rest_update_all(attr : TokenStream, item : TokenStream) -> TokenStream { - let output = expand_method(Method::UpdateAll, attr, item); - output + expand_method(Method::UpdateAll, attr, item) } #[proc_macro_attribute] pub fn rest_update(attr : TokenStream, item : TokenStream) -> TokenStream { - let output = expand_method(Method::Update, attr, item); - output + expand_method(Method::Update, attr, item) } #[proc_macro_attribute] pub fn rest_delete_all(attr : TokenStream, item : TokenStream) -> TokenStream { - let output = expand_method(Method::DeleteAll, attr, item); - output + expand_method(Method::DeleteAll, attr, item) } #[proc_macro_attribute] pub fn rest_delete(attr : TokenStream, item : TokenStream) -> TokenStream { - let output = expand_method(Method::Delete, attr, item); - output + expand_method(Method::Delete, attr, item) } diff --git a/gotham_restful_derive/src/method.rs b/gotham_restful_derive/src/method.rs index 801f985..77e9d4f 100644 --- a/gotham_restful_derive/src/method.rs +++ b/gotham_restful_derive/src/method.rs @@ -99,19 +99,11 @@ pub fn expand_method(method : Method, attrs : TokenStream, item : TokenStream) - }, FnArg::Receiver(_) => panic!("didn't expect self parameter") }).collect(); - let mut generics : Vec = Vec::new(); - for i in 1..args.len() - { - let (_, ty) = &args[i]; - generics.push(quote!(#ty)); - } + let mut generics : Vec = args.iter().skip(1).map(|(_, ty)| quote!(#ty)).collect(); generics.push(quote!(#ret)); let args : Vec = args.into_iter().map(|(pat, ty)| quote!(#pat : #ty)).collect(); let block = fun.block.stmts; - let ret_stmt = match is_no_content { - true => Some(quote!(().into())), - false => None - }; + let ret_stmt = if is_no_content { Some(quote!(#ret::default())) } else { None }; let trait_ident = method.trait_ident(); let fn_ident = method.fn_ident(); diff --git a/gotham_restful_derive/src/openapi_type.rs b/gotham_restful_derive/src/openapi_type.rs index 30aeb2b..97ed9b4 100644 --- a/gotham_restful_derive/src/openapi_type.rs +++ b/gotham_restful_derive/src/openapi_type.rs @@ -73,7 +73,7 @@ fn expand_field(field : &Field) -> TokenStream2 let ty = &field.ty; quote! {{ - let mut schema = <#ty>::to_schema(); + let mut schema = <#ty>::schema(); if schema.nullable { @@ -105,7 +105,7 @@ fn expand_field(field : &Field) -> TokenStream2 None => { properties.insert( stringify!(#ident).to_string(), - ReferenceOr::Item(Box::new(schema.to_schema())) + ReferenceOr::Item(Box::new(schema.into_schema())) ); } } @@ -128,7 +128,7 @@ pub fn expand_struct(input : ItemStruct) -> TokenStream2 quote!{ impl #generics ::gotham_restful::OpenapiType for #ident #generics { - fn to_schema() -> ::gotham_restful::OpenapiSchema + fn schema() -> ::gotham_restful::OpenapiSchema { use ::gotham_restful::{export::{openapi::*, IndexMap}, OpenapiSchema};