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

attempt testing (doesn't work)

This commit is contained in:
Dominic 2019-09-27 15:35:02 +02:00
parent 5ef80deecf
commit a42d9750ef
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
7 changed files with 540 additions and 46 deletions

View file

@ -12,7 +12,7 @@ pub use resource::{
};
mod result;
pub use result::ResourceResult;
pub use result::{ResourceResult, Success};
mod routing;
pub use routing::{DrawResources, DrawResourceRoutes};

View file

@ -1,26 +1,23 @@
use crate::ResourceResult;
use crate::{DrawResourceRoutes, ResourceResult};
use gotham::state::State;
use serde::{
de::DeserializeOwned,
ser::Serialize
};
use serde::de::DeserializeOwned;
pub trait Resource
{
fn setup();
fn setup<D : DrawResourceRoutes>(route : D);
}
pub trait IndexResource<R : Serialize, E : Serialize, Res : ResourceResult<R, E>>
pub trait IndexResource<R : ResourceResult>
{
fn index(state : &mut State) -> Res;
fn index(state : &mut State) -> R;
}
pub trait GetResource<ID : DeserializeOwned, R : Serialize, E : Serialize, Res : ResourceResult<R, E>>
pub trait GetResource<ID : DeserializeOwned>
{
fn get(state : State, id : ID) -> Res;
fn get(state : State, id : ID) -> dyn ResourceResult;
}
pub trait PostResource<Body : DeserializeOwned, R : Serialize, E : Serialize, Res : ResourceResult<R, E>>
pub trait PostResource<Body : DeserializeOwned>
{
fn post(state : State, body : Body) -> Res;
fn post(state : State, body : Body) -> dyn ResourceResult;
}

View file

@ -1,12 +1,15 @@
use crate::StatusCode;
use serde::Serialize;
use serde_json::error::Error as SerdeJsonError;
use std::error::Error;
pub trait ResourceResult<R : Serialize, E : Serialize>
/// A trait provided to convert a resource's result to json.
pub trait ResourceResult
{
fn to_result(self) -> (StatusCode, Result<R, E>);
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>;
}
/// The default json returned on an 500 Internal Server Error.
#[derive(Debug, Serialize)]
pub struct ResourceError
{
@ -25,13 +28,35 @@ impl<T : ToString> From<T> for ResourceError
}
}
impl<R : Serialize, E : Error> ResourceResult<R, ResourceError> for Result<R, E>
impl<R : Serialize, E : Error> ResourceResult for Result<R, E>
{
fn to_result(self) -> (StatusCode, Result<R, ResourceError>)
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
{
match self {
Ok(r) => (StatusCode::OK, Ok(r)),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, Err(e.into()))
}
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)?)
}
})
}
}
/// 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)
}
}
impl<T : Serialize> ResourceResult for Success<T>
{
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
{
Ok((StatusCode::OK, serde_json::to_string(&self.0)?))
}
}

View file

@ -8,7 +8,6 @@ use gotham::{
state::State
};
use mime::APPLICATION_JSON;
use serde::Serialize;
use std::panic::RefUnwindSafe;
/// This trait adds the `resource` method to gotham's routing. It allows you to register
@ -22,22 +21,17 @@ pub trait DrawResources
/// `Resource::setup` method.
pub trait DrawResourceRoutes
{
fn index<R : Serialize, E : Serialize, Res : ResourceResult<R, E>, IR : IndexResource<R, E, Res>>(&mut self);
fn index<R : ResourceResult, IR : IndexResource<R>>(&mut self);
}
fn to_handler_future<R, E, Res>(state : State, r : Res) -> Box<HandlerFuture>
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
where
R : Serialize,
E : Serialize,
Res : ResourceResult<R, E>
F : FnOnce(&mut State) -> R,
R : ResourceResult
{
let (status, res) = r.to_result();
let json = match res {
Ok(json) => serde_json::to_string(&json),
Err(json) => serde_json::to_string(&json)
};
match json {
Ok(body) => {
let res = get_result(&mut state).to_json();
match res {
Ok((status, body)) => {
let res = create_response(&state, status, APPLICATION_JSON, body);
Box::new(ok((state, res)))
},
@ -45,15 +39,9 @@ where
}
}
fn index_handler<R, E, Res, IR>(mut state : State) -> Box<HandlerFuture>
where
R : Serialize,
E : Serialize,
Res : ResourceResult<R, E>,
IR : IndexResource<R, E, Res>
fn index_handler<R : ResourceResult, IR : IndexResource<R>>(state : State) -> Box<HandlerFuture>
{
let res = IR::index(&mut state);
to_handler_future(state, res)
to_handler_future(state, |state| IR::index(state))
}
macro_rules! implDrawResourceRoutes {
@ -65,19 +53,19 @@ macro_rules! implDrawResourceRoutes {
{
fn resource<R : Resource, T : ToString>(&mut self, path : T)
{
R::setup();
R::setup((self, path.to_string()));
}
}
impl<'a, C, P> DrawResourceRoutes for ($implType<'a, C, P>, String)
impl<'a, C, P> DrawResourceRoutes for (&mut $implType<'a, C, P>, String)
where
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
/// Register an `IndexResource` with this resource.
fn index<R : Serialize, E : Serialize, Res : ResourceResult<R, E>, IR : IndexResource<R, E, Res>>(&mut self)
fn index<R : ResourceResult, IR : IndexResource<R>>(&mut self)
{
self.0.get(&self.1).to(|state| index_handler::<R, E, Res, IR>(state));
self.0.get(&self.1).to(|state| index_handler::<R, IR>(state));
}
}
}