From 79fa17d986ab019333a12e43983ef86c9345dd57 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 1 Nov 2018 18:10:36 +0100 Subject: [PATCH] use macro to declare static methodinfo items --- src/api_info.rs | 11 +++++++++++ src/main.rs | 26 +++++++++++++------------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/api_info.rs b/src/api_info.rs index 634df265..62047aba 100644 --- a/src/api_info.rs +++ b/src/api_info.rs @@ -23,6 +23,7 @@ macro_rules! subdirmap { } }} } + #[derive(Debug)] pub struct MethodInfo<'a> { pub get: Option<&'a ApiMethod<'a>>, @@ -40,6 +41,16 @@ pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo { subdirs: None, }; +#[macro_export] +macro_rules! methodinfo { + ($name:ident, $($option:ident => $e:expr),*) => { + static $name: MethodInfo = MethodInfo { + $( $option: Some($e), )* + ..METHOD_INFO_DEFAULTS + }; + } +} + pub fn find_method_info<'a>(root: &'a MethodInfo, components: &[&str]) -> Option<&'a MethodInfo<'a>> { if components.len() == 0 { return Some(root); }; diff --git a/src/main.rs b/src/main.rs index 4170d6d3..08336b6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,23 +100,23 @@ static TEST_API_METHOD: ApiMethod = ApiMethod { }; -static API3_TEST: MethodInfo = MethodInfo { - ..METHOD_INFO_DEFAULTS -}; +methodinfo!{ + API3_TEST, +} -static API3_NODES: MethodInfo = MethodInfo { - get: Some(&TEST_API_METHOD), - ..METHOD_INFO_DEFAULTS -}; +methodinfo!{ + API3_NODES, + get => &TEST_API_METHOD +} -static API_ROOT: MethodInfo = MethodInfo { - get: Some(&TEST_API_METHOD), - subdirs: Some(&subdirmap!{ +methodinfo!{ + API_ROOT, + get => &TEST_API_METHOD, + subdirs => &subdirmap!{ test => &API3_TEST, nodes => &API3_NODES - }), - ..METHOD_INFO_DEFAULTS -}; + } +} macro_rules! http_error { ($status:ident, $msg:expr) => {{