2018-10-31 09:42:14 +00:00
|
|
|
use failure::*;
|
|
|
|
|
2018-11-01 13:42:27 +00:00
|
|
|
use crate::json_schema::*;
|
2018-11-01 12:05:45 +00:00
|
|
|
use serde_json::{Value};
|
2018-10-31 09:42:14 +00:00
|
|
|
|
2018-11-01 14:41:08 +00:00
|
|
|
#[derive(Debug)]
|
2018-11-01 13:16:41 +00:00
|
|
|
pub struct ApiMethod<'a> {
|
|
|
|
pub description: &'a str,
|
|
|
|
pub properties: &'a PropertyMap<'a>,
|
|
|
|
pub returns: &'a Jss<'a>,
|
2018-10-31 09:42:14 +00:00
|
|
|
pub handler: fn(Value) -> Result<Value, Error>,
|
|
|
|
}
|
|
|
|
|
2018-11-01 13:16:41 +00:00
|
|
|
pub type SubdirMap<'a> = crate::static_map::StaticMap<'a, &'a str, &'a MethodInfo<'a>>;
|
2018-10-31 09:42:14 +00:00
|
|
|
|
2018-11-01 14:41:08 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! subdirmap {
|
|
|
|
($($name:ident => $e:expr),*) => {{
|
|
|
|
SubdirMap {
|
|
|
|
entries: &[
|
|
|
|
$( ( stringify!($name), $e), )*
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
#[derive(Debug)]
|
2018-11-01 13:16:41 +00:00
|
|
|
pub struct MethodInfo<'a> {
|
|
|
|
pub get: Option<&'a ApiMethod<'a>>,
|
|
|
|
pub put: Option<&'a ApiMethod<'a>>,
|
|
|
|
pub post: Option<&'a ApiMethod<'a>>,
|
|
|
|
pub delete: Option<&'a ApiMethod<'a>>,
|
|
|
|
pub subdirs: Option<&'a SubdirMap<'a>>,
|
2018-10-31 09:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo {
|
|
|
|
get: None,
|
|
|
|
put: None,
|
|
|
|
post: None,
|
|
|
|
delete: None,
|
|
|
|
subdirs: None,
|
|
|
|
};
|
2018-11-01 10:30:49 +00:00
|
|
|
|
2018-11-01 13:16:41 +00:00
|
|
|
pub fn find_method_info<'a>(root: &'a MethodInfo, components: &[&str]) -> Option<&'a MethodInfo<'a>> {
|
2018-11-01 10:30:49 +00:00
|
|
|
|
|
|
|
if components.len() == 0 { return Some(root); };
|
|
|
|
|
|
|
|
let (dir, rest) = (components[0], &components[1..]);
|
|
|
|
|
2018-11-01 12:05:45 +00:00
|
|
|
if let Some(ref dirmap) = root.subdirs {
|
|
|
|
if let Some(info) = dirmap.get(&dir) {
|
2018-11-01 10:30:49 +00:00
|
|
|
return find_method_info(info, rest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|