new macro syntax

4-update-check
plazmoid 4 years ago
parent 268b7783db
commit 8e081158a5
  1. 60
      lib/u_lib/src/client/network.rs

@ -1,3 +1,5 @@
#[allow(non_upper_case_globals)]
use crate::{ use crate::{
MASTER_SERVER, MASTER_SERVER,
MASTER_PORT, MASTER_PORT,
@ -16,6 +18,7 @@ use std::{
str::FromStr str::FromStr
}; };
pub struct Paths;
#[macro_export] #[macro_export]
macro_rules! epilogue { macro_rules! epilogue {
@ -31,23 +34,16 @@ macro_rules! epilogue {
}; };
} }
// build_handler![path = new, method = "post", param = ClientInfo, result = RawMsg]
// new syntax: build_handler!(POST new(ClientInfo) -> RawMsg)
// param and result must impl ToMsg // param and result must impl ToMsg
#[macro_export] #[macro_export]
macro_rules! build_handler { macro_rules! build_handler {
( ( $method:tt $path:tt($($param:ty)?) -> $result:ty ) => {
path = $path:tt,
method = $method:literal,
param = $($param:ty)?,
result = $result:ty
) => {
impl ClientHandler { impl ClientHandler {
pub async fn $path(&self $(, param: &$param)?) -> UResult<$result> { pub async fn $path(&self $(, param: &$param)?) -> UResult<$result> {
let builder = match $method { let builder = match stringify!($method) {
"post" => ClientHandler::build_post, "POST" => ClientHandler::build_post,
"get" => ClientHandler::build_get, "GET" => ClientHandler::build_get,
_ => panic!("Method '{}' is not allowed", $method) _ => panic!("Method '{}' is not allowed", stringify!($method))
}; };
let mut request = builder(self, stringify!($path)); let mut request = builder(self, stringify!($path));
request = request request = request
@ -61,39 +57,6 @@ macro_rules! build_handler {
pub const $path: &'static str = stringify!($path); pub const $path: &'static str = stringify!($path);
} }
}; };
(
path = $path:tt,
method = $method:literal
) => (
build_handler!(path = $path, method = $method, param = , result = ());
);
(
path = $path:tt,
method = $method:literal,
param = $param:ty
) => (
build_handler!(path = $path, method = $method, param = $param, result = ());
);
(
path = $path:tt,
method = $method:literal,
result = $result:ty
) => (
build_handler!(path = $path, method = $method, param = , result = $result);
);
}
pub struct Paths;
impl Paths {
pub const LIST: &'static str = "list";
pub const NEW: &'static str = "new";
//pub const DEL: &'static str = "del";
pub const JOB_REQ: &'static str = "job_req";
pub const JOB_ANS: &'static str = "job_ans";
} }
@ -140,7 +103,6 @@ impl ClientHandler {
} }
} }
build_handler!(POST init(ClientInfo) -> RawMsg);
build_handler!(path = init, method = "post", param = ClientInfo, result = RawMsg); build_handler!(GET ls() -> Vec<ClientInfo>);
build_handler!(path = ls, method = "get", result = Vec<ClientInfo>); build_handler!(POST del() -> ());
build_handler!(path = del, method = "post");

Loading…
Cancel
Save