mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 04:52:28 +00:00
rename a whole bunch of stuff
This commit is contained in:
parent
6751f840da
commit
2e2cb1b787
4 changed files with 91 additions and 84 deletions
|
@ -24,9 +24,9 @@ struct User
|
||||||
username : String
|
username : String
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IndexResource<Success<Vec<User>>> for Users
|
impl ResourceReadAll<Success<Vec<User>>> for Users
|
||||||
{
|
{
|
||||||
fn index(_state : &mut State) -> Success<Vec<User>>
|
fn read_all(_state : &mut State) -> Success<Vec<User>>
|
||||||
{
|
{
|
||||||
vec![Username().fake(), Username().fake()]
|
vec![Username().fake(), Username().fake()]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -36,16 +36,16 @@ impl IndexResource<Success<Vec<User>>> for Users
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GetResource<u64, Success<User>> for Users
|
impl ResourceRead<u64, Success<User>> for Users
|
||||||
{
|
{
|
||||||
fn get(_state : &mut State, id : u64) -> Success<User>
|
fn read(_state : &mut State, id : u64) -> Success<User>
|
||||||
{
|
{
|
||||||
let username : String = Username().fake();
|
let username : String = Username().fake();
|
||||||
User { username: format!("{}{}", username, id) }.into()
|
User { username: format!("{}{}", username, id) }.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CreateResource<User, Success<()>> for Users
|
impl ResourceCreate<User, Success<()>> for Users
|
||||||
{
|
{
|
||||||
fn create(_state : &mut State, body : User) -> Success<()>
|
fn create(_state : &mut State, body : User) -> Success<()>
|
||||||
{
|
{
|
||||||
|
@ -54,18 +54,18 @@ impl CreateResource<User, Success<()>> for Users
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChangeAllResource<Vec<User>, Success<()>> for Users
|
impl ResourceUpdateAll<Vec<User>, Success<()>> for Users
|
||||||
{
|
{
|
||||||
fn change_all(_state : &mut State, body : Vec<User>) -> Success<()>
|
fn update_all(_state : &mut State, body : Vec<User>) -> Success<()>
|
||||||
{
|
{
|
||||||
info!("Changing all Users to {:?}", body.into_iter().map(|u| u.username).collect::<Vec<String>>());
|
info!("Changing all Users to {:?}", body.into_iter().map(|u| u.username).collect::<Vec<String>>());
|
||||||
().into()
|
().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChangeResource<u64, User, Success<()>> for Users
|
impl ResourceUpdate<u64, User, Success<()>> for Users
|
||||||
{
|
{
|
||||||
fn change(_state : &mut State, id : u64, body : User) -> Success<()>
|
fn update(_state : &mut State, id : u64, body : User) -> Success<()>
|
||||||
{
|
{
|
||||||
info!("Change User {} to {}", id, body.username);
|
info!("Change User {} to {}", id, body.username);
|
||||||
().into()
|
().into()
|
||||||
|
@ -76,11 +76,11 @@ impl Resource for Users
|
||||||
{
|
{
|
||||||
fn setup<D : DrawResourceRoutes>(mut route : D)
|
fn setup<D : DrawResourceRoutes>(mut route : D)
|
||||||
{
|
{
|
||||||
route.index::<_, Self>();
|
route.read_all::<Self, _>();
|
||||||
route.get::<_, _, Self>();
|
route.read::<Self, _, _>();
|
||||||
route.create::<_, _, Self>();
|
route.create::<Self, _, _>();
|
||||||
route.change_all::<_, _, Self>();
|
route.update_all::<Self, _, _>();
|
||||||
route.change::<_, _, _, Self>();
|
route.update::<Self, _, _, _>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -6,11 +6,11 @@ pub use hyper::StatusCode;
|
||||||
mod resource;
|
mod resource;
|
||||||
pub use resource::{
|
pub use resource::{
|
||||||
Resource,
|
Resource,
|
||||||
IndexResource,
|
ResourceReadAll,
|
||||||
GetResource,
|
ResourceRead,
|
||||||
CreateResource,
|
ResourceCreate,
|
||||||
ChangeAllResource,
|
ResourceUpdateAll,
|
||||||
ChangeResource
|
ResourceUpdate
|
||||||
};
|
};
|
||||||
|
|
||||||
mod result;
|
mod result;
|
||||||
|
|
|
@ -11,35 +11,35 @@ pub trait Resource
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a GET request on the Resource root.
|
/// Handle a GET request on the Resource root.
|
||||||
pub trait IndexResource<R : ResourceResult>
|
pub trait ResourceReadAll<R : ResourceResult>
|
||||||
{
|
{
|
||||||
fn index(state : &mut State) -> R;
|
fn read_all(state : &mut State) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a GET request on the Resource with an id.
|
/// Handle a GET request on the Resource with an id.
|
||||||
pub trait GetResource<ID, R : ResourceResult>
|
pub trait ResourceRead<ID, R : ResourceResult>
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
||||||
{
|
{
|
||||||
fn get(state : &mut State, id : ID) -> R;
|
fn read(state : &mut State, id : ID) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a POST request on the Resource root.
|
/// Handle a POST request on the Resource root.
|
||||||
pub trait CreateResource<Body : DeserializeOwned, R : ResourceResult>
|
pub trait ResourceCreate<Body : DeserializeOwned, R : ResourceResult>
|
||||||
{
|
{
|
||||||
fn create(state : &mut State, body : Body) -> R;
|
fn create(state : &mut State, body : Body) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a PUT request on the Resource root.
|
/// Handle a PUT request on the Resource root.
|
||||||
pub trait ChangeAllResource<Body : DeserializeOwned, R : ResourceResult>
|
pub trait ResourceUpdateAll<Body : DeserializeOwned, R : ResourceResult>
|
||||||
{
|
{
|
||||||
fn change_all(state : &mut State, body : Body) -> R;
|
fn update_all(state : &mut State, body : Body) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a PUT request on the Resource with an id.
|
/// Handle a PUT request on the Resource with an id.
|
||||||
pub trait ChangeResource<ID, Body : DeserializeOwned, R : ResourceResult>
|
pub trait ResourceUpdate<ID, Body : DeserializeOwned, R : ResourceResult>
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
||||||
{
|
{
|
||||||
fn change(state : &mut State, id : ID, body : Body) -> R;
|
fn update(state : &mut State, id : ID, body : Body) -> R;
|
||||||
}
|
}
|
||||||
|
|
119
src/routing.rs
119
src/routing.rs
|
@ -1,12 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
result::ResourceError,
|
resource::*,
|
||||||
ChangeAllResource,
|
result::{ResourceError, ResourceResult},
|
||||||
ChangeResource,
|
|
||||||
CreateResource,
|
|
||||||
GetResource,
|
|
||||||
IndexResource,
|
|
||||||
Resource,
|
|
||||||
ResourceResult,
|
|
||||||
StatusCode
|
StatusCode
|
||||||
};
|
};
|
||||||
use futures::{
|
use futures::{
|
||||||
|
@ -42,35 +36,35 @@ pub trait DrawResources
|
||||||
/// `Resource::setup` method.
|
/// `Resource::setup` method.
|
||||||
pub trait DrawResourceRoutes
|
pub trait DrawResourceRoutes
|
||||||
{
|
{
|
||||||
fn index<R, IR>(&mut self)
|
fn read_all<Handler, Res>(&mut self)
|
||||||
where
|
where
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
IR : IndexResource<R>;
|
Handler : ResourceReadAll<Res>;
|
||||||
|
|
||||||
fn get<ID, R, GR>(&mut self)
|
fn read<Handler, ID, Res>(&mut self)
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
GR : GetResource<ID, R>;
|
Handler : ResourceRead<ID, Res>;
|
||||||
|
|
||||||
fn create<Body, R, CR>(&mut self)
|
fn create<Handler, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Body : DeserializeOwned,
|
Body : DeserializeOwned,
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
CR : CreateResource<Body, R>;
|
Handler : ResourceCreate<Body, Res>;
|
||||||
|
|
||||||
fn change_all<Body, R, CR>(&mut self)
|
fn update_all<Handler, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Body : DeserializeOwned,
|
Body : DeserializeOwned,
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
CR : ChangeAllResource<Body, R>;
|
Handler : ResourceUpdateAll<Body, Res>;
|
||||||
|
|
||||||
fn change<ID, Body, R, CR>(&mut self)
|
fn update<Handler, ID, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
Body : DeserializeOwned,
|
Body : DeserializeOwned,
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
CR : ChangeResource<ID, Body, R>;
|
Handler : ResourceUpdate<ID, Body, Res>;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
|
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
|
||||||
|
@ -131,44 +125,57 @@ where
|
||||||
Box::new(f)
|
Box::new(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index_handler<R : ResourceResult, IR : IndexResource<R>>(state : State) -> Box<HandlerFuture>
|
fn read_all_handler<Handler, Res>(state : State) -> Box<HandlerFuture>
|
||||||
|
where
|
||||||
|
Res : ResourceResult,
|
||||||
|
Handler : ResourceReadAll<Res>
|
||||||
{
|
{
|
||||||
to_handler_future(state, |state| IR::index(state))
|
to_handler_future(state, |state| Handler::read_all(state))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_handler<ID, R : ResourceResult, GR : GetResource<ID, R>>(state : State) -> Box<HandlerFuture>
|
fn read_handler<Handler, ID, Res>(state : State) -> Box<HandlerFuture>
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
|
Res : ResourceResult,
|
||||||
|
Handler : ResourceRead<ID, Res>
|
||||||
{
|
{
|
||||||
let id = {
|
let id = {
|
||||||
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
|
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
|
||||||
path.id.clone()
|
path.id.clone()
|
||||||
};
|
};
|
||||||
to_handler_future(state, |state| GR::get(state, id))
|
to_handler_future(state, |state| Handler::read(state, id))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_handler<Body : DeserializeOwned, R : ResourceResult, CR : CreateResource<Body, R>>(state : State) -> Box<HandlerFuture>
|
fn create_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||||
|
where
|
||||||
|
Body : DeserializeOwned,
|
||||||
|
Res : ResourceResult,
|
||||||
|
Handler : ResourceCreate<Body, Res>
|
||||||
{
|
{
|
||||||
handle_with_body::<Body, _, _>(state, |state, body| CR::create(state, body))
|
handle_with_body::<Body, _, _>(state, |state, body| Handler::create(state, body))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change_all_handler<Body : DeserializeOwned, R : ResourceResult, CR : ChangeAllResource<Body, R>>(state : State) -> Box<HandlerFuture>
|
fn update_all_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||||
|
where
|
||||||
|
Body : DeserializeOwned,
|
||||||
|
Res : ResourceResult,
|
||||||
|
Handler : ResourceUpdateAll<Body, Res>
|
||||||
{
|
{
|
||||||
handle_with_body::<Body, _, _>(state, |state, body| CR::change_all(state, body))
|
handle_with_body::<Body, _, _>(state, |state, body| Handler::update_all(state, body))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change_handler<ID, Body, R, CR>(state : State) -> Box<HandlerFuture>
|
fn update_handler<Handler, ID, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
Body : DeserializeOwned,
|
Body : DeserializeOwned,
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
CR : ChangeResource<ID, Body, R>
|
Handler : ResourceUpdate<ID, Body, Res>
|
||||||
{
|
{
|
||||||
let id = {
|
let id = {
|
||||||
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
|
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
|
||||||
path.id.clone()
|
path.id.clone()
|
||||||
};
|
};
|
||||||
handle_with_body::<Body, _, _>(state, |state, body| CR::change(state, id, body))
|
handle_with_body::<Body, _, _>(state, |state, body| Handler::update(state, id, body))
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! implDrawResourceRoutes {
|
macro_rules! implDrawResourceRoutes {
|
||||||
|
@ -189,56 +196,56 @@ macro_rules! implDrawResourceRoutes {
|
||||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||||
P : RefUnwindSafe + Send + Sync + 'static
|
P : RefUnwindSafe + Send + Sync + 'static
|
||||||
{
|
{
|
||||||
fn index<R, IR>(&mut self)
|
fn read_all<Handler, Res>(&mut self)
|
||||||
where
|
where
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
IR : IndexResource<R>
|
Handler : ResourceReadAll<Res>
|
||||||
{
|
{
|
||||||
self.0.get(&self.1)
|
self.0.get(&self.1)
|
||||||
.to(|state| index_handler::<R, IR>(state));
|
.to(|state| read_all_handler::<Handler, Res>(state));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get<ID, R, IR>(&mut self)
|
fn read<Handler, ID, Res>(&mut self)
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
IR : GetResource<ID, R>
|
Handler : ResourceRead<ID, Res>
|
||||||
{
|
{
|
||||||
self.0.get(&format!("{}/:id", self.1))
|
self.0.get(&format!("{}/:id", self.1))
|
||||||
.with_path_extractor::<PathExtractor<ID>>()
|
.with_path_extractor::<PathExtractor<ID>>()
|
||||||
.to(|state| get_handler::<ID, R, IR>(state));
|
.to(|state| read_handler::<Handler, ID, Res>(state));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create<Body, R, CR>(&mut self)
|
fn create<Handler, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Body : DeserializeOwned,
|
Body : DeserializeOwned,
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
CR : CreateResource<Body, R>
|
Handler : ResourceCreate<Body, Res>
|
||||||
{
|
{
|
||||||
self.0.post(&self.1)
|
self.0.post(&self.1)
|
||||||
.to(|state| create_handler::<Body, R, CR>(state));
|
.to(|state| create_handler::<Handler, Body, Res>(state));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change_all<Body, R, CR>(&mut self)
|
fn update_all<Handler, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Body : DeserializeOwned,
|
Body : DeserializeOwned,
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
CR : ChangeAllResource<Body, R>
|
Handler : ResourceUpdateAll<Body, Res>
|
||||||
{
|
{
|
||||||
self.0.put(&self.1)
|
self.0.put(&self.1)
|
||||||
.to(|state| change_all_handler::<Body, R, CR>(state));
|
.to(|state| update_all_handler::<Handler, Body, Res>(state));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change<ID, Body, R, CR>(&mut self)
|
fn update<Handler, ID, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
Body : DeserializeOwned,
|
Body : DeserializeOwned,
|
||||||
R : ResourceResult,
|
Res : ResourceResult,
|
||||||
CR : ChangeResource<ID, Body, R>
|
Handler : ResourceUpdate<ID, Body, Res>
|
||||||
{
|
{
|
||||||
self.0.put(&format!("{}/:id", self.1))
|
self.0.put(&format!("{}/:id", self.1))
|
||||||
.with_path_extractor::<PathExtractor<ID>>()
|
.with_path_extractor::<PathExtractor<ID>>()
|
||||||
.to(|state| change_handler::<ID, Body, R, CR>(state));
|
.to(|state| update_handler::<Handler, ID, Body, Res>(state));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue