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-03 14:10:21 +00:00
|
|
|
pub struct ApiMethod {
|
|
|
|
pub description: &'static str,
|
|
|
|
pub parameters: Jss,
|
|
|
|
pub returns: Jss,
|
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 {
|
2018-11-03 14:10:21 +00:00
|
|
|
pub get: Option<ApiMethod>,
|
|
|
|
pub put: Option<ApiMethod>,
|
|
|
|
pub post: Option<ApiMethod>,
|
|
|
|
pub delete: Option<ApiMethod>,
|
2018-11-03 09:42:48 +00:00
|
|
|
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 14:10:21 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
get: None,
|
|
|
|
put: None,
|
|
|
|
post: None,
|
|
|
|
delete: None,
|
|
|
|
subdirs: None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(mut self, m: ApiMethod) -> Self {
|
|
|
|
self.get = Some(m);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_method(&self, components: &[&str]) -> Option<&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 14:10:21 +00:00
|
|
|
// fixme: remove - not required?
|
2018-11-01 17:10:36 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! methodinfo {
|
2018-11-03 14:10:21 +00:00
|
|
|
($($option:ident => $e:expr),*) => {{
|
|
|
|
let info = MethodInfo::new();
|
|
|
|
|
|
|
|
$(
|
|
|
|
info.$option = Some($e);
|
|
|
|
)*
|
|
|
|
|
|
|
|
info
|
|
|
|
}}
|
2018-11-01 17:10:36 +00:00
|
|
|
}
|