use macro to declare static methodinfo items

This commit is contained in:
Dietmar Maurer 2018-11-01 18:10:36 +01:00
parent bfcba4fd86
commit 79fa17d986
2 changed files with 24 additions and 13 deletions

View File

@ -23,6 +23,7 @@ macro_rules! subdirmap {
} }
}} }}
} }
#[derive(Debug)] #[derive(Debug)]
pub struct MethodInfo<'a> { pub struct MethodInfo<'a> {
pub get: Option<&'a ApiMethod<'a>>, pub get: Option<&'a ApiMethod<'a>>,
@ -40,6 +41,16 @@ pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo {
subdirs: None, 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>> { pub fn find_method_info<'a>(root: &'a MethodInfo, components: &[&str]) -> Option<&'a MethodInfo<'a>> {
if components.len() == 0 { return Some(root); }; if components.len() == 0 { return Some(root); };

View File

@ -100,23 +100,23 @@ static TEST_API_METHOD: ApiMethod = ApiMethod {
}; };
static API3_TEST: MethodInfo = MethodInfo { methodinfo!{
..METHOD_INFO_DEFAULTS API3_TEST,
}; }
static API3_NODES: MethodInfo = MethodInfo { methodinfo!{
get: Some(&TEST_API_METHOD), API3_NODES,
..METHOD_INFO_DEFAULTS get => &TEST_API_METHOD
}; }
static API_ROOT: MethodInfo = MethodInfo { methodinfo!{
get: Some(&TEST_API_METHOD), API_ROOT,
subdirs: Some(&subdirmap!{ get => &TEST_API_METHOD,
subdirs => &subdirmap!{
test => &API3_TEST, test => &API3_TEST,
nodes => &API3_NODES nodes => &API3_NODES
}), }
..METHOD_INFO_DEFAULTS }
};
macro_rules! http_error { macro_rules! http_error {
($status:ident, $msg:expr) => {{ ($status:ident, $msg:expr) => {{