From bfcba4fd8694854ed31042e9a6a8755c901688b7 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 1 Nov 2018 15:41:08 +0100 Subject: [PATCH] new subdirmap macro, cleanups --- src/api_info.rs | 12 ++++++++++++ src/json_schema.rs | 3 +-- src/main.rs | 29 +++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/api_info.rs b/src/api_info.rs index a0049f84..634df265 100644 --- a/src/api_info.rs +++ b/src/api_info.rs @@ -3,6 +3,7 @@ use failure::*; use crate::json_schema::*; use serde_json::{Value}; +#[derive(Debug)] pub struct ApiMethod<'a> { pub description: &'a str, pub properties: &'a PropertyMap<'a>, @@ -12,6 +13,17 @@ pub struct ApiMethod<'a> { pub type SubdirMap<'a> = crate::static_map::StaticMap<'a, &'a str, &'a MethodInfo<'a>>; +#[macro_export] +macro_rules! subdirmap { + ($($name:ident => $e:expr),*) => {{ + SubdirMap { + entries: &[ + $( ( stringify!($name), $e), )* + ] + } + }} +} +#[derive(Debug)] pub struct MethodInfo<'a> { pub get: Option<&'a ApiMethod<'a>>, pub put: Option<&'a ApiMethod<'a>>, diff --git a/src/json_schema.rs b/src/json_schema.rs index a051440f..da292c97 100644 --- a/src/json_schema.rs +++ b/src/json_schema.rs @@ -1,6 +1,6 @@ use crate::static_map::StaticMap; -pub type PropertyMap<'a> = StaticMap<'a, &'a str, Jss<'a>>; +pub type PropertyMap<'a> = StaticMap<'a, &'a str, Jss<'a>>; #[derive(Debug)] pub struct JssBoolean<'a> { @@ -131,4 +131,3 @@ pub static PVE_VMID: Jss = Integer!{ description => "The (unique) ID of the VM.", minimum => Some(1) }; - diff --git a/src/main.rs b/src/main.rs index 4b462b43..4170d6d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,7 +45,7 @@ static PARAMETERS1: PropertyMap = PropertyMap { ("myobject", Object!{ description => "TEST Object.", properties => &PropertyMap { - entries: &[ + entries: &[ ("vmid", Jss::Reference { reference: &PVE_VMID}), ("loop", Integer!{ description => "Totally useless thing.", @@ -74,7 +74,7 @@ fn test_api_handler(param: Value) -> Result { //} let _force = param["force"].as_bool() - .ok_or_else(|| format_err!("meine fehlermeldung"))?; + .ok_or_else(|| format_err!("missing parameter 'force'"))?; if let Some(_force) = param["force"].as_bool() { } @@ -100,6 +100,10 @@ static TEST_API_METHOD: ApiMethod = ApiMethod { }; +static API3_TEST: MethodInfo = MethodInfo { + ..METHOD_INFO_DEFAULTS +}; + static API3_NODES: MethodInfo = MethodInfo { get: Some(&TEST_API_METHOD), ..METHOD_INFO_DEFAULTS @@ -107,10 +111,9 @@ static API3_NODES: MethodInfo = MethodInfo { static API_ROOT: MethodInfo = MethodInfo { get: Some(&TEST_API_METHOD), - subdirs: Some(&SubdirMap { - entries: &[ - ("nodes", &API3_NODES), - ] + subdirs: Some(&subdirmap!{ + test => &API3_TEST, + nodes => &API3_NODES }), ..METHOD_INFO_DEFAULTS }; @@ -166,7 +169,7 @@ fn handle_request(req: Request) -> Response { &Method::DELETE => info.delete, _ => None, }; - let _api_method = match api_method_opt { + let api_method = match api_method_opt { Some(m) => m, _ => http_error!(NOT_FOUND, format!("No such method '{} {}'\n", method, path)), }; @@ -174,11 +177,21 @@ fn handle_request(req: Request) -> Response { // handle auth // extract param - let _param = match query { + let param = match query { Some(data) => parse_query(data), None => json!({}), }; + match (api_method.handler)(param) { + Ok(res) => { + let json_str = res.to_string(); + return Response::new(Body::from(json_str)); + } + Err(err) => { + http_error!(NOT_FOUND, format!("Method returned error '{}'\n", err)); + } + } + } else { http_error!(NOT_FOUND, format!("No such path '{}'\n", path)); }