2016-10-04 00:09:13 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::convert::From;
|
2016-08-23 03:34:22 +00:00
|
|
|
|
2016-10-04 00:25:27 +00:00
|
|
|
use super::Collider; // :D
|
|
|
|
|
2016-03-21 09:22:22 +00:00
|
|
|
use term_painter::ToStyle;
|
|
|
|
use term_painter::Color::*;
|
2016-08-23 03:34:22 +00:00
|
|
|
|
2016-10-04 00:09:13 +00:00
|
|
|
use codegen::StaticRouteInfo;
|
|
|
|
use handler::Handler;
|
2016-08-26 08:55:11 +00:00
|
|
|
use request::Request;
|
2016-10-04 00:09:13 +00:00
|
|
|
use http::{Method, ContentType};
|
2016-10-04 00:25:27 +00:00
|
|
|
use http::uri::{URI, URIBuf};
|
2016-03-21 04:27:17 +00:00
|
|
|
|
2016-11-04 13:35:04 +00:00
|
|
|
/// A route: a method, its handler, path, rank, and format/content type.
|
2016-03-21 04:27:17 +00:00
|
|
|
pub struct Route {
|
2016-11-04 13:35:04 +00:00
|
|
|
/// The method this route matches against.
|
2016-04-02 07:51:40 +00:00
|
|
|
pub method: Method,
|
2016-11-04 13:35:04 +00:00
|
|
|
/// A function that should be called when the route matches.
|
2016-04-03 10:36:30 +00:00
|
|
|
pub handler: Handler,
|
2016-11-04 13:35:04 +00:00
|
|
|
/// The path (in Rocket format) that should be matched against.
|
2016-04-02 07:51:40 +00:00
|
|
|
pub path: URIBuf,
|
2016-11-04 13:35:04 +00:00
|
|
|
/// The rank of this route. Lower ranks have higher priorities.
|
|
|
|
pub rank: isize,
|
|
|
|
/// The Content-Type this route matches against.
|
2016-08-23 03:34:22 +00:00
|
|
|
pub content_type: ContentType,
|
2016-03-22 05:04:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 02:03:21 +00:00
|
|
|
fn default_rank(path: &str) -> isize {
|
|
|
|
// The rank for a given path is 0 if it is a static route (it doesn't
|
|
|
|
// contain any dynamic <segmants>) or 1 if it is dynamic.
|
|
|
|
path.contains('<') as isize
|
|
|
|
}
|
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
impl Route {
|
2016-11-04 13:35:04 +00:00
|
|
|
/// Creates a new route with the method, path, and handler.
|
|
|
|
///
|
|
|
|
/// The rank of the route will be `0` if the path contains no dynamic
|
|
|
|
/// segments, and `1` if it does.
|
|
|
|
pub fn new<S>(m: Method, path: S, handler: Handler) -> Route
|
2016-09-30 22:20:11 +00:00
|
|
|
where S: AsRef<str>
|
|
|
|
{
|
2016-08-23 03:38:39 +00:00
|
|
|
Route {
|
|
|
|
method: m,
|
|
|
|
handler: handler,
|
2016-11-04 13:35:04 +00:00
|
|
|
rank: default_rank(path.as_ref()),
|
|
|
|
path: URIBuf::from(path.as_ref()),
|
2016-08-23 03:38:39 +00:00
|
|
|
content_type: ContentType::any(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 13:35:04 +00:00
|
|
|
/// Creates a new route with the given rank, method, path, and handler.
|
|
|
|
pub fn ranked<S>(rank: isize, m: Method, path: S, handler: Handler) -> Route
|
2016-09-30 22:20:11 +00:00
|
|
|
where S: AsRef<str>
|
|
|
|
{
|
2016-04-02 08:46:41 +00:00
|
|
|
Route {
|
|
|
|
method: m,
|
2016-04-03 10:36:30 +00:00
|
|
|
path: URIBuf::from(path.as_ref()),
|
2016-11-04 13:35:04 +00:00
|
|
|
handler: handler,
|
|
|
|
rank: rank,
|
2016-08-23 03:34:22 +00:00
|
|
|
content_type: ContentType::any(),
|
2016-03-21 04:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 13:35:04 +00:00
|
|
|
/// Sets the path of the route. Does not update the rank or any other
|
|
|
|
/// parameters.
|
2016-09-30 22:20:11 +00:00
|
|
|
pub fn set_path<S>(&mut self, path: S)
|
|
|
|
where S: AsRef<str>
|
|
|
|
{
|
2016-04-03 10:36:30 +00:00
|
|
|
self.path = URIBuf::from(path.as_ref());
|
2016-03-21 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 05:04:39 +00:00
|
|
|
// FIXME: Decide whether a component has to be fully variable or not. That
|
2016-03-28 09:34:09 +00:00
|
|
|
// is, whether you can have: /a<a>b/ or even /<a>:<b>/
|
2016-03-22 05:04:39 +00:00
|
|
|
// TODO: Don't return a Vec...take in an &mut [&'a str] (no alloc!)
|
2016-11-04 13:35:04 +00:00
|
|
|
/// Given a URI, returns a vector of slices of that URI corresponding to the
|
|
|
|
/// dynamic segments in this route.
|
|
|
|
#[doc(hidden)]
|
2016-08-26 08:55:11 +00:00
|
|
|
pub fn get_params<'a>(&self, uri: URI<'a>) -> Vec<&'a str> {
|
2016-10-18 02:29:58 +00:00
|
|
|
let route_segs = self.path.as_uri().segments();
|
2016-09-08 07:02:17 +00:00
|
|
|
let uri_segs = uri.segments();
|
2016-03-22 05:04:39 +00:00
|
|
|
|
2016-04-02 07:51:40 +00:00
|
|
|
let mut result = Vec::with_capacity(self.path.segment_count());
|
2016-09-08 07:02:17 +00:00
|
|
|
for (route_seg, uri_seg) in route_segs.zip(uri_segs) {
|
2016-09-30 22:20:11 +00:00
|
|
|
if route_seg.ends_with("..>") {
|
|
|
|
// FIXME: Here.
|
2016-09-08 07:02:17 +00:00
|
|
|
break;
|
2016-09-30 22:20:11 +00:00
|
|
|
} else if route_seg.ends_with('>') {
|
|
|
|
// FIXME: Here.
|
2016-04-02 07:51:40 +00:00
|
|
|
result.push(uri_seg);
|
2016-03-22 05:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
2016-03-21 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 06:20:49 +00:00
|
|
|
impl Clone for Route {
|
|
|
|
fn clone(&self) -> Route {
|
|
|
|
Route {
|
|
|
|
method: self.method,
|
|
|
|
handler: self.handler,
|
|
|
|
rank: self.rank,
|
|
|
|
path: self.path.clone(),
|
|
|
|
content_type: self.content_type.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 09:22:22 +00:00
|
|
|
impl fmt::Display for Route {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-08-27 04:34:28 +00:00
|
|
|
write!(f, "{} {}", Green.paint(&self.method), Blue.paint(&self.path))?;
|
2016-08-26 08:55:11 +00:00
|
|
|
|
2016-08-27 12:10:29 +00:00
|
|
|
if self.rank > 1 {
|
|
|
|
write!(f, " [{}]", White.paint(&self.rank))?;
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:55:11 +00:00
|
|
|
if !self.content_type.is_any() {
|
2016-08-27 04:34:28 +00:00
|
|
|
write!(f, " {}", Yellow.paint(&self.content_type))
|
2016-08-26 08:55:11 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-03-21 09:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 07:02:17 +00:00
|
|
|
impl fmt::Debug for Route {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
<Route as fmt::Display>::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 13:35:04 +00:00
|
|
|
#[doc(hidden)]
|
2016-04-03 11:25:37 +00:00
|
|
|
impl<'a> From<&'a StaticRouteInfo> for Route {
|
|
|
|
fn from(info: &'a StaticRouteInfo) -> Route {
|
2016-08-26 08:55:11 +00:00
|
|
|
let mut route = Route::new(info.method, info.path, info.handler);
|
2016-09-04 11:06:28 +00:00
|
|
|
route.content_type = info.format.clone().unwrap_or(ContentType::any());
|
2016-08-27 12:10:29 +00:00
|
|
|
if let Some(rank) = info.rank {
|
|
|
|
route.rank = rank;
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:55:11 +00:00
|
|
|
route
|
2016-04-03 11:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
impl Collider for Route {
|
|
|
|
fn collides_with(&self, b: &Route) -> bool {
|
2016-09-08 07:02:17 +00:00
|
|
|
self.method == b.method
|
2016-08-26 08:55:11 +00:00
|
|
|
&& self.rank == b.rank
|
|
|
|
&& self.content_type.collides_with(&b.content_type)
|
2016-09-08 07:02:17 +00:00
|
|
|
&& self.path.as_uri().collides_with(&b.path.as_uri())
|
2016-03-21 09:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-26 08:55:11 +00:00
|
|
|
|
2016-10-08 06:20:49 +00:00
|
|
|
impl Collider<Request> for Route {
|
2016-08-26 08:55:11 +00:00
|
|
|
fn collides_with(&self, req: &Request) -> bool {
|
|
|
|
self.method == req.method
|
2016-10-05 02:26:33 +00:00
|
|
|
&& req.uri().collides_with(&self.path.as_uri())
|
2016-10-13 02:08:19 +00:00
|
|
|
&& req.content_type().collides_with(&self.content_type)
|
2016-08-26 08:55:11 +00:00
|
|
|
}
|
|
|
|
}
|