mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 13:02:28 +00:00
apply clippy suggestions
This commit is contained in:
parent
286466fcc9
commit
4429fced3b
7 changed files with 62 additions and 78 deletions
|
@ -60,11 +60,10 @@ impl OpenapiRouter
|
||||||
/// modify the path and add it back after the modification
|
/// modify the path and add it back after the modification
|
||||||
fn remove_path(&mut self, path : &str) -> PathItem
|
fn remove_path(&mut self, path : &str) -> PathItem
|
||||||
{
|
{
|
||||||
if let Some(Item(item)) = self.0.paths.swap_remove(path)
|
match self.0.paths.swap_remove(path) {
|
||||||
{
|
Some(Item(item)) => item,
|
||||||
return item;
|
_ => PathItem::default()
|
||||||
}
|
}
|
||||||
return PathItem::default()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_path<Path : ToString>(&mut self, path : Path, item : PathItem)
|
fn add_path<Path : ToString>(&mut self, path : Path, item : PathItem)
|
||||||
|
@ -78,11 +77,11 @@ impl OpenapiRouter
|
||||||
|
|
||||||
match &mut self.0.components {
|
match &mut self.0.components {
|
||||||
Some(comp) => {
|
Some(comp) => {
|
||||||
comp.schemas.insert(name, Item(schema.to_schema()));
|
comp.schemas.insert(name, Item(schema.into_schema()));
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
let mut comp = Components::default();
|
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);
|
self.0.components = Some(comp);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -103,17 +102,17 @@ impl OpenapiRouter
|
||||||
|
|
||||||
fn add_schema<T : OpenapiType>(&mut self) -> ReferenceOr<Schema>
|
fn add_schema<T : OpenapiType>(&mut self) -> ReferenceOr<Schema>
|
||||||
{
|
{
|
||||||
let mut schema = T::to_schema();
|
let mut schema = T::schema();
|
||||||
if let Some(name) = schema.name.clone()
|
match schema.name.clone() {
|
||||||
{
|
Some(name) => {
|
||||||
let reference = Reference { reference: format!("#/components/schemas/{}", name) };
|
let reference = Reference { reference: format!("#/components/schemas/{}", name) };
|
||||||
self.add_schema_impl(name, schema);
|
self.add_schema_impl(name, schema);
|
||||||
reference
|
reference
|
||||||
}
|
},
|
||||||
else
|
None => {
|
||||||
{
|
self.add_schema_dependencies(&mut schema.dependencies);
|
||||||
self.add_schema_dependencies(&mut schema.dependencies);
|
Item(schema.into_schema())
|
||||||
Item(schema.to_schema())
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -211,7 +210,7 @@ impl<'a> OperationParams<'a>
|
||||||
description: None,
|
description: None,
|
||||||
required: true,
|
required: true,
|
||||||
deprecated: None,
|
deprecated: None,
|
||||||
format: ParameterSchemaOrContent::Schema(Item(String::to_schema().to_schema())),
|
format: ParameterSchemaOrContent::Schema(Item(String::schema().into_schema())),
|
||||||
example: None,
|
example: None,
|
||||||
examples: IndexMap::new()
|
examples: IndexMap::new()
|
||||||
},
|
},
|
||||||
|
|
|
@ -32,7 +32,7 @@ impl OpenapiSchema
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_schema(self) -> Schema
|
pub fn into_schema(self) -> Schema
|
||||||
{
|
{
|
||||||
Schema {
|
Schema {
|
||||||
schema_data: SchemaData {
|
schema_data: SchemaData {
|
||||||
|
@ -54,12 +54,12 @@ impl OpenapiSchema
|
||||||
|
|
||||||
pub trait OpenapiType
|
pub trait OpenapiType
|
||||||
{
|
{
|
||||||
fn to_schema() -> OpenapiSchema;
|
fn schema() -> OpenapiSchema;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OpenapiType for ()
|
impl OpenapiType for ()
|
||||||
{
|
{
|
||||||
fn to_schema() -> OpenapiSchema
|
fn schema() -> OpenapiSchema
|
||||||
{
|
{
|
||||||
OpenapiSchema::new(SchemaKind::Type(Type::Object(ObjectType::default())))
|
OpenapiSchema::new(SchemaKind::Type(Type::Object(ObjectType::default())))
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ impl OpenapiType for ()
|
||||||
|
|
||||||
impl OpenapiType for bool
|
impl OpenapiType for bool
|
||||||
{
|
{
|
||||||
fn to_schema() -> OpenapiSchema
|
fn schema() -> OpenapiSchema
|
||||||
{
|
{
|
||||||
OpenapiSchema::new(SchemaKind::Type(Type::Boolean{}))
|
OpenapiSchema::new(SchemaKind::Type(Type::Boolean{}))
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ macro_rules! int_types {
|
||||||
($($int_ty:ty),*) => {$(
|
($($int_ty:ty),*) => {$(
|
||||||
impl OpenapiType for $int_ty
|
impl OpenapiType for $int_ty
|
||||||
{
|
{
|
||||||
fn to_schema() -> OpenapiSchema
|
fn schema() -> OpenapiSchema
|
||||||
{
|
{
|
||||||
OpenapiSchema::new(SchemaKind::Type(Type::Integer(IntegerType::default())))
|
OpenapiSchema::new(SchemaKind::Type(Type::Integer(IntegerType::default())))
|
||||||
}
|
}
|
||||||
|
@ -91,7 +91,7 @@ macro_rules! num_types {
|
||||||
($($num_ty:ty),*) => {$(
|
($($num_ty:ty),*) => {$(
|
||||||
impl OpenapiType for $num_ty
|
impl OpenapiType for $num_ty
|
||||||
{
|
{
|
||||||
fn to_schema() -> OpenapiSchema
|
fn schema() -> OpenapiSchema
|
||||||
{
|
{
|
||||||
OpenapiSchema::new(SchemaKind::Type(Type::Number(NumberType::default())))
|
OpenapiSchema::new(SchemaKind::Type(Type::Number(NumberType::default())))
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ macro_rules! str_types {
|
||||||
($($str_ty:ty),*) => {$(
|
($($str_ty:ty),*) => {$(
|
||||||
impl OpenapiType for $str_ty
|
impl OpenapiType for $str_ty
|
||||||
{
|
{
|
||||||
fn to_schema() -> OpenapiSchema
|
fn schema() -> OpenapiSchema
|
||||||
{
|
{
|
||||||
OpenapiSchema::new(SchemaKind::Type(Type::String(StringType::default())))
|
OpenapiSchema::new(SchemaKind::Type(Type::String(StringType::default())))
|
||||||
}
|
}
|
||||||
|
@ -131,9 +131,9 @@ str_types!(String, &str);
|
||||||
|
|
||||||
impl<T : OpenapiType> OpenapiType for Option<T>
|
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 mut dependencies = schema.dependencies.clone();
|
||||||
let schema = match schema.name.clone() {
|
let schema = match schema.name.clone() {
|
||||||
Some(name) => {
|
Some(name) => {
|
||||||
|
@ -155,20 +155,19 @@ impl<T : OpenapiType> OpenapiType for Option<T>
|
||||||
|
|
||||||
impl<T : OpenapiType> OpenapiType for Vec<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 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) };
|
Some(name) => {
|
||||||
dependencies.insert(name, schema);
|
let reference = Reference { reference: format!("#/components/schemas/{}", name) };
|
||||||
reference
|
dependencies.insert(name, schema);
|
||||||
}
|
reference
|
||||||
else
|
},
|
||||||
{
|
None => Item(Box::new(schema.into_schema()))
|
||||||
Item(Box::new(schema.to_schema()))
|
|
||||||
};
|
};
|
||||||
|
|
||||||
OpenapiSchema {
|
OpenapiSchema {
|
||||||
|
|
|
@ -11,7 +11,7 @@ pub trait ResourceResult
|
||||||
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>;
|
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>;
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
fn to_schema() -> OpenapiSchema;
|
fn schema() -> OpenapiSchema;
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
fn default_status() -> StatusCode
|
fn default_status() -> StatusCode
|
||||||
|
@ -23,9 +23,9 @@ pub trait ResourceResult
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
impl<Res : ResourceResult> crate::OpenapiType for Res
|
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")]
|
#[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")]
|
#[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.
|
/// This can be returned from a resource when there is no content to send.
|
||||||
|
#[derive(Default)]
|
||||||
pub struct NoContent;
|
pub struct NoContent;
|
||||||
|
|
||||||
impl From<()> for NoContent
|
impl From<()> for NoContent
|
||||||
|
@ -112,9 +113,9 @@ impl ResourceResult for NoContent
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
fn to_schema() -> OpenapiSchema
|
fn schema() -> OpenapiSchema
|
||||||
{
|
{
|
||||||
<()>::to_schema()
|
<()>::schema()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
|
@ -138,9 +139,9 @@ impl<E : Error> ResourceResult for Result<NoContent, E>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
fn to_schema() -> OpenapiSchema
|
fn schema() -> OpenapiSchema
|
||||||
{
|
{
|
||||||
<()>::to_schema()
|
<()>::schema()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
|
|
|
@ -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)
|
impl<'a, C, P> DrawResourceRoutes for (&mut $implType<'a, C, P>, String)
|
||||||
where
|
where
|
||||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||||
|
|
|
@ -25,55 +25,47 @@ pub fn derive_resource(tokens : TokenStream) -> TokenStream
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn rest_read_all(attr : TokenStream, item : TokenStream) -> TokenStream
|
pub fn rest_read_all(attr : TokenStream, item : TokenStream) -> TokenStream
|
||||||
{
|
{
|
||||||
let output = expand_method(Method::ReadAll, attr, item);
|
expand_method(Method::ReadAll, attr, item)
|
||||||
output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn rest_read(attr : TokenStream, item : TokenStream) -> TokenStream
|
pub fn rest_read(attr : TokenStream, item : TokenStream) -> TokenStream
|
||||||
{
|
{
|
||||||
let output = expand_method(Method::Read, attr, item);
|
expand_method(Method::Read, attr, item)
|
||||||
output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn rest_search(attr : TokenStream, item : TokenStream) -> TokenStream
|
pub fn rest_search(attr : TokenStream, item : TokenStream) -> TokenStream
|
||||||
{
|
{
|
||||||
let output = expand_method(Method::Search, attr, item);
|
expand_method(Method::Search, attr, item)
|
||||||
output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn rest_create(attr : TokenStream, item : TokenStream) -> TokenStream
|
pub fn rest_create(attr : TokenStream, item : TokenStream) -> TokenStream
|
||||||
{
|
{
|
||||||
let output = expand_method(Method::Create, attr, item);
|
expand_method(Method::Create, attr, item)
|
||||||
output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn rest_update_all(attr : TokenStream, item : TokenStream) -> TokenStream
|
pub fn rest_update_all(attr : TokenStream, item : TokenStream) -> TokenStream
|
||||||
{
|
{
|
||||||
let output = expand_method(Method::UpdateAll, attr, item);
|
expand_method(Method::UpdateAll, attr, item)
|
||||||
output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn rest_update(attr : TokenStream, item : TokenStream) -> TokenStream
|
pub fn rest_update(attr : TokenStream, item : TokenStream) -> TokenStream
|
||||||
{
|
{
|
||||||
let output = expand_method(Method::Update, attr, item);
|
expand_method(Method::Update, attr, item)
|
||||||
output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn rest_delete_all(attr : TokenStream, item : TokenStream) -> TokenStream
|
pub fn rest_delete_all(attr : TokenStream, item : TokenStream) -> TokenStream
|
||||||
{
|
{
|
||||||
let output = expand_method(Method::DeleteAll, attr, item);
|
expand_method(Method::DeleteAll, attr, item)
|
||||||
output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn rest_delete(attr : TokenStream, item : TokenStream) -> TokenStream
|
pub fn rest_delete(attr : TokenStream, item : TokenStream) -> TokenStream
|
||||||
{
|
{
|
||||||
let output = expand_method(Method::Delete, attr, item);
|
expand_method(Method::Delete, attr, item)
|
||||||
output
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,19 +99,11 @@ pub fn expand_method(method : Method, attrs : TokenStream, item : TokenStream) -
|
||||||
},
|
},
|
||||||
FnArg::Receiver(_) => panic!("didn't expect self parameter")
|
FnArg::Receiver(_) => panic!("didn't expect self parameter")
|
||||||
}).collect();
|
}).collect();
|
||||||
let mut generics : Vec<TokenStream2> = Vec::new();
|
let mut generics : Vec<TokenStream2> = args.iter().skip(1).map(|(_, ty)| quote!(#ty)).collect();
|
||||||
for i in 1..args.len()
|
|
||||||
{
|
|
||||||
let (_, ty) = &args[i];
|
|
||||||
generics.push(quote!(#ty));
|
|
||||||
}
|
|
||||||
generics.push(quote!(#ret));
|
generics.push(quote!(#ret));
|
||||||
let args : Vec<TokenStream2> = args.into_iter().map(|(pat, ty)| quote!(#pat : #ty)).collect();
|
let args : Vec<TokenStream2> = args.into_iter().map(|(pat, ty)| quote!(#pat : #ty)).collect();
|
||||||
let block = fun.block.stmts;
|
let block = fun.block.stmts;
|
||||||
let ret_stmt = match is_no_content {
|
let ret_stmt = if is_no_content { Some(quote!(#ret::default())) } else { None };
|
||||||
true => Some(quote!(().into())),
|
|
||||||
false => None
|
|
||||||
};
|
|
||||||
|
|
||||||
let trait_ident = method.trait_ident();
|
let trait_ident = method.trait_ident();
|
||||||
let fn_ident = method.fn_ident();
|
let fn_ident = method.fn_ident();
|
||||||
|
|
|
@ -73,7 +73,7 @@ fn expand_field(field : &Field) -> TokenStream2
|
||||||
let ty = &field.ty;
|
let ty = &field.ty;
|
||||||
|
|
||||||
quote! {{
|
quote! {{
|
||||||
let mut schema = <#ty>::to_schema();
|
let mut schema = <#ty>::schema();
|
||||||
|
|
||||||
if schema.nullable
|
if schema.nullable
|
||||||
{
|
{
|
||||||
|
@ -105,7 +105,7 @@ fn expand_field(field : &Field) -> TokenStream2
|
||||||
None => {
|
None => {
|
||||||
properties.insert(
|
properties.insert(
|
||||||
stringify!(#ident).to_string(),
|
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!{
|
quote!{
|
||||||
impl #generics ::gotham_restful::OpenapiType for #ident #generics
|
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};
|
use ::gotham_restful::{export::{openapi::*, IndexMap}, OpenapiSchema};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue