1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-20 14:57:01 +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,14 +1,14 @@
use super::{ResourceResult, into_response_helper};
use crate::{Response, ResponseBody};
use super::{into_response_helper, ResourceResult};
#[cfg(feature = "openapi")]
use crate::OpenapiSchema;
use crate::{Response, ResponseBody};
use gotham::hyper::StatusCode;
use mime::{Mime, APPLICATION_JSON};
use std::{
fmt::Debug,
future::Future,
pin::Pin,
ops::{Deref, DerefMut}
ops::{Deref, DerefMut},
pin::Pin
};
/**
@ -45,119 +45,95 @@ fn read_all(_state: &mut State) -> Success<MyResponse> {
#[derive(Debug)]
pub struct Success<T>(T);
impl<T> AsMut<T> for Success<T>
{
fn as_mut(&mut self) -> &mut T
{
impl<T> AsMut<T> for Success<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> AsRef<T> for Success<T>
{
fn as_ref(&self) -> &T
{
impl<T> AsRef<T> for Success<T> {
fn as_ref(&self) -> &T {
&self.0
}
}
impl<T> Deref for Success<T>
{
impl<T> Deref for Success<T> {
type Target = T;
fn deref(&self) -> &T
{
fn deref(&self) -> &T {
&self.0
}
}
impl<T> DerefMut for Success<T>
{
fn deref_mut(&mut self) -> &mut T
{
impl<T> DerefMut for Success<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> From<T> for Success<T>
{
fn from(t : T) -> Self
{
impl<T> From<T> for Success<T> {
fn from(t: T) -> Self {
Self(t)
}
}
impl<T : Clone> Clone for Success<T>
{
fn clone(&self) -> Self
{
impl<T: Clone> Clone for Success<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T : Copy> Copy for Success<T>
{
}
impl<T: Copy> Copy for Success<T> {}
impl<T : Default> Default for Success<T>
{
fn default() -> Self
{
impl<T: Default> Default for Success<T> {
fn default() -> Self {
Self(T::default())
}
}
impl<T : ResponseBody> ResourceResult for Success<T>
impl<T: ResponseBody> ResourceResult for Success<T>
where
Self : Send
Self: Send
{
type Err = serde_json::Error;
fn into_response(self) -> Pin<Box<dyn Future<Output = Result<Response, Self::Err>> + Send>>
{
fn into_response(self) -> Pin<Box<dyn Future<Output = Result<Response, Self::Err>> + Send>> {
into_response_helper(|| Ok(Response::json(StatusCode::OK, serde_json::to_string(self.as_ref())?)))
}
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 {
T::schema()
}
}
#[cfg(test)]
mod test
{
mod test {
use super::*;
use crate::result::OrAllTypes;
use futures_executor::block_on;
#[derive(Debug, Default, Serialize)]
#[cfg_attr(feature = "openapi", derive(crate::OpenapiType))]
struct Msg
{
msg : String
struct Msg {
msg: String
}
#[test]
fn success_always_successfull()
{
let success : Success<Msg> = Msg::default().into();
fn success_always_successfull() {
let success: Success<Msg> = Msg::default().into();
let res = block_on(success.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 success_accepts_json()
{
fn success_accepts_json() {
assert!(<Success<Msg>>::accepted_types().or_all_types().contains(&APPLICATION_JSON))
}
}