1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 21:12:28 +00:00
deprecated-gotham-restful/gotham_restful/src/result.rs

152 lines
2.8 KiB
Rust
Raw Normal View History

2019-09-30 20:58:15 +02:00
use crate::{ResourceType, StatusCode};
2019-10-01 00:23:34 +02:00
#[cfg(feature = "openapi")]
2019-10-05 14:50:05 +02:00
use crate::{OpenapiSchema, OpenapiType};
2019-09-26 17:24:40 +02:00
use serde::Serialize;
2019-09-27 15:35:02 +02:00
use serde_json::error::Error as SerdeJsonError;
2019-09-26 17:24:40 +02:00
use std::error::Error;
2019-09-27 15:35:02 +02:00
/// A trait provided to convert a resource's result to json.
pub trait ResourceResult
2019-09-26 17:24:40 +02:00
{
2019-09-27 15:35:02 +02:00
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>;
2019-09-30 23:53:55 +02:00
#[cfg(feature = "openapi")]
2019-10-01 15:33:05 +02:00
fn to_schema() -> OpenapiSchema;
2019-10-05 14:50:05 +02:00
#[cfg(feature = "openapi")]
fn default_status() -> StatusCode
{
StatusCode::OK
}
2019-09-26 17:24:40 +02:00
}
2019-10-01 00:49:13 +02:00
#[cfg(feature = "openapi")]
impl<Res : ResourceResult> crate::OpenapiType for Res
{
2019-10-01 15:33:05 +02:00
fn to_schema() -> OpenapiSchema
2019-10-01 00:49:13 +02:00
{
Self::to_schema()
}
}
2019-09-27 15:35:02 +02:00
/// The default json returned on an 500 Internal Server Error.
2019-09-26 17:24:40 +02:00
#[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()
}
}
}
2019-09-30 20:58:15 +02:00
impl<R : ResourceType, E : Error> ResourceResult for Result<R, E>
2019-09-26 17:24:40 +02:00
{
2019-09-27 15:35:02 +02:00
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
2019-09-26 17:24:40 +02:00
{
2019-09-27 15:35:02 +02:00
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)?)
}
})
}
2019-09-30 23:53:55 +02:00
#[cfg(feature = "openapi")]
2019-10-01 15:33:05 +02:00
fn to_schema() -> OpenapiSchema
2019-09-30 20:58:15 +02:00
{
R::to_schema()
}
2019-09-27 15:35:02 +02:00
}
/// 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)
}
}
2019-09-30 20:58:15 +02:00
impl<T : ResourceType> ResourceResult for Success<T>
2019-09-27 15:35:02 +02:00
{
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
{
Ok((StatusCode::OK, serde_json::to_string(&self.0)?))
2019-09-26 17:24:40 +02:00
}
2019-09-30 23:53:55 +02:00
#[cfg(feature = "openapi")]
2019-10-01 15:33:05 +02:00
fn to_schema() -> OpenapiSchema
2019-09-30 20:58:15 +02:00
{
T::to_schema()
}
2019-09-26 17:24:40 +02:00
}
2019-10-05 14:50:05 +02:00
/// This can be returned from a resource when there is no content to send.
pub struct NoContent;
impl From<()> for NoContent
{
fn from(_ : ()) -> Self
{
Self {}
}
}
impl ResourceResult for NoContent
{
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
{
Ok((StatusCode::NO_CONTENT, "".to_string()))
}
#[cfg(feature = "openapi")]
fn to_schema() -> OpenapiSchema
{
<()>::to_schema()
}
#[cfg(feature = "openapi")]
fn default_status() -> StatusCode
{
StatusCode::NO_CONTENT
}
}
2019-10-05 16:06:08 +02:00
impl<E : Error> ResourceResult for Result<NoContent, E>
{
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
{
Ok(match self {
Ok(_) => (StatusCode::NO_CONTENT, "".to_string()),
Err(e) => {
let err : ResourceError = e.into();
(StatusCode::INTERNAL_SERVER_ERROR, serde_json::to_string(&err)?)
}
})
}
#[cfg(feature = "openapi")]
fn to_schema() -> OpenapiSchema
{
<()>::to_schema()
}
#[cfg(feature = "openapi")]
fn default_status() -> StatusCode
{
StatusCode::NO_CONTENT
}
}