From e486ff03c20ed1b172d1b9f209d4e02713cc2754 Mon Sep 17 00:00:00 2001 From: plazmoid Date: Wed, 2 Sep 2020 02:49:54 +0500 Subject: [PATCH] working macro builder --- lib/u_lib/src/client/client.rs | 2 -- lib/u_lib/src/client/network.rs | 47 +++++++++++++++++----------- lib/u_lib/src/contracts/messaging.rs | 4 --- lib/u_lib/src/contracts/mod.rs | 2 +- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/lib/u_lib/src/client/client.rs b/lib/u_lib/src/client/client.rs index 2c568fc..a6828d8 100644 --- a/lib/u_lib/src/client/client.rs +++ b/lib/u_lib/src/client/client.rs @@ -53,8 +53,6 @@ impl ClientInfo { } } -impl ToMsg for ClientInfo {} - const DEFAULT_JOBS: &[(&str, &str)] = &[ //("local ip", "ip a"), ("hostname", "hostname"), diff --git a/lib/u_lib/src/client/network.rs b/lib/u_lib/src/client/network.rs index a646894..78a037d 100644 --- a/lib/u_lib/src/client/network.rs +++ b/lib/u_lib/src/client/network.rs @@ -2,7 +2,8 @@ use crate::{ MASTER_SERVER, MASTER_PORT, contracts::*, - UResult + UResult, + UError }; use reqwest::{ Client, @@ -12,8 +13,7 @@ use reqwest::{ }; use std::{ net::Ipv4Addr, - str::FromStr, - any::Any + str::FromStr }; pub struct Paths; @@ -71,7 +71,7 @@ impl ClientHandler { pub async fn init(&self, param: &ClientInfo) -> UResult { let response: Response = self.build_post(Paths::NEW) - .json(¶m.as_message()) + .json::>(¶m.as_message()) .send() .await?; let msg = response @@ -96,33 +96,42 @@ impl ClientHandler { }*/ } +#[macro_export] +macro_rules! epilogue { + ( () ) => ( |_| async Ok(()) ); + ( $result:ty ) => { + |response: Response| async { + response + .json::>() + .await + .map(|msg| msg.into_item().into_owned()) + .map_err(|e| UError::from(e)) + } + }; +} + // build_handler![path = new, method = "post", param = ClientInfo, result = RawMsg] // param and result must impl ToMsg +#[macro_export] macro_rules! build_handler { ( path = $path:tt, method = $method:literal, - param = $param:ty, + param = $($param:ty)?, result = $result:ty ) => { impl ClientHandler { - pub async fn $path(&self, param: &$param) -> UResult<$result> { + pub async fn $path(&self $(, param: &$param)?) -> UResult<$result> { let builder = match $method { "post" => ClientHandler::build_post, "get" => ClientHandler::build_get, _ => panic!("Method '{}' is not allowed", $method) }; - let base_request = builder(self, stringify!($path)); - let request = if let Some(p) = (param as &dyn Any).downcast_ref::() { - base_request - } else { - base_request.json(¶m.as_message()) - }; + let mut request = builder(self, stringify!($path)); + request = request + $(.json::>(¶m.as_message()))?; let response = request.send().await?; - let msg = response - .json::>() - .await?; - Ok(msg.into_item().into_owned()) + ($crate::epilogue!($result)(response)).await } } @@ -135,7 +144,7 @@ macro_rules! build_handler { path = $path:tt, method = $method:literal ) => ( - build_handler!(path = $path, method = $method, param = EmptyMsg, result = EmptyMsg); + build_handler!(path = $path, method = $method, param = , result = ()); ); ( @@ -143,7 +152,7 @@ macro_rules! build_handler { method = $method:literal, param = $param:ty ) => ( - build_handler!(path = $path, method = $method, param = $param, result = EmptyMsg); + build_handler!(path = $path, method = $method, param = $param, result = ()); ); ( @@ -151,7 +160,7 @@ macro_rules! build_handler { method = $method:literal, result = $result:ty ) => ( - build_handler!(path = $path, method = $method, param = EmptyMsg, result = $result); + build_handler!(path = $path, method = $method, param = , result = $result); ); } diff --git a/lib/u_lib/src/contracts/messaging.rs b/lib/u_lib/src/contracts/messaging.rs index 8ab8182..80cb367 100644 --- a/lib/u_lib/src/contracts/messaging.rs +++ b/lib/u_lib/src/contracts/messaging.rs @@ -55,10 +55,6 @@ where I: Clone { pub struct RawMsg(pub String); -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct EmptyMsg; - - /* #[cfg(test)] mod tests { diff --git a/lib/u_lib/src/contracts/mod.rs b/lib/u_lib/src/contracts/mod.rs index 4996df4..369798a 100644 --- a/lib/u_lib/src/contracts/mod.rs +++ b/lib/u_lib/src/contracts/mod.rs @@ -34,4 +34,4 @@ macro_rules! to_message { } } -to_message!(ClientInfo, RawMsg, EmptyMsg); \ No newline at end of file +to_message!(ClientInfo, RawMsg); \ No newline at end of file