working macro builder

4-update-check
plazmoid 4 years ago
parent 6ad5c306e4
commit e486ff03c2
  1. 2
      lib/u_lib/src/client/client.rs
  2. 47
      lib/u_lib/src/client/network.rs
  3. 4
      lib/u_lib/src/contracts/messaging.rs
  4. 2
      lib/u_lib/src/contracts/mod.rs

@ -53,8 +53,6 @@ impl ClientInfo {
} }
} }
impl ToMsg for ClientInfo {}
const DEFAULT_JOBS: &[(&str, &str)] = &[ const DEFAULT_JOBS: &[(&str, &str)] = &[
//("local ip", "ip a"), //("local ip", "ip a"),
("hostname", "hostname"), ("hostname", "hostname"),

@ -2,7 +2,8 @@ use crate::{
MASTER_SERVER, MASTER_SERVER,
MASTER_PORT, MASTER_PORT,
contracts::*, contracts::*,
UResult UResult,
UError
}; };
use reqwest::{ use reqwest::{
Client, Client,
@ -12,8 +13,7 @@ use reqwest::{
}; };
use std::{ use std::{
net::Ipv4Addr, net::Ipv4Addr,
str::FromStr, str::FromStr
any::Any
}; };
pub struct Paths; pub struct Paths;
@ -71,7 +71,7 @@ impl ClientHandler {
pub async fn init(&self, param: &ClientInfo) -> UResult<RawMsg> { pub async fn init(&self, param: &ClientInfo) -> UResult<RawMsg> {
let response: Response = self.build_post(Paths::NEW) let response: Response = self.build_post(Paths::NEW)
.json(&param.as_message()) .json::<Message<'_, ClientInfo>>(&param.as_message())
.send() .send()
.await?; .await?;
let msg = response let msg = response
@ -96,33 +96,42 @@ impl ClientHandler {
}*/ }*/
} }
#[macro_export]
macro_rules! epilogue {
( () ) => ( |_| async Ok(()) );
( $result:ty ) => {
|response: Response| async {
response
.json::<Message<$result>>()
.await
.map(|msg| msg.into_item().into_owned())
.map_err(|e| UError::from(e))
}
};
}
// build_handler![path = new, method = "post", param = ClientInfo, result = RawMsg] // build_handler![path = new, method = "post", param = ClientInfo, result = RawMsg]
// param and result must impl ToMsg // param and result must impl ToMsg
#[macro_export]
macro_rules! build_handler { macro_rules! build_handler {
( (
path = $path:tt, path = $path:tt,
method = $method:literal, method = $method:literal,
param = $param:ty, param = $($param:ty)?,
result = $result: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 $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", $method)
}; };
let base_request = builder(self, stringify!($path)); let mut request = builder(self, stringify!($path));
let request = if let Some(p) = (param as &dyn Any).downcast_ref::<EmptyMsg>() { request = request
base_request $(.json::<Message<'_, $param>>(&param.as_message()))?;
} else {
base_request.json(&param.as_message())
};
let response = request.send().await?; let response = request.send().await?;
let msg = response ($crate::epilogue!($result)(response)).await
.json::<Message<$result>>()
.await?;
Ok(msg.into_item().into_owned())
} }
} }
@ -135,7 +144,7 @@ macro_rules! build_handler {
path = $path:tt, path = $path:tt,
method = $method:literal 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, method = $method:literal,
param = $param:ty 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, method = $method:literal,
result = $result:ty result = $result:ty
) => ( ) => (
build_handler!(path = $path, method = $method, param = EmptyMsg, result = $result); build_handler!(path = $path, method = $method, param = , result = $result);
); );
} }

@ -55,10 +55,6 @@ where I: Clone {
pub struct RawMsg(pub String); pub struct RawMsg(pub String);
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EmptyMsg;
/* /*
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

@ -34,4 +34,4 @@ macro_rules! to_message {
} }
} }
to_message!(ClientInfo, RawMsg, EmptyMsg); to_message!(ClientInfo, RawMsg);
Loading…
Cancel
Save