2019-09-27 15:35:02 +02:00
|
|
|
use crate::{DrawResourceRoutes, ResourceResult};
|
2019-09-26 17:24:40 +02:00
|
|
|
use gotham::state::State;
|
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-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-27 15:35:02 +02:00
|
|
|
pub trait IndexResource<R : ResourceResult>
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2019-09-27 15:35:02 +02:00
|
|
|
fn index(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.
|
|
|
|
pub trait GetResource<ID, R : ResourceResult>
|
|
|
|
where
|
|
|
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2019-09-27 16:36:38 +02:00
|
|
|
fn get(state : &mut State, id : ID) -> R;
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 16:36:38 +02:00
|
|
|
/// Handle a POST request on the Resource root.
|
|
|
|
pub trait CreateResource<Body : DeserializeOwned, 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.
|
|
|
|
pub trait ChangeAllResource<Body : DeserializeOwned, R : ResourceResult>
|
|
|
|
{
|
|
|
|
fn change_all(state : &mut State, body : Body) -> R;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a PUT request on the Resource with an id.
|
|
|
|
pub trait ChangeResource<ID, Body : DeserializeOwned, R : ResourceResult>
|
|
|
|
where
|
|
|
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
|
|
|
{
|
|
|
|
fn change(state : &mut State, id : ID, body : Body) -> R;
|
|
|
|
}
|