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

197 lines
3.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-13 23:36:10 +02:00
fn 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-13 23:36:10 +02:00
fn schema() -> OpenapiSchema
2019-10-01 00:49:13 +02:00
{
2019-10-13 23:36:10 +02:00
Self::schema()
2019-10-01 00:49:13 +02:00
}
}
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-13 23:36:10 +02:00
fn schema() -> OpenapiSchema
2019-09-30 20:58:15 +02:00
{
2019-10-13 23:36:10 +02:00
R::schema()
2019-09-30 20:58:15 +02:00
}
2019-09-27 15:35:02 +02:00
}
2019-10-14 00:59:02 +02:00
/**
This can be returned from a resource when there is no cause of an error. For example:
```
# #[macro_use] extern crate gotham_restful_derive;
# use gotham::state::State;
# use gotham_restful::Success;
# use serde::{Deserialize, Serialize};
#
# #[derive(Resource)]
# struct MyResource;
#
#[derive(Deserialize, Serialize)]
# #[derive(OpenapiType)]
struct MyResponse {
message: String
}
#[rest_read_all(MyResource)]
fn read_all(_state: &mut State) -> Success<MyResponse> {
let res = MyResponse { message: "I'm always happy".to_string() };
res.into()
}
```
*/
2019-09-27 15:35:02 +02:00
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-13 23:36:10 +02:00
fn schema() -> OpenapiSchema
2019-09-30 20:58:15 +02:00
{
2019-10-13 23:36:10 +02:00
T::schema()
2019-09-30 20:58:15 +02:00
}
2019-09-26 17:24:40 +02:00
}
2019-10-05 14:50:05 +02:00
2019-10-14 00:59:02 +02:00
/**
This is the return type of a resource that doesn't actually return something. It will result
in a _204 No Content_ answer by default. You don't need to use this type directly if using
the function attributes:
```
# #[macro_use] extern crate gotham_restful_derive;
# use gotham::state::State;
#
# #[derive(Resource)]
# struct MyResource;
#
#[rest_read_all(MyResource)]
fn read_all(_state: &mut State) {
// do something
}
```
*/
2019-10-13 23:36:10 +02:00
#[derive(Default)]
2019-10-05 14:50:05 +02:00
pub struct NoContent;
impl From<()> for NoContent
{
fn from(_ : ()) -> Self
{
Self {}
}
}
impl ResourceResult for NoContent
{
2019-10-14 00:59:02 +02:00
/// This will always be a _204 No Content_ together with an empty string.
2019-10-05 14:50:05 +02:00
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
{
2019-10-14 00:59:02 +02:00
Ok((Self::default_status(), "".to_string()))
2019-10-05 14:50:05 +02:00
}
2019-10-14 00:59:02 +02:00
/// Returns the schema of the `()` type.
2019-10-05 14:50:05 +02:00
#[cfg(feature = "openapi")]
2019-10-13 23:36:10 +02:00
fn schema() -> OpenapiSchema
2019-10-05 14:50:05 +02:00
{
2019-10-13 23:36:10 +02:00
<()>::schema()
2019-10-05 14:50:05 +02:00
}
2019-10-14 00:59:02 +02:00
/// This will always be a _204 No Content_
2019-10-05 14:50:05 +02:00
#[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")]
2019-10-13 23:36:10 +02:00
fn schema() -> OpenapiSchema
2019-10-05 16:06:08 +02:00
{
2019-10-13 23:36:10 +02:00
<()>::schema()
2019-10-05 16:06:08 +02:00
}
#[cfg(feature = "openapi")]
fn default_status() -> StatusCode
{
StatusCode::NO_CONTENT
}
}