mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-04-20 06:54:46 +00:00
add proc macro derive for openapitype
This commit is contained in:
parent
a4185a5665
commit
4ef216e8c8
17 changed files with 273 additions and 47 deletions
88
gotham_restful/src/result.rs
Normal file
88
gotham_restful/src/result.rs
Normal file
|
@ -0,0 +1,88 @@
|
|||
use crate::{ResourceType, StatusCode};
|
||||
#[cfg(feature = "openapi")]
|
||||
use crate::OpenapiSchema;
|
||||
use serde::Serialize;
|
||||
use serde_json::error::Error as SerdeJsonError;
|
||||
use std::error::Error;
|
||||
|
||||
/// A trait provided to convert a resource's result to json.
|
||||
pub trait ResourceResult
|
||||
{
|
||||
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>;
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
fn to_schema() -> OpenapiSchema;
|
||||
}
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
impl<Res : ResourceResult> crate::OpenapiType for Res
|
||||
{
|
||||
fn to_schema() -> OpenapiSchema
|
||||
{
|
||||
Self::to_schema()
|
||||
}
|
||||
}
|
||||
|
||||
/// The default json returned on an 500 Internal Server Error.
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct ResourceError
|
||||
{
|
||||
error : bool,
|
||||
message : String
|
||||
}
|
||||
|
||||
impl<T : ToString> From<T> for ResourceError
|
||||
{
|
||||
fn from(message : T) -> Self
|
||||
{
|
||||
Self {
|
||||
error: true,
|
||||
message: message.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R : ResourceType, E : Error> ResourceResult for Result<R, E>
|
||||
{
|
||||
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
|
||||
{
|
||||
Ok(match self {
|
||||
Ok(r) => (StatusCode::OK, serde_json::to_string(r)?),
|
||||
Err(e) => {
|
||||
let err : ResourceError = e.into();
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, serde_json::to_string(&err)?)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
fn to_schema() -> OpenapiSchema
|
||||
{
|
||||
R::to_schema()
|
||||
}
|
||||
}
|
||||
|
||||
/// This can be returned from a resource when there is no cause of an error.
|
||||
pub struct Success<T>(T);
|
||||
|
||||
impl<T> From<T> for Success<T>
|
||||
{
|
||||
fn from(t : T) -> Self
|
||||
{
|
||||
Self(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)?))
|
||||
}
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
fn to_schema() -> OpenapiSchema
|
||||
{
|
||||
T::to_schema()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue