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

start adding helper macros

This commit is contained in:
Dominic 2019-09-30 19:19:06 +02:00
parent 3626e5360c
commit e845d996d9
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
4 changed files with 49 additions and 17 deletions

View file

@ -1,5 +1,4 @@
#[macro_use] extern crate log; #[macro_use] extern crate log;
#[macro_use] extern crate serde;
use fake::{faker::internet::en::Username, Fake}; use fake::{faker::internet::en::Username, Fake};
use gotham::{ use gotham::{
@ -16,13 +15,11 @@ use log4rs::{
encode::pattern::PatternEncoder encode::pattern::PatternEncoder
}; };
struct Users; rest_resource!{Users}
#[derive(Deserialize, Serialize)] rest_struct!{User {
struct User
{
username : String username : String
} }}
impl ResourceReadAll<Success<Vec<User>>> for Users impl ResourceReadAll<Success<Vec<User>>> for Users
{ {
@ -90,17 +87,17 @@ impl ResourceDelete<u64, Success<()>> for Users
} }
} }
impl Resource for Users // impl Resource for Users
{ // {
fn setup<D : DrawResourceRoutes>(mut route : D) // fn setup<D : DrawResourceRoutes>(mut route : D)
{ // {
route.read_all::<Self, _>(); // route.read_all::<Self, _>();
route.read::<Self, _, _>(); // route.read::<Self, _, _>();
route.create::<Self, _, _>(); // route.create::<Self, _, _>();
route.update_all::<Self, _, _>(); // route.update_all::<Self, _, _>();
route.update::<Self, _, _, _>(); // route.update::<Self, _, _, _>();
} // }
} // }
const ADDR : &str = "127.0.0.1:18080"; const ADDR : &str = "127.0.0.1:18080";

31
src/helper.rs Normal file
View file

@ -0,0 +1,31 @@
#[macro_export]
macro_rules! rest_struct {
($struct_name:ident { $($field_id:ident : $field_ty:ty),* }) => {
#[derive(serde::Deserialize, serde::Serialize)]
struct $struct_name
{
$($field_id : $field_ty),*
}
}
}
#[macro_export]
macro_rules! rest_resource {
($res_name:ident) => {
struct $res_name;
impl ::gotham_restful::Resource for $res_name
{
fn name() -> String
{
stringify!($res_name).to_string()
}
fn setup<D : ::gotham_restful::DrawResourceRoutes>(mut route : D)
{
unimplemented!();
}
}
}
}

View file

@ -3,6 +3,8 @@
pub use hyper::StatusCode; pub use hyper::StatusCode;
pub mod helper;
mod resource; mod resource;
pub use resource::{ pub use resource::{
Resource, Resource,

View file

@ -7,6 +7,8 @@ use std::panic::RefUnwindSafe;
/// allow you to register the different methods for this Resource. /// allow you to register the different methods for this Resource.
pub trait Resource pub trait Resource
{ {
fn name() -> String;
fn setup<D : DrawResourceRoutes>(route : D); fn setup<D : DrawResourceRoutes>(route : D);
} }