proxmox-backup/src/api_info.rs

45 lines
1.1 KiB
Rust
Raw Normal View History

2018-10-31 09:42:14 +00:00
use failure::*;
use json_schema::*;
2018-11-01 12:05:45 +00:00
use serde_json::{Value};
2018-10-31 09:42:14 +00:00
pub struct ApiMethod {
pub description: &'static str,
pub properties: StaticPropertyMap,
pub returns: Jss,
pub handler: fn(Value) -> Result<Value, Error>,
}
2018-11-01 12:05:45 +00:00
pub type StaticSubdirMap = crate::static_map::StaticMap<'static, &'static str, &'static MethodInfo>;
2018-10-31 09:42:14 +00:00
pub struct MethodInfo {
pub get: Option<&'static ApiMethod>,
pub put: Option<&'static ApiMethod>,
pub post: Option<&'static ApiMethod>,
pub delete: Option<&'static ApiMethod>,
pub subdirs: Option<&'static StaticSubdirMap>,
}
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 12:54:04 +00:00
pub fn find_method_info<'a>(root: &'a MethodInfo, components: &[&str]) -> Option<&'a MethodInfo> {
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
}