diff --git a/src/api3.rs b/src/api3.rs index 20179476..ecf2fcae 100644 --- a/src/api3.rs +++ b/src/api3.rs @@ -24,9 +24,24 @@ fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result Result { + println!("This is a subdir {}", param); + + Ok(json!(null)) +} + pub fn router() -> Router { - let route = Router::new() + let route3 = Router::new() + .get(ApiMethod { + handler: test_subdir_api_handler, + description: "Another Endpoint.", + parameters: parameter!{}, + returns: Jss::Null, + }); + + let route2 = Router::new() .get(ApiMethod { handler: test_sync_api_handler, description: "This is a simple test.", @@ -37,7 +52,15 @@ pub fn router() -> Router { } }, returns: Jss::Null, + }) + .subdirs({ + let mut map = HashMap::new(); + map.insert("subdir3".into(), route3); + map }); + let route = Router::new() + .match_all(route2); + route } diff --git a/src/api_info.rs b/src/api_info.rs index f2ee1c6d..efa537cc 100644 --- a/src/api_info.rs +++ b/src/api_info.rs @@ -11,12 +11,18 @@ pub struct ApiMethod { pub handler: fn(Value, &ApiMethod) -> Result, } +pub enum SubRoute { + None, + Hash(HashMap), + MatchAll { router: Box, param_name: String }, +} + pub struct Router { pub get: Option, pub put: Option, pub post: Option, pub delete: Option, - pub subdirs: Option>, + pub subroute: SubRoute, } impl Router { @@ -27,10 +33,20 @@ impl Router { put: None, post: None, delete: None, - subdirs: None + subroute: SubRoute::None } } + pub fn subdirs(mut self, map: HashMap) -> Self { + self.subroute = SubRoute::Hash(map); + self + } + + pub fn match_all(mut self, router: Router) -> Self { + self.subroute = SubRoute::MatchAll { router: Box::new(router), param_name: "test".into() }; + self + } + pub fn get(mut self, m: ApiMethod) -> Self { self.get = Some(m); self @@ -42,10 +58,18 @@ impl Router { let (dir, rest) = (components[0], &components[1..]); - if let Some(ref dirmap) = self.subdirs { - if let Some(ref info) = dirmap.get(dir) { - return info.find_route(rest); + match self.subroute { + SubRoute::None => {}, + SubRoute::Hash(ref dirmap) => { + if let Some(ref router) = dirmap.get(dir) { + println!("FOUND SUBDIR {}", dir); + return router.find_route(rest); + } } + SubRoute::MatchAll { ref router, ref param_name } => { + println!("URI PARAM {} = {}", param_name, dir); // fixme: store somewhere + return router.find_route(rest); + }, } None diff --git a/src/json_schema.rs b/src/json_schema.rs index 7bbd945f..8d07c705 100644 --- a/src/json_schema.rs +++ b/src/json_schema.rs @@ -140,6 +140,7 @@ macro_rules! ApiString { }} } + #[macro_export] macro_rules! parameter { ($($name:ident => $e:expr),*) => {{