define macro for propertymap

This commit is contained in:
Dietmar Maurer 2018-11-02 09:44:18 +01:00
parent 79fa17d986
commit 016aa6cbab
2 changed files with 49 additions and 44 deletions

View File

@ -131,3 +131,14 @@ pub static PVE_VMID: Jss = Integer!{
description => "The (unique) ID of the VM.", description => "The (unique) ID of the VM.",
minimum => Some(1) minimum => Some(1)
}; };
#[macro_export]
macro_rules! propertymap {
($($name:ident => $e:expr),*) => {
PropertyMap {
entries: &[
$( ( stringify!($name), $e), )*
]
}
}
}

View File

@ -18,44 +18,40 @@ use hyper::{Method, Body, Request, Response, Server, StatusCode};
use hyper::rt::Future; use hyper::rt::Future;
use hyper::service::service_fn_ok; use hyper::service::service_fn_ok;
static PARAMETERS1: PropertyMap = PropertyMap { static PARAMETERS1: PropertyMap = propertymap!{
entries: &[ force => Boolean!{
("force", Boolean!{ description => "Test for boolean options."
description => "Test for boolean options." },
}), text1 => ApiString!{
("text1", ApiString!{ description => "A simple text string.",
description => "A simple text string.", min_length => Some(10),
min_length => Some(10), max_length => Some(30)
max_length => Some(30) },
}), count => Integer!{
("count", Integer!{ description => "A counter for everything.",
description => "A counter for everything.", minimum => Some(0),
minimum => Some(0), maximum => Some(10)
maximum => Some(10) },
}), myarray1 => Array!{
("myarray1", Array!{ description => "Test Array of simple integers.",
description => "Test Array of simple integers.", items => &PVE_VMID
items => &PVE_VMID },
}), myarray2 => Jss::Array(JssArray {
("myarray2", Jss::Array(JssArray { description: "Test Array of simple integers.",
description: "Test Array of simple integers.", optional: Some(false),
optional: Some(false), items: &Object!{description => "Empty Object."},
items: &Object!{description => "Empty Object."}, }),
})), myobject => Object!{
("myobject", Object!{ description => "TEST Object.",
description => "TEST Object.", properties => &propertymap!{
properties => &PropertyMap { vmid => Jss::Reference { reference: &PVE_VMID},
entries: &[ loop => Integer!{
("vmid", Jss::Reference { reference: &PVE_VMID}), description => "Totally useless thing.",
("loop", Integer!{ optional => Some(false)
description => "Totally useless thing.",
optional => Some(false)
})
]
} }
}), }
("emptyobject", Object!{description => "Empty Object."}), },
] emptyobject => Object!{description => "Empty Object."}
}; };
@ -87,13 +83,11 @@ fn test_api_handler(param: Value) -> Result<Value, Error> {
static TEST_API_METHOD: ApiMethod = ApiMethod { static TEST_API_METHOD: ApiMethod = ApiMethod {
description: "This is a simple test.", description: "This is a simple test.",
properties: &PropertyMap { properties: &propertymap!{
entries: &[ force => Boolean!{
("force", Boolean!{ optional => Some(true),
optional => Some(true), description => "Test for boolean options."
description => "Test for boolean options." }
})
]
}, },
returns: &Jss::Null, returns: &Jss::Null,
handler: test_api_handler, handler: test_api_handler,