convert find_method_info function into a method
This commit is contained in:
parent
8741bc2079
commit
18671ca530
|
@ -33,6 +33,24 @@ pub struct MethodInfo<'a> {
|
||||||
pub subdirs: Option<&'a SubdirMap<'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 {
|
pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo {
|
||||||
get: None,
|
get: None,
|
||||||
put: 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
|
|
||||||
}
|
|
||||||
|
|
|
@ -154,7 +154,7 @@ fn handle_request(req: Request<Body>) -> Response<Body> {
|
||||||
http_error!(NOT_FOUND, format!("Unsupported format '{}'\n", format))
|
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");
|
println!("FOUND INFO");
|
||||||
let api_method_opt = match method {
|
let api_method_opt = match method {
|
||||||
&Method::GET => info.get,
|
&Method::GET => info.get,
|
||||||
|
|
Loading…
Reference in New Issue