1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-20 06:54:46 +00:00

update to gotham 0.5 and start using rustfmt

This commit is contained in:
Dominic 2020-09-15 15:10:41 +02:00
parent 5317e50961
commit d55b0897e9
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
39 changed files with 1798 additions and 2095 deletions

View file

@ -1,106 +1,95 @@
use super::{ResourceResult, handle_error, into_response_helper};
use crate::{
result::ResourceError,
Response, ResponseBody, StatusCode
};
use super::{handle_error, into_response_helper, ResourceResult};
#[cfg(feature = "openapi")]
use crate::OpenapiSchema;
use crate::{result::ResourceError, Response, ResponseBody, StatusCode};
use futures_core::future::Future;
use mime::{Mime, APPLICATION_JSON};
use std::{
error::Error,
fmt::Display,
pin::Pin
};
use std::{error::Error, fmt::Display, pin::Pin};
pub trait IntoResponseError {
type Err: Error + Send + 'static;
pub trait IntoResponseError
{
type Err : Error + Send + 'static;
fn into_response_error(self) -> Result<Response, Self::Err>;
}
impl<E : Error> IntoResponseError for E
{
impl<E: Error> IntoResponseError for E {
type Err = serde_json::Error;
fn into_response_error(self) -> Result<Response, Self::Err>
{
let err : ResourceError = self.into();
Ok(Response::json(StatusCode::INTERNAL_SERVER_ERROR, serde_json::to_string(&err)?))
fn into_response_error(self) -> Result<Response, Self::Err> {
let err: ResourceError = self.into();
Ok(Response::json(
StatusCode::INTERNAL_SERVER_ERROR,
serde_json::to_string(&err)?
))
}
}
impl<R, E> ResourceResult for Result<R, E>
where
R : ResponseBody,
E : Display + IntoResponseError<Err = serde_json::Error>
R: ResponseBody,
E: Display + IntoResponseError<Err = serde_json::Error>
{
type Err = E::Err;
fn into_response(self) -> Pin<Box<dyn Future<Output = Result<Response, E::Err>> + Send>>
{
fn into_response(self) -> Pin<Box<dyn Future<Output = Result<Response, E::Err>> + Send>> {
match self {
Ok(r) => into_response_helper(|| Ok(Response::json(StatusCode::OK, serde_json::to_string(&r)?))),
Err(e) => handle_error(e)
}
}
fn accepted_types() -> Option<Vec<Mime>>
{
fn accepted_types() -> Option<Vec<Mime>> {
Some(vec![APPLICATION_JSON])
}
#[cfg(feature = "openapi")]
fn schema() -> OpenapiSchema
{
fn schema() -> OpenapiSchema {
R::schema()
}
}
#[cfg(test)]
mod test
{
mod test {
use super::*;
use crate::result::OrAllTypes;
use futures_executor::block_on;
use thiserror::Error;
#[derive(Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "openapi", derive(crate::OpenapiType))]
struct Msg
{
msg : String
struct Msg {
msg: String
}
#[derive(Debug, Default, Error)]
#[error("An Error")]
struct MsgError;
#[test]
fn result_ok()
{
let ok : Result<Msg, MsgError> = Ok(Msg::default());
fn result_ok() {
let ok: Result<Msg, MsgError> = Ok(Msg::default());
let res = block_on(ok.into_response()).expect("didn't expect error response");
assert_eq!(res.status, StatusCode::OK);
assert_eq!(res.mime, Some(APPLICATION_JSON));
assert_eq!(res.full_body().unwrap(), r#"{"msg":""}"#.as_bytes());
}
#[test]
fn result_err()
{
let err : Result<Msg, MsgError> = Err(MsgError::default());
fn result_err() {
let err: Result<Msg, MsgError> = Err(MsgError::default());
let res = block_on(err.into_response()).expect("didn't expect error response");
assert_eq!(res.status, StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(res.mime, Some(APPLICATION_JSON));
assert_eq!(res.full_body().unwrap(), format!(r#"{{"error":true,"message":"{}"}}"#, MsgError::default()).as_bytes());
assert_eq!(
res.full_body().unwrap(),
format!(r#"{{"error":true,"message":"{}"}}"#, MsgError::default()).as_bytes()
);
}
#[test]
fn success_accepts_json()
{
assert!(<Result<Msg, MsgError>>::accepted_types().or_all_types().contains(&APPLICATION_JSON))
fn success_accepts_json() {
assert!(<Result<Msg, MsgError>>::accepted_types()
.or_all_types()
.contains(&APPLICATION_JSON))
}
}