diff --git a/examples/users.rs b/examples/users.rs index 13cebea..7a3a78b 100644 --- a/examples/users.rs +++ b/examples/users.rs @@ -24,9 +24,9 @@ struct User username : String } -impl IndexResource>> for Users +impl ResourceReadAll>> for Users { - fn index(_state : &mut State) -> Success> + fn read_all(_state : &mut State) -> Success> { vec![Username().fake(), Username().fake()] .into_iter() @@ -36,16 +36,16 @@ impl IndexResource>> for Users } } -impl GetResource> for Users +impl ResourceRead> for Users { - fn get(_state : &mut State, id : u64) -> Success + fn read(_state : &mut State, id : u64) -> Success { let username : String = Username().fake(); User { username: format!("{}{}", username, id) }.into() } } -impl CreateResource> for Users +impl ResourceCreate> for Users { fn create(_state : &mut State, body : User) -> Success<()> { @@ -54,18 +54,18 @@ impl CreateResource> for Users } } -impl ChangeAllResource, Success<()>> for Users +impl ResourceUpdateAll, Success<()>> for Users { - fn change_all(_state : &mut State, body : Vec) -> Success<()> + fn update_all(_state : &mut State, body : Vec) -> Success<()> { info!("Changing all Users to {:?}", body.into_iter().map(|u| u.username).collect::>()); ().into() } } -impl ChangeResource> for Users +impl ResourceUpdate> 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(mut route : D) { - route.index::<_, Self>(); - route.get::<_, _, Self>(); - route.create::<_, _, Self>(); - route.change_all::<_, _, Self>(); - route.change::<_, _, _, Self>(); + route.read_all::(); + route.read::(); + route.create::(); + route.update_all::(); + route.update::(); } } diff --git a/src/lib.rs b/src/lib.rs index bb1bae5..936f179 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/resource.rs b/src/resource.rs index ea2c055..6bad8b2 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -11,35 +11,35 @@ pub trait Resource } /// Handle a GET request on the Resource root. -pub trait IndexResource +pub trait ResourceReadAll { - 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 +pub trait ResourceRead 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 +pub trait ResourceCreate { fn create(state : &mut State, body : Body) -> R; } /// Handle a PUT request on the Resource root. -pub trait ChangeAllResource +pub trait ResourceUpdateAll { - 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 +pub trait ResourceUpdate 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; } diff --git a/src/routing.rs b/src/routing.rs index 246171f..1701843 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -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(&mut self) + fn read_all(&mut self) where - R : ResourceResult, - IR : IndexResource; + Res : ResourceResult, + Handler : ResourceReadAll; - fn get(&mut self) + fn read(&mut self) where ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, - R : ResourceResult, - GR : GetResource; + Res : ResourceResult, + Handler : ResourceRead; - fn create(&mut self) + fn create(&mut self) where Body : DeserializeOwned, - R : ResourceResult, - CR : CreateResource; + Res : ResourceResult, + Handler : ResourceCreate; - fn change_all(&mut self) + fn update_all(&mut self) where Body : DeserializeOwned, - R : ResourceResult, - CR : ChangeAllResource; + Res : ResourceResult, + Handler : ResourceUpdateAll; - fn change(&mut self) + fn update(&mut self) where ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, Body : DeserializeOwned, - R : ResourceResult, - CR : ChangeResource; + Res : ResourceResult, + Handler : ResourceUpdate; } fn to_handler_future(mut state : State, get_result : F) -> Box @@ -131,44 +125,57 @@ where Box::new(f) } -fn index_handler>(state : State) -> Box +fn read_all_handler(state : State) -> Box +where + Res : ResourceResult, + Handler : ResourceReadAll { - to_handler_future(state, |state| IR::index(state)) + to_handler_future(state, |state| Handler::read_all(state)) } -fn get_handler>(state : State) -> Box +fn read_handler(state : State) -> Box where - ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static + ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, + Res : ResourceResult, + Handler : ResourceRead { let id = { let path : &PathExtractor = 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>(state : State) -> Box +fn create_handler(state : State) -> Box +where + Body : DeserializeOwned, + Res : ResourceResult, + Handler : ResourceCreate { - handle_with_body::(state, |state, body| CR::create(state, body)) + handle_with_body::(state, |state, body| Handler::create(state, body)) } -fn change_all_handler>(state : State) -> Box +fn update_all_handler(state : State) -> Box +where + Body : DeserializeOwned, + Res : ResourceResult, + Handler : ResourceUpdateAll { - handle_with_body::(state, |state, body| CR::change_all(state, body)) + handle_with_body::(state, |state, body| Handler::update_all(state, body)) } -fn change_handler(state : State) -> Box +fn update_handler(state : State) -> Box where ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, Body : DeserializeOwned, - R : ResourceResult, - CR : ChangeResource + Res : ResourceResult, + Handler : ResourceUpdate { let id = { let path : &PathExtractor = PathExtractor::borrow_from(&state); path.id.clone() }; - handle_with_body::(state, |state, body| CR::change(state, id, body)) + handle_with_body::(state, |state, body| Handler::update(state, id, body)) } macro_rules! implDrawResourceRoutes { @@ -189,56 +196,56 @@ macro_rules! implDrawResourceRoutes { C : PipelineHandleChain

+ Copy + Send + Sync + 'static, P : RefUnwindSafe + Send + Sync + 'static { - fn index(&mut self) + fn read_all(&mut self) where - R : ResourceResult, - IR : IndexResource + Res : ResourceResult, + Handler : ResourceReadAll { self.0.get(&self.1) - .to(|state| index_handler::(state)); + .to(|state| read_all_handler::(state)); } - fn get(&mut self) + fn read(&mut self) where ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, - R : ResourceResult, - IR : GetResource + Res : ResourceResult, + Handler : ResourceRead { self.0.get(&format!("{}/:id", self.1)) .with_path_extractor::>() - .to(|state| get_handler::(state)); + .to(|state| read_handler::(state)); } - fn create(&mut self) + fn create(&mut self) where Body : DeserializeOwned, - R : ResourceResult, - CR : CreateResource + Res : ResourceResult, + Handler : ResourceCreate { self.0.post(&self.1) - .to(|state| create_handler::(state)); + .to(|state| create_handler::(state)); } - fn change_all(&mut self) + fn update_all(&mut self) where Body : DeserializeOwned, - R : ResourceResult, - CR : ChangeAllResource + Res : ResourceResult, + Handler : ResourceUpdateAll { self.0.put(&self.1) - .to(|state| change_all_handler::(state)); + .to(|state| update_all_handler::(state)); } - fn change(&mut self) + fn update(&mut self) where ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, Body : DeserializeOwned, - R : ResourceResult, - CR : ChangeResource + Res : ResourceResult, + Handler : ResourceUpdate { self.0.put(&format!("{}/:id", self.1)) .with_path_extractor::>() - .to(|state| change_handler::(state)); + .to(|state| update_handler::(state)); } } }