1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 04:52:28 +00:00

rename update to change, delete to remove, and remove rest_ prefix from macros

This commit is contained in:
Dominic 2020-05-04 20:45:46 +02:00
parent 3de130e104
commit cc86d3396c
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
8 changed files with 85 additions and 88 deletions

View file

@ -18,13 +18,13 @@ use log4rs::{
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Resource)] #[derive(Resource)]
#[rest_resource(ReadAll, Read, Search, Create, DeleteAll, Delete, Update, UpdateAll)] #[resource(read_all, read, search, create, change_all, change, remove, remove_all)]
struct Users struct Users
{ {
} }
#[derive(Resource)] #[derive(Resource)]
#[rest_resource(ReadAll)] #[resource(ReadAll)]
struct Auth struct Auth
{ {
} }
@ -35,7 +35,7 @@ struct User
username : String username : String
} }
#[rest_read_all(Users)] #[read_all(Users)]
fn read_all() -> Success<Vec<Option<User>>> fn read_all() -> Success<Vec<Option<User>>>
{ {
vec![Username().fake(), Username().fake()] vec![Username().fake(), Username().fake()]
@ -45,50 +45,50 @@ fn read_all() -> Success<Vec<Option<User>>>
.into() .into()
} }
#[rest_read(Users)] #[read(Users)]
fn read(id : u64) -> Success<User> fn read(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()
} }
#[rest_search(Users)] #[search(Users)]
fn search(query : User) -> Success<User> fn search(query : User) -> Success<User>
{ {
query.into() query.into()
} }
#[rest_create(Users)] #[create(Users)]
fn create(body : User) fn create(body : User)
{ {
info!("Created User: {}", body.username); info!("Created User: {}", body.username);
} }
#[rest_update_all(Users)] #[change_all(Users)]
fn update_all(body : Vec<User>) fn update_all(body : Vec<User>)
{ {
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>>());
} }
#[rest_update(Users)] #[change(Users)]
fn update(id : u64, body : User) fn update(id : u64, body : User)
{ {
info!("Change User {} to {}", id, body.username); info!("Change User {} to {}", id, body.username);
} }
#[rest_delete_all(Users)] #[delete_all(Users)]
fn delete_all() fn delete_all()
{ {
info!("Delete all Users"); info!("Delete all Users");
} }
#[rest_delete(Users)] #[delete(Users)]
fn delete(id : u64) fn delete(id : u64)
{ {
info!("Delete User {}", id); info!("Delete User {}", id);
} }
#[rest_read_all(Auth)] #[read_all(Auth)]
fn auth_read_all(auth : AuthStatus<()>) -> AuthSuccess<String> fn auth_read_all(auth : AuthStatus<()>) -> AuthSuccess<String>
{ {
match auth { match auth {

View file

@ -175,10 +175,10 @@ pub use resource::{
ResourceRead, ResourceRead,
ResourceSearch, ResourceSearch,
ResourceCreate, ResourceCreate,
ResourceUpdateAll, ResourceChangeAll,
ResourceUpdate, ResourceChange,
ResourceDeleteAll, ResourceRemoveAll,
ResourceDelete ResourceRemove
}; };
mod response; mod response;

View file

@ -106,7 +106,7 @@ macro_rules! implOpenapiRouter {
(&mut *(self.0).router, self.1).create::<Handler>() (&mut *(self.0).router, self.1).create::<Handler>()
} }
fn update_all<Handler : ResourceUpdateAll>(&mut self) fn change_all<Handler : ResourceChangeAll>(&mut self)
where where
Handler::Res : 'static, Handler::Res : 'static,
Handler::Body : 'static Handler::Body : 'static
@ -119,10 +119,10 @@ macro_rules! implOpenapiRouter {
item.put = Some(OperationDescription::new::<Handler>(schema).with_body::<Handler::Body>(body_schema).into_operation()); item.put = Some(OperationDescription::new::<Handler>(schema).with_body::<Handler::Body>(body_schema).into_operation());
(self.0).openapi_builder.add_path(path, item); (self.0).openapi_builder.add_path(path, item);
(&mut *(self.0).router, self.1).update_all::<Handler>() (&mut *(self.0).router, self.1).change_all::<Handler>()
} }
fn update<Handler : ResourceUpdate>(&mut self) fn change<Handler : ResourceChange>(&mut self)
where where
Handler::Res : 'static, Handler::Res : 'static,
Handler::Body : 'static Handler::Body : 'static
@ -136,10 +136,10 @@ macro_rules! implOpenapiRouter {
item.put = Some(OperationDescription::new::<Handler>(schema).add_path_param("id", id_schema).with_body::<Handler::Body>(body_schema).into_operation()); item.put = Some(OperationDescription::new::<Handler>(schema).add_path_param("id", id_schema).with_body::<Handler::Body>(body_schema).into_operation());
(self.0).openapi_builder.add_path(path, item); (self.0).openapi_builder.add_path(path, item);
(&mut *(self.0).router, self.1).update::<Handler>() (&mut *(self.0).router, self.1).change::<Handler>()
} }
fn delete_all<Handler : ResourceDeleteAll>(&mut self) fn remove_all<Handler : ResourceRemoveAll>(&mut self)
{ {
let schema = (self.0).openapi_builder.add_schema::<Handler::Res>(); let schema = (self.0).openapi_builder.add_schema::<Handler::Res>();
@ -148,10 +148,10 @@ macro_rules! implOpenapiRouter {
item.delete = Some(OperationDescription::new::<Handler>(schema).into_operation()); item.delete = Some(OperationDescription::new::<Handler>(schema).into_operation());
(self.0).openapi_builder.add_path(path, item); (self.0).openapi_builder.add_path(path, item);
(&mut *(self.0).router, self.1).delete_all::<Handler>() (&mut *(self.0).router, self.1).remove_all::<Handler>()
} }
fn delete<Handler : ResourceDelete>(&mut self) fn remove<Handler : ResourceRemove>(&mut self)
{ {
let schema = (self.0).openapi_builder.add_schema::<Handler::Res>(); let schema = (self.0).openapi_builder.add_schema::<Handler::Res>();
let id_schema = (self.0).openapi_builder.add_schema::<Handler::ID>(); let id_schema = (self.0).openapi_builder.add_schema::<Handler::ID>();
@ -161,7 +161,7 @@ macro_rules! implOpenapiRouter {
item.delete = Some(OperationDescription::new::<Handler>(schema).add_path_param("id", id_schema).into_operation()); item.delete = Some(OperationDescription::new::<Handler>(schema).add_path_param("id", id_schema).into_operation());
(self.0).openapi_builder.add_path(path, item); (self.0).openapi_builder.add_path(path, item);
(&mut *(self.0).router, self.1).delete::<Handler>() (&mut *(self.0).router, self.1).remove::<Handler>()
} }
} }

