2020-04-29 19:10:11 +02:00
|
|
|
use crate::{DrawResourceRoutes, RequestBody, ResourceID, ResourceResult, ResourceType};
|
2020-09-15 15:10:41 +02:00
|
|
|
use gotham::{extractor::QueryStringExtractor, hyper::Body, state::State};
|
|
|
|
use std::{future::Future, pin::Pin};
|
2019-09-26 17:24:40 +02:00
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// This trait must be implemented for every resource. It allows you to register the different
|
|
|
|
/// methods that can be handled by this resource to be registered with the underlying router.
|
|
|
|
///
|
|
|
|
/// It is not recommended to implement this yourself, rather just use `#[derive(Resource)]`.
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait Resource {
|
2020-05-09 18:01:47 +02:00
|
|
|
/// Register all methods handled by this resource with the underlying router.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn setup<D: DrawResourceRoutes>(route: D);
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// A common trait for every resource method. It defines the return type as well as some general
|
|
|
|
/// information about a resource method.
|
|
|
|
///
|
|
|
|
/// It is not recommended to implement this yourself. Rather, just write your handler method and
|
|
|
|
/// annotate it with `#[<method>(YourResource)]`, where `<method>` is one of the supported
|
|
|
|
/// resource methods.
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceMethod {
|
|
|
|
type Res: ResourceResult + Send + 'static;
|
|
|
|
|
2020-04-07 20:44:02 +00:00
|
|
|
#[cfg(feature = "openapi")]
|
2020-09-15 15:10:41 +02:00
|
|
|
fn operation_id() -> Option<String> {
|
2020-04-07 20:44:02 +00:00
|
|
|
None
|
|
|
|
}
|
2020-09-15 15:10:41 +02:00
|
|
|
|
|
|
|
fn wants_auth() -> bool {
|
2020-04-11 18:13:36 +00:00
|
|
|
false
|
|
|
|
}
|
2020-04-06 16:20:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The read_all [`ResourceMethod`](trait.ResourceMethod.html).
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceReadAll: ResourceMethod {
|
2020-05-09 18:01:47 +02:00
|
|
|
/// Handle a GET request on the Resource root.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn read_all(state: State) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The read [`ResourceMethod`](trait.ResourceMethod.html).
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceRead: ResourceMethod {
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The ID type to be parsed from the request path.
|
2020-09-15 15:10:41 +02:00
|
|
|
type ID: ResourceID + 'static;
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// Handle a GET request on the Resource with an id.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn read(state: State, id: Self::ID) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The search [`ResourceMethod`](trait.ResourceMethod.html).
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceSearch: ResourceMethod {
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The Query type to be parsed from the request parameters.
|
2020-09-15 15:10:41 +02:00
|
|
|
type Query: ResourceType + QueryStringExtractor<Body> + Sync;
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// Handle a GET request on the Resource with additional search parameters.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn search(state: State, query: Self::Query) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
2019-10-13 17:43:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The create [`ResourceMethod`](trait.ResourceMethod.html).
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceCreate: ResourceMethod {
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The Body type to be parsed from the request body.
|
2020-09-15 15:10:41 +02:00
|
|
|
type Body: RequestBody;
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// Handle a POST request on the Resource root.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn create(state: State, body: Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
2019-09-27 21:33:24 +02:00
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The change_all [`ResourceMethod`](trait.ResourceMethod.html).
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceChangeAll: ResourceMethod {
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The Body type to be parsed from the request body.
|
2020-09-15 15:10:41 +02:00
|
|
|
type Body: RequestBody;
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// Handle a PUT request on the Resource root.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn change_all(state: State, body: Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
2019-09-27 21:33:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The change [`ResourceMethod`](trait.ResourceMethod.html).
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceChange: ResourceMethod {
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The Body type to be parsed from the request body.
|
2020-09-15 15:10:41 +02:00
|
|
|
type Body: RequestBody;
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The ID type to be parsed from the request path.
|
2020-09-15 15:10:41 +02:00
|
|
|
type ID: ResourceID + 'static;
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// Handle a PUT request on the Resource with an id.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn change(state: State, id: Self::ID, body: Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
2019-09-27 21:33:24 +02:00
|
|
|
}
|
2019-09-29 19:19:38 +02:00
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The remove_all [`ResourceMethod`](trait.ResourceMethod.html).
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceRemoveAll: ResourceMethod {
|
2020-05-09 18:01:47 +02:00
|
|
|
/// Handle a DELETE request on the Resource root.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn remove_all(state: State) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
2019-09-29 19:19:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The remove [`ResourceMethod`](trait.ResourceMethod.html).
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceRemove: ResourceMethod {
|
2020-05-09 18:01:47 +02:00
|
|
|
/// The ID type to be parsed from the request path.
|
2020-09-15 15:10:41 +02:00
|
|
|
type ID: ResourceID + 'static;
|
|
|
|
|
2020-05-09 18:01:47 +02:00
|
|
|
/// Handle a DELETE request on the Resource with an id.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn remove(state: State, id: Self::ID) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
2019-09-29 19:19:38 +02:00
|
|
|
}
|