2019-10-20 14:49:53 +00:00
|
|
|
use crate::{DrawResourceRoutes, RequestBody, ResourceResult, ResourceType};
|
2019-10-13 17:43:42 +02:00
|
|
|
use gotham::{
|
|
|
|
router::response::extender::StaticResponseExtender,
|
|
|
|
state::{State, StateData}
|
|
|
|
};
|
2019-09-27 15:35:02 +02:00
|
|
|
use serde::de::DeserializeOwned;
|
2019-09-27 16:36:38 +02:00
|
|
|
use std::panic::RefUnwindSafe;
|
2019-09-26 17:24:40 +02:00
|
|
|
|
2019-09-27 16:36:38 +02:00
|
|
|
/// This trait must be implemented by every RESTful Resource. It will
|
|
|
|
/// allow you to register the different methods for this Resource.
|
2019-09-26 17:24:40 +02:00
|
|
|
pub trait Resource
|
|
|
|
{
|
2019-09-30 20:10:51 +02:00
|
|
|
/// The name of this resource. Must be unique.
|
2019-09-30 19:19:06 +02:00
|
|
|
fn name() -> String;
|
2019-09-30 20:10:51 +02:00
|
|
|
|
|
|
|
/// Setup all routes of this resource. Take a look at the rest_resource!
|
|
|
|
/// macro if you don't feel like caring yourself.
|
2019-09-27 15:35:02 +02:00
|
|
|
fn setup<D : DrawResourceRoutes>(route : D);
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 16:36:38 +02:00
|
|
|
/// Handle a GET request on the Resource root.
|
2019-09-28 13:38:08 +02:00
|
|
|
pub trait ResourceReadAll<R : ResourceResult>
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2019-09-28 13:38:08 +02:00
|
|
|
fn read_all(state : &mut State) -> R;
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 16:36:38 +02:00
|
|
|
/// Handle a GET request on the Resource with an id.
|
2019-09-28 13:38:08 +02:00
|
|
|
pub trait ResourceRead<ID, R : ResourceResult>
|
2019-09-27 16:36:38 +02:00
|
|
|
where
|
|
|
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2019-09-28 13:38:08 +02:00
|
|
|
fn read(state : &mut State, id : ID) -> R;
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 17:43:42 +02:00
|
|
|
/// Handle a GET request on the Resource with additional search parameters.
|
|
|
|
pub trait ResourceSearch<Query : ResourceType, R : ResourceResult>
|
|
|
|
where
|
2019-10-20 14:49:53 +00:00
|
|
|
Query : ResourceType + DeserializeOwned + StateData + StaticResponseExtender
|
2019-10-13 17:43:42 +02:00
|
|
|
{
|
|
|
|
fn search(state : &mut State, query : Query) -> R;
|
|
|
|
}
|
|
|
|
|
2019-09-27 16:36:38 +02:00
|
|
|
/// Handle a POST request on the Resource root.
|
2019-10-20 14:49:53 +00:00
|
|
|
pub trait ResourceCreate<Body : RequestBody, R : ResourceResult>
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2019-09-27 17:43:01 +02:00
|
|
|
fn create(state : &mut State, body : Body) -> R;
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
2019-09-27 21:33:24 +02:00
|
|
|
|
|
|
|
/// Handle a PUT request on the Resource root.
|
2019-10-20 14:49:53 +00:00
|
|
|
pub trait ResourceUpdateAll<Body : RequestBody, R : ResourceResult>
|
2019-09-27 21:33:24 +02:00
|
|
|
{
|
2019-09-28 13:38:08 +02:00
|
|
|
fn update_all(state : &mut State, body : Body) -> R;
|
2019-09-27 21:33:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a PUT request on the Resource with an id.
|
2019-10-20 14:49:53 +00:00
|
|
|
pub trait ResourceUpdate<ID, Body : RequestBody, R : ResourceResult>
|
2019-09-27 21:33:24 +02:00
|
|
|
where
|
|
|
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
|
|
|
{
|
2019-09-28 13:38:08 +02:00
|
|
|
fn update(state : &mut State, id : ID, body : Body) -> R;
|
2019-09-27 21:33:24 +02:00
|
|
|
}
|
2019-09-29 19:19:38 +02:00
|
|
|
|
|
|
|
/// Handle a DELETE request on the Resource root.
|
|
|
|
pub trait ResourceDeleteAll<R : ResourceResult>
|
|
|
|
{
|
|
|
|
fn delete_all(state : &mut State) -> R;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a DELETE request on the Resource with an id.
|
|
|
|
pub trait ResourceDelete<ID, R : ResourceResult>
|
|
|
|
where
|
|
|
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
|
|
|
{
|
|
|
|
fn delete(state : &mut State, id : ID) -> R;
|
|
|
|
}
|