From d2275df6ff0f82f7c6045032effbbf3ee2cf2c5a Mon Sep 17 00:00:00 2001 From: Dominic Date: Mon, 30 Sep 2019 20:10:51 +0200 Subject: [PATCH] have the setup method be a macro arg for now --- examples/users.rs | 20 +++++++------------- src/helper.rs | 7 ++----- src/resource.rs | 5 ++++- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/examples/users.rs b/examples/users.rs index f21d90b..ed3c4d6 100644 --- a/examples/users.rs +++ b/examples/users.rs @@ -15,7 +15,13 @@ use log4rs::{ encode::pattern::PatternEncoder }; -rest_resource!{Users} +rest_resource!{Users, route => { + route.read_all::(); + route.read::(); + route.create::(); + route.update_all::(); + route.update::(); +}} rest_struct!{User { username : String @@ -87,18 +93,6 @@ impl ResourceDelete> for Users } } -// impl Resource for Users -// { -// fn setup(mut route : D) -// { -// route.read_all::(); -// route.read::(); -// route.create::(); -// route.update_all::(); -// route.update::(); -// } -// } - const ADDR : &str = "127.0.0.1:18080"; fn main() diff --git a/src/helper.rs b/src/helper.rs index 7b7996b..44d606b 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -12,7 +12,7 @@ macro_rules! rest_struct { #[macro_export] macro_rules! rest_resource { - ($res_name:ident) => { + ($res_name:ident, $route:ident => $setup:block) => { struct $res_name; impl ::gotham_restful::Resource for $res_name @@ -22,10 +22,7 @@ macro_rules! rest_resource { stringify!($res_name).to_string() } - fn setup(mut route : D) - { - unimplemented!(); - } + fn setup(mut $route : D) $setup } } } diff --git a/src/resource.rs b/src/resource.rs index f8e183c..f82947c 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -7,8 +7,11 @@ use std::panic::RefUnwindSafe; /// allow you to register the different methods for this Resource. pub trait Resource { + /// The name of this resource. Must be unique. 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. fn setup(route : D); }