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-03 09:42:48 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
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,
|
2018-11-03 09:42:48 +00:00
|
|
|
pub parameters: &'a Jss<'a>,
|
2018-11-01 13:16:41 +00:00
|
|
|
pub returns: &'a Jss<'a>,
|
2018-10-31 09:42:14 +00:00
|
|
|
pub handler: fn(Value) -> Result<Value, Error>,
|
|
|
|
}
|
|
|
|
|
2018-11-01 14:41:08 +00:00
|
|
|
#[derive(Debug)]
|
2018-11-03 09:42:48 +00:00
|
|
|
pub struct MethodInfo {
|
|
|
|
pub get: Option<&'static ApiMethod<'static>>,
|
|
|
|
pub put: Option<&'static ApiMethod<'static>>,
|
|
|
|
pub post: Option<&'static ApiMethod<'static>>,
|
|
|
|
pub delete: Option<&'static ApiMethod<'static>>,
|
|
|
|
pub subdirs: Option<HashMap<String, MethodInfo>>,
|
2018-10-31 09:42:14 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 09:42:48 +00:00
|
|
|
impl MethodInfo {
|
2018-11-02 09:01:47 +00:00
|
|
|
|
2018-11-03 09:42:48 +00:00
|
|
|
pub fn find_method<'a>(&'a self, components: &[&str]) -> Option<&'a MethodInfo> {
|
2018-11-02 09:01:47 +00:00
|
|
|
|
|
|
|
if components.len() == 0 { return Some(self); };
|
|
|
|
|
|
|
|
let (dir, rest) = (components[0], &components[1..]);
|
|
|
|
|
|
|
|
if let Some(ref dirmap) = self.subdirs {
|
2018-11-03 09:42:48 +00:00
|
|
|
if let Some(ref info) = dirmap.get(dir) {
|
2018-11-02 09:01:47 +00:00
|
|
|
return info.find_method(rest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 09:42:48 +00:00
|
|
|
pub const METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo {
|
2018-10-31 09:42:14 +00:00
|
|
|
get: None,
|
|
|
|
put: None,
|
|
|
|
post: None,
|
|
|
|
delete: None,
|
|
|
|
subdirs: None,
|
|
|
|
};
|
2018-11-01 10:30:49 +00:00
|
|
|
|
2018-11-01 17:10:36 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! methodinfo {
|
2018-11-03 09:42:48 +00:00
|
|
|
($($option:ident => $e:expr),*) => {
|
|
|
|
MethodInfo {
|
2018-11-01 17:10:36 +00:00
|
|
|
$( $option: Some($e), )*
|
|
|
|
..METHOD_INFO_DEFAULTS
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|