]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/api_info.rs
avoid macros
[proxmox-backup.git] / src / api_info.rs
index a0049f84b756f16ccf871f7e9bce07f3a6c57a0c..ee4de13df4368d6db697e9a359559d078d78cb77 100644 (file)
@@ -2,43 +2,66 @@ use failure::*;
 
 use crate::json_schema::*;
 use serde_json::{Value};
+use std::collections::HashMap;
 
-pub struct ApiMethod<'a> {
-    pub description: &'a str,
-    pub properties: &'a PropertyMap<'a>,
-    pub returns: &'a Jss<'a>,
-    pub handler: fn(Value) -> Result<Value, Error>,
+pub struct ApiMethod {
+    pub description: &'static str,
+    pub parameters: Jss,
+    pub returns: Jss,
+    pub handler: fn(Value, &ApiMethod) -> Result<Value, Error>,
 }
 
-pub type SubdirMap<'a> = crate::static_map::StaticMap<'a, &'a str, &'a MethodInfo<'a>>;
-
-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>>,
+pub struct MethodInfo {
+    pub get: Option<ApiMethod>,
+    pub put: Option<ApiMethod>,
+    pub post: Option<ApiMethod>,
+    pub delete: Option<ApiMethod>,
+    pub subdirs: Option<HashMap<String, MethodInfo>>,
 }
 
-pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo {
-    get: None,
-    put: None,
-    post: None,
-    delete: None,
-    subdirs: None,
-};
+impl MethodInfo {
+
+    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_info<'a>(root: &'a MethodInfo, components: &[&str]) -> Option<&'a MethodInfo<'a>> {
+    pub fn find_route(&self, components: &[&str]) -> Option<&MethodInfo> {
 
-    if components.len() == 0 { return Some(root); };
+        if components.len() == 0 { return Some(self); };
 
-    let (dir, rest) = (components[0], &components[1..]);
+        let (dir, rest) = (components[0], &components[1..]);
 
-    if let Some(ref dirmap) = root.subdirs {
-        if let Some(info) = dirmap.get(&dir) {
-            return find_method_info(info, rest);
+        if let Some(ref dirmap) = self.subdirs {
+            if let Some(ref info) = dirmap.get(dir) {
+                return info.find_route(rest);
+            }
         }
+
+        None
     }
+}
+
+// fixme: remove - not required?
+#[macro_export]
+macro_rules! methodinfo {
+    ($($option:ident => $e:expr),*) => {{
+        let info = MethodInfo::new();
+
+        $(
+            info.$option = Some($e);
+        )*
 
-    None
+        info
+    }}
 }