1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 13:02:28 +00:00
deprecated-gotham-restful/gotham_restful/src/resource.rs

102 lines
2.8 KiB
Rust
Raw Normal View History

2019-10-20 14:49:53 +00:00
use crate::{DrawResourceRoutes, RequestBody, ResourceResult, ResourceType};
2019-10-13 17:43:42 +02:00
use gotham::{
2020-04-06 16:20:08 +00:00
extractor::QueryStringExtractor,
state::State
2019-10-13 17:43:42 +02:00
};
2020-04-06 16:20:08 +00:00
use hyper::Body;
2019-09-27 15:35:02 +02:00
use serde::de::DeserializeOwned;
2020-04-15 20:55:25 +02:00
use std::{
future::Future,
panic::RefUnwindSafe,
pin::Pin
};
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
{
/// The name of this resource. Must be unique.
2019-09-30 19:19:06 +02:00
fn name() -> String;
/// 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
}
2020-04-06 16:20:08 +00:00
pub trait ResourceMethod
{
2020-04-15 20:55:25 +02:00
type Res : ResourceResult + Send + 'static;
2020-04-07 20:44:02 +00:00
#[cfg(feature = "openapi")]
fn operation_id() -> Option<String>
{
None
}
fn wants_auth() -> bool
{
false
}
2020-04-06 16:20:08 +00:00
}
2019-09-27 16:36:38 +02:00
/// Handle a GET request on the Resource root.
2020-04-06 16:20:08 +00:00
pub trait ResourceReadAll : ResourceMethod
2019-09-26 17:24:40 +02:00
{
2020-04-15 20:55:25 +02:00
fn read_all(state : &mut State) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
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.
2020-04-06 16:20:08 +00:00
pub trait ResourceRead : ResourceMethod
2019-09-26 17:24:40 +02:00
{
2020-04-06 16:20:08 +00:00
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
2020-04-15 20:55:25 +02:00
fn read(state : &mut State, id : Self::ID) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
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.
2020-04-06 16:20:08 +00:00
pub trait ResourceSearch : ResourceMethod
2019-10-13 17:43:42 +02:00
{
2020-04-06 16:20:08 +00:00
type Query : ResourceType + QueryStringExtractor<Body> + Sync;
2020-04-15 20:55:25 +02:00
fn search(state : &mut State, query : Self::Query) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
2019-10-13 17:43:42 +02:00
}
2019-09-27 16:36:38 +02:00
/// Handle a POST request on the Resource root.
2020-04-06 16:20:08 +00:00
pub trait ResourceCreate : ResourceMethod
2019-09-26 17:24:40 +02:00
{
2020-04-06 16:20:08 +00:00
type Body : RequestBody;
2020-04-15 20:55:25 +02:00
fn create(state : &mut State, body : Self::Body) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
2019-09-26 17:24:40 +02:00
}
2019-09-27 21:33:24 +02:00
/// Handle a PUT request on the Resource root.
2020-04-06 16:20:08 +00:00
pub trait ResourceUpdateAll : ResourceMethod
2019-09-27 21:33:24 +02:00
{
2020-04-06 16:20:08 +00:00
type Body : RequestBody;
2020-04-15 20:55:25 +02:00
fn update_all(state : &mut State, body : Self::Body) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
2019-09-27 21:33:24 +02:00
}
/// Handle a PUT request on the Resource with an id.
2020-04-06 16:20:08 +00:00
pub trait ResourceUpdate : ResourceMethod
2019-09-27 21:33:24 +02:00
{
2020-04-06 16:20:08 +00:00
type Body : RequestBody;
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
2020-04-15 20:55:25 +02:00
fn update(state : &mut State, id : Self::ID, body : Self::Body) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
2019-09-27 21:33:24 +02:00
}
2019-09-29 19:19:38 +02:00
/// Handle a DELETE request on the Resource root.
2020-04-06 16:20:08 +00:00
pub trait ResourceDeleteAll : ResourceMethod
2019-09-29 19:19:38 +02:00
{
2020-04-15 20:55:25 +02:00
fn delete_all(state : &mut State) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
2019-09-29 19:19:38 +02:00
}
/// Handle a DELETE request on the Resource with an id.
2020-04-06 16:20:08 +00:00
pub trait ResourceDelete : ResourceMethod
2019-09-29 19:19:38 +02:00
{
2020-04-06 16:20:08 +00:00
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
2020-04-15 20:55:25 +02:00
fn delete(state : &mut State, id : Self::ID) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
2019-09-29 19:19:38 +02:00
}