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)] = &[
//("local ip", "ip a"),
("hostname", "hostname"),

@ -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<RawMsg> {
let response: Response = self.build_post(Paths::NEW)
.json(&param.as_message())
.json::<Message<'_, ClientInfo>>(&param.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::<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]
// 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::<EmptyMsg>() {
base_request
} else {
base_request.json(&param.as_message())
};
let mut request = builder(self, stringify!($path));
request = request
$(.json::<Message<'_, $param>>(&param.as_message()))?;
let response = request.send().await?;
let msg = response
.json::<Message<$result>>()
.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);
);
}

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

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