View file

@ -68,32 +68,32 @@ pub trait ResourceCreate : ResourceMethod
} }
/// Handle a PUT request on the Resource root. /// Handle a PUT request on the Resource root.
pub trait ResourceUpdateAll : ResourceMethod pub trait ResourceChangeAll : ResourceMethod
{ {
type Body : RequestBody; type Body : RequestBody;
fn update_all(state : State, body : Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>; fn change_all(state : State, body : Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
} }
/// Handle a PUT request on the Resource with an id. /// Handle a PUT request on the Resource with an id.
pub trait ResourceUpdate : ResourceMethod pub trait ResourceChange : ResourceMethod
{ {
type Body : RequestBody; type Body : RequestBody;
type ID : ResourceID + 'static; type ID : ResourceID + 'static;
fn update(state : State, id : Self::ID, body : Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>; fn change(state : State, id : Self::ID, body : Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
} }
/// Handle a DELETE request on the Resource root. /// Handle a DELETE request on the Resource root.
pub trait ResourceDeleteAll : ResourceMethod pub trait ResourceRemoveAll : ResourceMethod
{ {
fn delete_all(state : State) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>; fn remove_all(state : State) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
} }
/// Handle a DELETE request on the Resource with an id. /// Handle a DELETE request on the Resource with an id.
pub trait ResourceDelete : ResourceMethod pub trait ResourceRemove : ResourceMethod
{ {
type ID : ResourceID + 'static; type ID : ResourceID + 'static;
fn delete(state : State, id : Self::ID) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>; fn remove(state : State, id : Self::ID) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
} }

View file

@ -78,19 +78,19 @@ pub trait DrawResourceRoutes
Handler::Res : 'static, Handler::Res : 'static,
Handler::Body : 'static; Handler::Body : 'static;
fn update_all<Handler : ResourceUpdateAll>(&mut self) fn change_all<Handler : ResourceChangeAll>(&mut self)
where where
Handler::Res : 'static, Handler::Res : 'static,
Handler::Body : 'static; Handler::Body : 'static;
fn update<Handler : ResourceUpdate>(&mut self) fn change<Handler : ResourceChange>(&mut self)
where where
Handler::Res : 'static, Handler::Res : 'static,
Handler::Body : 'static; Handler::Body : 'static;
fn delete_all<Handler : ResourceDeleteAll>(&mut self); fn remove_all<Handler : ResourceRemoveAll>(&mut self);
fn delete<Handler : ResourceDelete>(&mut self); fn remove<Handler : ResourceRemove>(&mut self);
} }
fn response_from(res : Response, state : &State) -> gotham::hyper::Response<Body> fn response_from(res : Response, state : &State) -> gotham::hyper::Response<Body>
@ -217,15 +217,15 @@ where
handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::create(state, body)) handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::create(state, body))
} }
fn update_all_handler<Handler : ResourceUpdateAll>(state : State) -> Pin<Box<HandlerFuture>> fn change_all_handler<Handler : ResourceChangeAll>(state : State) -> Pin<Box<HandlerFuture>>
where where
Handler::Res : 'static, Handler::Res : 'static,
Handler::Body : 'static Handler::Body : 'static
{ {
handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::update_all(state, body)) handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::change_all(state, body))
} }
fn update_handler<Handler : ResourceUpdate>(state : State) -> Pin<Box<HandlerFuture>> fn change_handler<Handler : ResourceChange>(state : State) -> Pin<Box<HandlerFuture>>
where where
Handler::Res : 'static, Handler::Res : 'static,
Handler::Body : 'static Handler::Body : 'static
@ -234,21 +234,21 @@ where
let path : &PathExtractor<Handler::ID> = PathExtractor::borrow_from(&state); let path : &PathExtractor<Handler::ID> = PathExtractor::borrow_from(&state);
path.id.clone() path.id.clone()
}; };
handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::update(state, id, body)) handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::change(state, id, body))
} }
fn delete_all_handler<Handler : ResourceDeleteAll>(state : State) -> Pin<Box<HandlerFuture>> fn remove_all_handler<Handler : ResourceRemoveAll>(state : State) -> Pin<Box<HandlerFuture>>
{ {
to_handler_future(state, |state| Handler::delete_all(state)).boxed() to_handler_future(state, |state| Handler::remove_all(state)).boxed()
} }
fn delete_handler<Handler : ResourceDelete>(state : State) -> Pin<Box<HandlerFuture>> fn remove_handler<Handler : ResourceRemove>(state : State) -> Pin<Box<HandlerFuture>>
{ {
let id = { let id = {
let path : &PathExtractor<Handler::ID> = PathExtractor::borrow_from(&state); let path : &PathExtractor<Handler::ID> = PathExtractor::borrow_from(&state);
path.id.clone() path.id.clone()
}; };
to_handler_future(state, |state| Handler::delete(state, id)).boxed() to_handler_future(state, |state| Handler::remove(state, id)).boxed()
} }
#[derive(Clone)] #[derive(Clone)]
@ -386,7 +386,7 @@ macro_rules! implDrawResourceRoutes {
.to(|state| create_handler::<Handler>(state)); .to(|state| create_handler::<Handler>(state));
} }
fn update_all<Handler : ResourceUpdateAll>(&mut self) fn change_all<Handler : ResourceChangeAll>(&mut self)
where where
Handler::Res : Send + 'static, Handler::Res : Send + 'static,
Handler::Body : 'static Handler::Body : 'static
@ -396,10 +396,10 @@ macro_rules! implDrawResourceRoutes {
self.0.put(&self.1) self.0.put(&self.1)
.extend_route_matcher(accept_matcher) .extend_route_matcher(accept_matcher)
.extend_route_matcher(content_matcher) .extend_route_matcher(content_matcher)
.to(|state| update_all_handler::<Handler>(state)); .to(|state| change_all_handler::<Handler>(state));
} }
fn update<Handler : ResourceUpdate>(&mut self) fn change<Handler : ResourceChange>(&mut self)
where where
Handler::Res : Send + 'static, Handler::Res : Send + 'static,
Handler::Body : 'static Handler::Body : 'static
@ -410,24 +410,24 @@ macro_rules! implDrawResourceRoutes {
.extend_route_matcher(accept_matcher) .extend_route_matcher(accept_matcher)
.extend_route_matcher(content_matcher) .extend_route_matcher(content_matcher)
.with_path_extractor::<PathExtractor<Handler::ID>>() .with_path_extractor::<PathExtractor<Handler::ID>>()
.to(|state| update_handler::<Handler>(state)); .to(|state| change_handler::<Handler>(state));
} }
fn delete_all<Handler : ResourceDeleteAll>(&mut self) fn remove_all<Handler : ResourceRemoveAll>(&mut self)
{ {
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into(); let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
self.0.delete(&self.1) self.0.delete(&self.1)
.extend_route_matcher(matcher) .extend_route_matcher(matcher)
.to(|state| delete_all_handler::<Handler>(state)); .to(|state| remove_all_handler::<Handler>(state));
} }
fn delete<Handler : ResourceDelete>(&mut self) fn remove<Handler : ResourceRemove>(&mut self)
{ {
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into(); let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
self.0.delete(&format!("{}/:id", self.1)) self.0.delete(&format!("{}/:id", self.1))
.extend_route_matcher(matcher) .extend_route_matcher(matcher)
.with_path_extractor::<PathExtractor<Handler::ID>>() .with_path_extractor::<PathExtractor<Handler::ID>>()
.to(|state| delete_handler::<Handler>(state)); .to(|state| remove_handler::<Handler>(state));
} }
} }
} }

