From 18671ca53093a7d28b8da0155680972601ca27a5 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 2 Nov 2018 10:01:47 +0100 Subject: [PATCH] convert find_method_info function into a method --- src/api_info.rs | 33 ++++++++++++++++++--------------- src/main.rs | 2 +- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/api_info.rs b/src/api_info.rs index 62047aba..baf950a4 100644 --- a/src/api_info.rs +++ b/src/api_info.rs @@ -33,6 +33,24 @@ pub struct MethodInfo<'a> { pub subdirs: Option<&'a SubdirMap<'a>>, } +impl<'a> MethodInfo<'a> { + + pub fn find_method(&'a self, components: &[&str]) -> Option<&'a MethodInfo<'a>> { + + if components.len() == 0 { return Some(self); }; + + let (dir, rest) = (components[0], &components[1..]); + + if let Some(ref dirmap) = self.subdirs { + if let Some(info) = dirmap.get(&dir) { + return info.find_method(rest); + } + } + + None + } +} + pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo { get: None, put: None, @@ -50,18 +68,3 @@ macro_rules! methodinfo { }; } } - -pub fn find_method_info<'a>(root: &'a MethodInfo, components: &[&str]) -> Option<&'a MethodInfo<'a>> { - - if components.len() == 0 { return Some(root); }; - - 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); - } - } - - None -} diff --git a/src/main.rs b/src/main.rs index a831fb1b..735b9448 100644 --- a/src/main.rs +++ b/src/main.rs @@ -154,7 +154,7 @@ fn handle_request(req: Request) -> Response { http_error!(NOT_FOUND, format!("Unsupported format '{}'\n", format)) } - if let Some(info) = find_method_info(&API_ROOT, &components[2..]) { + if let Some(info) = API_ROOT.find_method(&components[2..]) { println!("FOUND INFO"); let api_method_opt = match method { &Method::GET => info.get,