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)]
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); };

View File

@ -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) => {{