View file

@ -72,7 +72,7 @@ pub fn derive_request_body(input : TokenStream) -> TokenStream
expand_derive(input, expand_request_body) expand_derive(input, expand_request_body)
} }
#[proc_macro_derive(Resource, attributes(rest_resource))] #[proc_macro_derive(Resource, attributes(resource))]
pub fn derive_resource(input : TokenStream) -> TokenStream pub fn derive_resource(input : TokenStream) -> TokenStream
{ {
expand_derive(input, expand_resource) expand_derive(input, expand_resource)
@ -86,49 +86,49 @@ pub fn derive_resource_error(input : TokenStream) -> TokenStream
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn rest_read_all(attr : TokenStream, item : TokenStream) -> TokenStream pub fn read_all(attr : TokenStream, item : TokenStream) -> TokenStream
{ {
expand_macro(attr, item, |attr, item| expand_method(Method::ReadAll, attr, item)) expand_macro(attr, item, |attr, item| expand_method(Method::ReadAll, attr, item))
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn rest_read(attr : TokenStream, item : TokenStream) -> TokenStream pub fn read(attr : TokenStream, item : TokenStream) -> TokenStream
{ {
expand_macro(attr, item, |attr, item| expand_method(Method::Read, attr, item)) expand_macro(attr, item, |attr, item| expand_method(Method::Read, attr, item))
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn rest_search(attr : TokenStream, item : TokenStream) -> TokenStream pub fn search(attr : TokenStream, item : TokenStream) -> TokenStream
{ {
expand_macro(attr, item, |attr, item| expand_method(Method::Search, attr, item)) expand_macro(attr, item, |attr, item| expand_method(Method::Search, attr, item))
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn rest_create(attr : TokenStream, item : TokenStream) -> TokenStream pub fn create(attr : TokenStream, item : TokenStream) -> TokenStream
{ {
expand_macro(attr, item, |attr, item| expand_method(Method::Create, attr, item)) expand_macro(attr, item, |attr, item| expand_method(Method::Create, attr, item))
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn rest_update_all(attr : TokenStream, item : TokenStream) -> TokenStream pub fn change_all(attr : TokenStream, item : TokenStream) -> TokenStream
{ {
expand_macro(attr, item, |attr, item| expand_method(Method::UpdateAll, attr, item)) expand_macro(attr, item, |attr, item| expand_method(Method::ChangeAll, attr, item))
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn rest_update(attr : TokenStream, item : TokenStream) -> TokenStream pub fn change(attr : TokenStream, item : TokenStream) -> TokenStream
{ {
expand_macro(attr, item, |attr, item| expand_method(Method::Update, attr, item)) expand_macro(attr, item, |attr, item| expand_method(Method::Change, attr, item))
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn rest_delete_all(attr : TokenStream, item : TokenStream) -> TokenStream pub fn delete_all(attr : TokenStream, item : TokenStream) -> TokenStream
{ {
expand_macro(attr, item, |attr, item| expand_method(Method::DeleteAll, attr, item)) expand_macro(attr, item, |attr, item| expand_method(Method::RemoveAll, attr, item))
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn rest_delete(attr : TokenStream, item : TokenStream) -> TokenStream pub fn delete(attr : TokenStream, item : TokenStream) -> TokenStream
{ {
expand_macro(attr, item, |attr, item| expand_method(Method::Delete, attr, item)) expand_macro(attr, item, |attr, item| expand_method(Method::Remove, attr, item))
} }

View file

@ -26,10 +26,10 @@ pub enum Method
Read, Read,
Search, Search,
Create, Create,
UpdateAll, ChangeAll,
Update, Change,
DeleteAll, RemoveAll,
Delete Remove
} }
impl FromStr for Method impl FromStr for Method
@ -43,10 +43,10 @@ impl FromStr for Method
"Read" | "read" => Ok(Self::Read), "Read" | "read" => Ok(Self::Read),
"Search" | "search" => Ok(Self::Search), "Search" | "search" => Ok(Self::Search),
"Create" | "create" => Ok(Self::Create), "Create" | "create" => Ok(Self::Create),
"UpdateAll" | "update_all" => Ok(Self::UpdateAll), "ChangeAll" | "change_all" => Ok(Self::ChangeAll),
"Update" | "update" => Ok(Self::Update), "Change" | "change" => Ok(Self::Change),
"DeleteAll" | "delete_all" => Ok(Self::DeleteAll), "RemoveAll" | "remove_all" => Ok(Self::RemoveAll),
"Delete" | "delete" => Ok(Self::Delete), "Remove" | "remove" => Ok(Self::Remove),
_ => Err(Error::new(Span::call_site(), format!("Unknown method: `{}'", str))) _ => Err(Error::new(Span::call_site(), format!("Unknown method: `{}'", str)))
} }
} }
@ -59,14 +59,11 @@ impl Method
use Method::*; use Method::*;
match self { match self {
ReadAll => vec![], ReadAll | RemoveAll => vec![],
Read => vec!["ID"], Read | Remove => vec!["ID"],
Search => vec!["Query"], Search => vec!["Query"],
Create => vec!["Body"], Create | ChangeAll => vec!["Body"],
UpdateAll => vec!["Body"], Change => vec!["ID", "Body"]
Update => vec!["ID", "Body"],
DeleteAll => vec![],
Delete => vec!["ID"]
} }
} }
@ -79,10 +76,10 @@ impl Method
Read => "Read", Read => "Read",
Search => "Search", Search => "Search",
Create => "Create", Create => "Create",
UpdateAll => "UpdateAll", ChangeAll => "ChangeAll",
Update => "Update", Change => "Change",
DeleteAll => "DeleteAll", RemoveAll => "RemoveAll",
Delete => "Delete" Remove => "Remove"
}; };
format_ident!("Resource{}", name) format_ident!("Resource{}", name)
} }
@ -96,10 +93,10 @@ impl Method
Read => "read", Read => "read",
Search => "search", Search => "search",
Create => "create", Create => "create",
UpdateAll => "update_all", ChangeAll => "change_all",
Update => "update", Change => "change",
DeleteAll => "delete_all", RemoveAll => "remove_all",
Delete => "delete" Remove => "remove"
}; };
format_ident!("{}", name) format_ident!("{}", name)
} }

View file

@ -32,7 +32,7 @@ pub fn expand_resource(input : DeriveInput) -> Result<TokenStream>
let name = ident.to_string(); let name = ident.to_string();
let methods = input.attrs.into_iter().filter(|attr| let methods = input.attrs.into_iter().filter(|attr|
attr.path.segments.iter().last().map(|segment| segment.ident.to_string()) == Some("rest_resource".to_string()) // TODO wtf attr.path.segments.iter().last().map(|segment| segment.ident.to_string()) == Some("resource".to_string()) // TODO wtf
).map(|attr| { ).map(|attr| {
syn::parse2(attr.tokens).map(|m : MethodList| m.0.into_iter()) syn::parse2(attr.tokens).map(|m : MethodList| m.0.into_iter())
}).flat_map(|list| match list { }).flat_map(|list| match list {