1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 04:52:28 +00:00

apply clippy suggestions

This commit is contained in:
Dominic 2019-10-13 23:36:10 +02:00
parent 286466fcc9
commit 4429fced3b
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
7 changed files with 62 additions and 78 deletions

View file

@ -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<Path : ToString>(&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<T : OpenapiType>(&mut self) -> ReferenceOr<Schema>
{
let mut schema = T::to_schema();
if let Some(name) = schema.name.clone()
{
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
}
else
{
},
None => {
self.add_schema_dependencies(&mut schema.dependencies);
Item(schema.to_schema())
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()
},

View file

@ -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<T : OpenapiType> OpenapiType for Option<T>
{
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<T : OpenapiType> OpenapiType for Option<T>
impl<T : OpenapiType> OpenapiType for Vec<T>
{
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()
{
Some(name) => {
let reference = Reference { reference: format!("#/components/schemas/{}", name) };
dependencies.insert(name, schema);
reference
}
else
{
Item(Box::new(schema.to_schema()))
},
None => Item(Box::new(schema.into_schema()))
};
OpenapiSchema {

View file

@ -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<Res : ResourceResult> crate::OpenapiType for Res
{
fn to_schema() -> OpenapiSchema
fn schema() -> OpenapiSchema
{
Self::to_schema()
Self::schema()
}
}
@ -62,9 +62,9 @@ impl<R : ResourceType, E : Error> ResourceResult for Result<R, E>
}
#[cfg(feature = "openapi")]
fn to_schema() -> OpenapiSchema
fn schema() -> OpenapiSchema
{
R::to_schema()
R::schema()
}
}
@ -87,13 +87,14 @@ impl<T : ResourceType> ResourceResult for Success<T>
}
#[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<E : Error> ResourceResult for Result<NoContent, E>
}
#[cfg(feature = "openapi")]
fn to_schema() -> OpenapiSchema
fn schema() -> OpenapiSchema
{
<()>::to_schema()
<()>::schema()
}
#[cfg(feature = "openapi")]

View file

@ -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<P> + Copy + Send + Sync + 'static,

View file

@ -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)
}

View file

@ -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<TokenStream2> = Vec::new();
for i in 1..args.len()
{
let (_, ty) = &args[i];
generics.push(quote!(#ty));
}
let mut generics : Vec<TokenStream2> = args.iter().skip(1).map(|(_, ty)| quote!(#ty)).collect();
generics.push(quote!(#ret));
let args : Vec<TokenStream2> = 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();

View file

@ -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};