add static route info to Route

This commit is contained in:
Benjamin Fry 2017-06-23 11:36:04 -07:00
parent 6021609ee3
commit c368a71320
3 changed files with 11 additions and 1 deletions

View File

@ -269,6 +269,7 @@ fn generic_route_decorator(known_method: Option<Spanned<Method>>,
// function as its handler. A proper Rocket route will be created from this.
let struct_name = user_fn_name.prepend(ROUTE_STRUCT_PREFIX);
let (path, method, media_type, rank) = route.explode(ecx);
let handler_name = user_fn_name.to_string();
let static_route_info_item = quote_item!(ecx,
/// Rocket code generated static route information structure.
#[allow(non_upper_case_globals)]
@ -277,6 +278,7 @@ fn generic_route_decorator(known_method: Option<Spanned<Method>>,
method: $method,
path: $path,
handler: $route_fn_name,
handler_name: $handler_name,
format: $media_type,
rank: $rank,
};

View File

@ -6,6 +6,7 @@ pub struct StaticRouteInfo {
pub path: &'static str,
pub format: Option<MediaType>,
pub handler: Handler,
pub handler_name: &'static str,
pub rank: Option<isize>,
}

View File

@ -23,6 +23,9 @@ pub struct Route {
pub rank: isize,
/// The media type this route matches against, if any.
pub format: Option<MediaType>,
/// Static route information generated by codegen
#[doc(hidden)]
pub route_info: Option<&'static StaticRouteInfo>,
}
#[inline(always)]
@ -86,6 +89,7 @@ impl Route {
base: URI::from("/"),
uri: uri,
format: None,
route_info: None,
}
}
@ -116,6 +120,7 @@ impl Route {
uri: URI::from(uri.as_ref().to_string()),
rank: rank,
format: None,
route_info: None,
}
}
@ -227,6 +232,7 @@ impl Clone for Route {
base: self.base.clone(),
uri: self.uri.clone(),
format: self.format.clone(),
route_info: self.route_info.clone(),
}
}
}
@ -254,10 +260,11 @@ impl fmt::Debug for Route {
}
#[doc(hidden)]
impl<'a> From<&'a StaticRouteInfo> for Route {
impl<'a> From<&'a StaticRouteInfo> for Route where 'a: 'static {
fn from(info: &'a StaticRouteInfo) -> Route {
let mut route = Route::new(info.method, info.path, info.handler);
route.format = info.format.clone();
route.route_info = Some(info);
if let Some(rank) = info.rank {
route.rank = rank;
}