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

introduce openapitype

This commit is contained in:
Dominic 2019-09-30 20:58:15 +02:00
parent 24f8fd96db
commit 681482ece3
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
6 changed files with 126 additions and 6 deletions

View file

@ -2,13 +2,18 @@
#[macro_use] extern crate serde;
pub use hyper::StatusCode;
#[cfg(not(feature = "openapi"))]
use serde::Serialize;
pub mod helper;
#[cfg(feature = "openapi")]
pub mod openapi;
#[cfg(feature = "openapi")]
pub use openapi::{GetOpenapi, OpenapiRouter};
pub use openapi::{
router::{GetOpenapi, OpenapiRouter},
types::OpenapiType
};
mod resource;
pub use resource::{
@ -29,3 +34,30 @@ mod routing;
pub use routing::{DrawResources, DrawResourceRoutes};
#[cfg(feature = "openapi")]
pub use routing::WithOpenapi;
/// A type that can be used inside a request or response body. Implemented for every type
/// that is serializable with serde, however, it is recommended to use the rest_struct!
/// macro to create one.
#[cfg(not(feature = "openapi"))]
pub trait ResourceType : Serialize
{
}
#[cfg(not(feature = "openapi"))]
impl<T : Serialize> ResourceType for T
{
}
/// A type that can be used inside a request or response body. Implemented for every type
/// that is serializable with serde, however, it is recommended to use the rest_struct!
/// macro to create one.
#[cfg(feature = "openapi")]
pub trait ResourceType : OpenapiType
{
}
#[cfg(feature = "openapi")]
impl<T : OpenapiType> ResourceType for T
{
}

3
src/openapi/mod.rs Normal file
View file

@ -0,0 +1,3 @@
pub mod router;
pub mod types;

View file

@ -14,7 +14,9 @@ use gotham::{
use indexmap::IndexMap;
use log::error;
use mime::{APPLICATION_JSON, TEXT_PLAIN};
use openapiv3::{MediaType, OpenAPI, Operation, PathItem, Paths, ReferenceOr, ReferenceOr::Item, Response, Responses, Server, StatusCode};
use openapiv3::{
MediaType, OpenAPI, Operation, PathItem, Paths, ReferenceOr, ReferenceOr::Item, Response, Responses, Server, StatusCode
};
use serde::de::DeserializeOwned;
use std::panic::RefUnwindSafe;

69
src/openapi/types.rs Normal file
View file

@ -0,0 +1,69 @@
use indexmap::IndexMap;
use openapiv3::{
IntegerType, NumberType, ObjectType, SchemaKind, StringType, Type
};
use serde::Serialize;
pub trait OpenapiType : Serialize
{
fn to_schema() -> SchemaKind;
}
impl OpenapiType for ()
{
fn to_schema() -> SchemaKind
{
SchemaKind::Type(Type::Object(ObjectType::default()))
}
}
impl OpenapiType for bool
{
fn to_schema() -> SchemaKind
{
SchemaKind::Type(Type::Boolean{})
}
}
macro_rules! int_types {
($($int_ty:ty),*) => {$(
impl OpenapiType for $int_ty
{
fn to_schema() -> SchemaKind
{
SchemaKind::Type(Type::Integer(IntegerType::default()))
}
}
)*}
}
int_types!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128);
macro_rules! num_types {
($($num_ty:ty),*) => {$(
impl OpenapiType for $num_ty
{
fn to_schema() -> SchemaKind
{
SchemaKind::Type(Type::Number(NumberType::default()))
}
}
)*}
}
num_types!(f32, f64);
macro_rules! str_types {
($($str_ty:ty),*) => {$(
impl OpenapiType for $str_ty
{
fn to_schema() -> SchemaKind
{
SchemaKind::Type(Type::String(StringType::default()))
}
}
)*}
}
str_types!(String, &str);

View file

@ -1,4 +1,5 @@
use crate::StatusCode;
use crate::{ResourceType, StatusCode};
use openapiv3::SchemaKind;
use serde::Serialize;
use serde_json::error::Error as SerdeJsonError;
use std::error::Error;
@ -7,6 +8,9 @@ use std::error::Error;
pub trait ResourceResult
{
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>;
#[cfg(feature = "openapi")]
fn to_schema() -> SchemaKind;
}
/// The default json returned on an 500 Internal Server Error.
@ -28,7 +32,7 @@ impl<T : ToString> From<T> for ResourceError
}
}
impl<R : Serialize, E : Error> ResourceResult for Result<R, E>
impl<R : ResourceType, E : Error> ResourceResult for Result<R, E>
{
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
{
@ -40,6 +44,11 @@ impl<R : Serialize, E : Error> ResourceResult for Result<R, E>
}
})
}
fn to_schema() -> SchemaKind
{
R::to_schema()
}
}
/// This can be returned from a resource when there is no cause of an error.
@ -53,10 +62,15 @@ impl<T> From<T> for Success<T>
}
}
impl<T : Serialize> ResourceResult for Success<T>
impl<T : ResourceType> ResourceResult for Success<T>
{
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
{
Ok((StatusCode::OK, serde_json::to_string(&self.0)?))
}
fn to_schema() -> SchemaKind
{
T::to_schema()
}
}

View file

@ -4,7 +4,7 @@ use crate::{
StatusCode
};
#[cfg(feature = "openapi")]
use crate::openapi::OpenapiRouter;
use crate::OpenapiRouter;
use futures::{
future::{Future, err, ok},