From d3369f819d3d761b20920282b429ea8eb4c9c25a Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 6 Dec 2018 10:23:45 +0100 Subject: [PATCH] code cleanup --- src/api/router.rs | 43 +++++++++++++++++++++++++++++++++++++------ src/api3.rs | 33 +++++++++++---------------------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/api/router.rs b/src/api/router.rs index 7d825f8e..ca53e996 100644 --- a/src/api/router.rs +++ b/src/api/router.rs @@ -4,10 +4,24 @@ use crate::api::schema::*; use serde_json::{Value}; use std::collections::HashMap; +type ApiHandlerFn = fn(Value, &ApiMethod) -> Result; + pub struct ApiMethod { pub parameters: ObjectSchema, pub returns: Schema, - pub handler: fn(Value, &ApiMethod) -> Result, + pub handler: ApiHandlerFn, +} + +impl ApiMethod { + + pub fn new(handler: ApiHandlerFn, parameters: ObjectSchema) -> Self { + Self { + parameters, + handler, + returns: Schema::Null, + } + } + } pub enum SubRoute { @@ -36,13 +50,30 @@ impl Router { } } + pub fn subdir>(mut self, subdir: S, router: Router) -> Self { + if let SubRoute::None = self.subroute { + self.subroute = SubRoute::Hash(HashMap::new()); + } + match self.subroute { + SubRoute::Hash(ref mut map) => { + map.insert(subdir.into(), router); + } + _ => panic!("unexpected subroute type"), + } + self + } + pub fn subdirs(mut self, map: HashMap) -> Self { self.subroute = SubRoute::Hash(map); self } - pub fn match_all(mut self, param_name: S, router: Router) -> Self where S: Into { - self.subroute = SubRoute::MatchAll { router: Box::new(router), param_name: param_name.into() }; + pub fn match_all>(mut self, param_name: S, router: Router) -> Self { + if let SubRoute::None = self.subroute { + self.subroute = SubRoute::MatchAll { router: Box::new(router), param_name: param_name.into() }; + } else { + panic!("unexpected subroute type"); + } self } @@ -50,17 +81,17 @@ impl Router { self.get = Some(m); self } - + pub fn put(mut self, m: ApiMethod) -> Self { self.put = Some(m); self } - + pub fn post(mut self, m: ApiMethod) -> Self { self.post = Some(m); self } - + pub fn delete(mut self, m: ApiMethod) -> Self { self.delete = Some(m); self diff --git a/src/api3.rs b/src/api3.rs index 8dd76ec7..de8d1414 100644 --- a/src/api3.rs +++ b/src/api3.rs @@ -54,27 +54,20 @@ pub fn router() -> Router { let nodeinfo = Router::new() - .get(ApiMethod { - handler: test_sync_api_handler, - parameters: ObjectSchema::new("This is a simple test.") - .optional("force", BooleanSchema::new("Test for boolean options")), - returns: Schema::Null, - }) - .subdirs({ - let mut map = HashMap::new(); - map.insert("subdir3".into(), route4); - map - }); + .get(ApiMethod::new( + test_sync_api_handler, + ObjectSchema::new("This is a simple test.") + .optional("force", BooleanSchema::new("Test for boolean options"))) + ) + .subdir("subdir3", route4); let nodes = Router::new() .match_all("node", nodeinfo); let version = Router::new() - .get(ApiMethod { - handler: get_version, - parameters: ObjectSchema::new("Proxmox Backup Server API version."), - returns: Schema::Null, - }); + .get(ApiMethod::new( + get_version, + ObjectSchema::new("Proxmox Backup Server API version."))); let route = Router::new() .get(ApiMethod { @@ -82,12 +75,8 @@ pub fn router() -> Router { parameters: ObjectSchema::new("Directory index."), returns: Schema::Null, }) - .subdirs({ - let mut map = HashMap::new(); - map.insert("version".into(), version); - map.insert("nodes".into(), nodes); - map - }); + .subdir("version", version) + .subdir("nodes", nodes); route }