1
0
Fork 0
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:
Dominic 2019-09-28 13:38:08 +02:00
parent 6751f840da
commit 2e2cb1b787
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
4 changed files with 91 additions and 84 deletions

View file

@ -24,9 +24,9 @@ struct User
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()]
.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();
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<()>
{
@ -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>>());
().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);
().into()
@ -76,11 +76,11 @@ impl Resource for Users
{
fn setup<D : DrawResourceRoutes>(mut route : D)
{
route.index::<_, Self>();
route.get::<_, _, Self>();
route.create::<_, _, Self>();
route.change_all::<_, _, Self>();
route.change::<_, _, _, Self>();
route.read_all::<Self, _>();
route.read::<Self, _, _>();
route.create::<Self, _, _>();
route.update_all::<Self, _, _>();
route.update::<Self, _, _, _>();
}
}

View file

@ -6,11 +6,11 @@ pub use hyper::StatusCode;
mod resource;
pub use resource::{
Resource,
IndexResource,
GetResource,
CreateResource,
ChangeAllResource,
ChangeResource
ResourceReadAll,
ResourceRead,
ResourceCreate,
ResourceUpdateAll,
ResourceUpdate
};
mod result;

View file

@ -11,35 +11,35 @@ pub trait Resource
}
/// 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.
pub trait GetResource<ID, R : ResourceResult>
pub trait ResourceRead<ID, R : ResourceResult>
where
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.
pub trait CreateResource<Body : DeserializeOwned, R : ResourceResult>
pub trait ResourceCreate<Body : DeserializeOwned, R : ResourceResult>
{
fn create(state : &mut State, body : Body) -> R;
}
/// 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.
pub trait ChangeResource<ID, Body : DeserializeOwned, R : ResourceResult>
pub trait ResourceUpdate<ID, Body : DeserializeOwned, R : ResourceResult>
where
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;
}

View file

@ -1,12 +1,6 @@
use crate::{
result::ResourceError,
ChangeAllResource,
ChangeResource,
CreateResource,
GetResource,
IndexResource,
Resource,
ResourceResult,
resource::*,
result::{ResourceError, ResourceResult},
StatusCode
};
use futures::{
@ -42,35 +36,35 @@ pub trait DrawResources
/// `Resource::setup` method.
pub trait DrawResourceRoutes
{
fn index<R, IR>(&mut self)
fn read_all<Handler, Res>(&mut self)
where
R : ResourceResult,
IR : IndexResource<R>;
Res : ResourceResult,
Handler : ResourceReadAll<Res>;
fn get<ID, R, GR>(&mut self)
fn read<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
R : ResourceResult,
GR : GetResource<ID, R>;
Res : ResourceResult,
Handler : ResourceRead<ID, Res>;
fn create<Body, R, CR>(&mut self)
fn create<Handler, Body, Res>(&mut self)
where
Body : DeserializeOwned,
R : ResourceResult,
CR : CreateResource<Body, R>;
Res : ResourceResult,
Handler : ResourceCreate<Body, Res>;
fn change_all<Body, R, CR>(&mut self)
fn update_all<Handler, Body, Res>(&mut self)
where
Body : DeserializeOwned,
R : ResourceResult,
CR : ChangeAllResource<Body, R>;
Res : ResourceResult,
Handler : ResourceUpdateAll<Body, Res>;
fn change<ID, Body, R, CR>(&mut self)
fn update<Handler, ID, Body, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Body : DeserializeOwned,
R : ResourceResult,
CR : ChangeResource<ID, Body, R>;
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>;
}
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
@ -131,44 +125,57 @@ where
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
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceRead<ID, Res>
{
let id = {
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
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
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Body : DeserializeOwned,
R : ResourceResult,
CR : ChangeResource<ID, Body, R>
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>
{
let id = {
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
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 {
@ -189,56 +196,56 @@ macro_rules! implDrawResourceRoutes {
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
fn index<R, IR>(&mut self)
fn read_all<Handler, Res>(&mut self)
where
R : ResourceResult,
IR : IndexResource<R>
Res : ResourceResult,
Handler : ResourceReadAll<Res>
{
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
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
R : ResourceResult,
IR : GetResource<ID, R>
Res : ResourceResult,
Handler : ResourceRead<ID, Res>
{
self.0.get(&format!("{}/:id", self.1))
.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
Body : DeserializeOwned,
R : ResourceResult,
CR : CreateResource<Body, R>
Res : ResourceResult,
Handler : ResourceCreate<Body, Res>
{
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
Body : DeserializeOwned,
R : ResourceResult,
CR : ChangeAllResource<Body, R>
Res : ResourceResult,
Handler : ResourceUpdateAll<Body, Res>
{
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
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Body : DeserializeOwned,
R : ResourceResult,
CR : ChangeResource<ID, Body, R>
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>
{
self.0.put(&format!("{}/:id", self.1))
.with_path_extractor::<PathExtractor<ID>>()
.to(|state| change_handler::<ID, Body, R, CR>(state));
.to(|state| update_handler::<Handler, ID, Body, Res>(state));
}
}
}