From 10be1d290aed85d8420529de9bb76cb198bbbd4e Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 15 Nov 2018 10:57:52 +0100 Subject: [PATCH] avoid macros --- src/api_config.rs | 2 +- src/api_info.rs | 3 --- src/api_server.rs | 27 +++++---------------------- 3 files changed, 6 insertions(+), 26 deletions(-) diff --git a/src/api_config.rs b/src/api_config.rs index 3d2f1154..850a89e8 100644 --- a/src/api_config.rs +++ b/src/api_config.rs @@ -1,5 +1,5 @@ use crate::api_info::*; -use crate::json_schema::*; +//use crate::json_schema::*; use std::collections::HashMap; use std::path::{PathBuf}; diff --git a/src/api_info.rs b/src/api_info.rs index ae832f40..ee4de13d 100644 --- a/src/api_info.rs +++ b/src/api_info.rs @@ -1,10 +1,7 @@ -use std::fmt; use failure::*; use crate::json_schema::*; use serde_json::{Value}; -use hyper::{StatusCode}; - use std::collections::HashMap; pub struct ApiMethod { diff --git a/src/api_server.rs b/src/api_server.rs index 528e3a4e..6a89d960 100644 --- a/src/api_server.rs +++ b/src/api_server.rs @@ -3,7 +3,6 @@ use crate::api_info::*; use crate::api_config::*; use std::fmt; -use std::collections::HashMap; use std::path::{PathBuf}; use std::sync::Arc; @@ -19,7 +18,7 @@ use tokio_codec; //use hyper::body::Payload; use hyper::http::request::Parts; -use hyper::{Body, Request, Method, Response, StatusCode}; +use hyper::{Body, Request, Response, StatusCode}; use hyper::service::{Service, NewService}; use hyper::rt::{Future, Stream}; use hyper::header; @@ -106,21 +105,6 @@ macro_rules! http_err { }} } -macro_rules! error_response { - ($status:ident, $msg:expr) => {{ - let mut resp = Response::new(Body::from($msg)); - *resp.status_mut() = StatusCode::$status; - resp - }} -} - -macro_rules! http_error_future { - ($status:ident, $msg:expr) => {{ - let resp = error_response!($status, $msg); - return Box::new(future::ok(resp)); - }} -} - fn get_request_parameters_async( info: &'static ApiMethod, parts: Parts, @@ -203,7 +187,7 @@ fn handle_sync_api_request( .header(header::CONTENT_TYPE, "application/json") .body(Body::from(json_str))?) } - Err(err) => Ok(error_response!(BAD_REQUEST, err.to_string())) + Err(err) => Err(http_err!(BAD_REQUEST, err.to_string())) } }); @@ -274,7 +258,7 @@ pub fn handle_request(api: Arc, req: Request) -> BoxFut { for name in items { if name.is_empty() { continue; } if name.starts_with(".") { - http_error_future!(BAD_REQUEST, "Path contains illegal components.\n"); + return Box::new(future::err(http_err!(BAD_REQUEST, "Path contains illegal components.".to_string()))); } path.push('/'); path.push_str(name); @@ -291,7 +275,7 @@ pub fn handle_request(api: Arc, req: Request) -> BoxFut { if comp_len >= 2 { let format = components[1]; if format != "json" { - http_error_future!(BAD_REQUEST, format!("Unsupported output format '{}'.", format)) + return Box::new(future::err(http_err!(BAD_REQUEST, format!("Unsupported output format '{}'.", format)))) } if let Some(api_method) = api.find_method(&components[2..], method) { @@ -306,7 +290,6 @@ pub fn handle_request(api: Arc, req: Request) -> BoxFut { return handle_static_file_download(filename); } - http_error_future!(NOT_FOUND, "Path not found.") - //Box::new(ok(Response::new(Body::from("RETURN WEB GUI\n")))) + Box::new(future::err(http_err!(NOT_FOUND, "Path not found.".to_string()))) }