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 {
|
2019-10-14 02:17:25 +02:00
|
|
|
Ok(_) => (Self::default_status(), "".to_string()),
|
2019-10-05 16:06:08 +02:00
|
|
|
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-14 02:17:25 +02:00
|
|
|
<NoContent as ResourceResult>::schema()
|
2019-10-05 16:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "openapi")]
|
|
|
|
fn default_status() -> StatusCode
|
|
|
|
{
|
2019-10-14 02:17:25 +02:00
|
|
|
NoContent::default_status()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test
|
|
|
|
{
|
|
|
|
use super::*;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Deserialize, Serialize)]
|
|
|
|
#[cfg_attr(feature = "openapi", derive(OpenapiType))]
|
|
|
|
struct Msg
|
|
|
|
{
|
|
|
|
msg : String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Error)]
|
|
|
|
#[error("An Error")]
|
|
|
|
struct MsgError;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resource_result_ok()
|
|
|
|
{
|
|
|
|
let ok : Result<Msg, MsgError> = Ok(Msg::default());
|
|
|
|
let (status, json) = ok.to_json().expect("didn't expect error response");
|
|
|
|
assert_eq!(status, StatusCode::OK);
|
|
|
|
assert_eq!(json, r#"{"msg":""}"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resource_result_err()
|
|
|
|
{
|
|
|
|
let err : Result<Msg, MsgError> = Err(MsgError::default());
|
|
|
|
let (status, json) = err.to_json().expect("didn't expect error response");
|
|
|
|
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
assert_eq!(json, format!(r#"{{"error":true,"message":"{}"}}"#, err.unwrap_err()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn success_always_successfull()
|
|
|
|
{
|
|
|
|
let success : Success<Msg> = Msg::default().into();
|
|
|
|
let (status, json) = success.to_json().expect("didn't expect error response");
|
|
|
|
assert_eq!(status, StatusCode::OK);
|
|
|
|
assert_eq!(json, r#"{"msg":""}"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_content_has_empty_json()
|
|
|
|
{
|
|
|
|
let no_content = NoContent::default();
|
|
|
|
let (status, json) = no_content.to_json().expect("didn't expect error response");
|
|
|
|
assert_eq!(status, StatusCode::NO_CONTENT);
|
|
|
|
assert_eq!(json, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_content_result()
|
|
|
|
{
|
|
|
|
let no_content = NoContent::default();
|
|
|
|
let res_def = no_content.to_json().expect("didn't expect error response");
|
|
|
|
let res_err = Result::<NoContent, MsgError>::Ok(no_content).to_json().expect("didn't expect error response");
|
|
|
|
assert_eq!(res_def, res_err);
|
2019-10-05 16:06:08 +02:00
|
|
|
}
|
|
|
|
}
|