2016-03-21 09:22:22 +00:00
|
|
|
use term_painter::ToStyle;
|
|
|
|
use term_painter::Color::*;
|
2016-03-21 04:27:17 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-03-21 09:22:22 +00:00
|
|
|
use std::fmt;
|
2016-03-21 04:27:17 +00:00
|
|
|
use method::Method;
|
|
|
|
use super::Collider; // :D
|
2016-03-22 05:04:39 +00:00
|
|
|
use std::path::Component;
|
|
|
|
use Handler;
|
2016-03-21 04:27:17 +00:00
|
|
|
|
2016-03-21 09:22:22 +00:00
|
|
|
// FIXME: Take in the handler! Or maybe keep that in `Router`?
|
2016-03-21 04:27:17 +00:00
|
|
|
pub struct Route {
|
|
|
|
method: Method,
|
|
|
|
n_components: usize,
|
2016-03-22 05:04:39 +00:00
|
|
|
pub handler: Handler<'static>,
|
2016-03-21 04:27:17 +00:00
|
|
|
path: PathBuf
|
|
|
|
}
|
|
|
|
|
2016-03-22 05:04:39 +00:00
|
|
|
macro_rules! comp_to_str {
|
|
|
|
($component:expr) => (
|
|
|
|
match $component {
|
|
|
|
&Component::Normal(ref comp) => {
|
|
|
|
if let Some(string) = comp.to_str() { string }
|
|
|
|
else { panic!("Whoops, no string!") }
|
|
|
|
},
|
|
|
|
&Component::RootDir => "/",
|
|
|
|
&c@_ => panic!("Whoops, not normal: {:?}!", c)
|
|
|
|
};
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
impl Route {
|
2016-03-22 05:04:39 +00:00
|
|
|
pub fn new(m: Method, mount: &'static str, route: &'static str,
|
|
|
|
handler: Handler<'static>) -> Route {
|
2016-03-21 04:27:17 +00:00
|
|
|
let deduped_path = Route::dedup(mount, route);
|
|
|
|
let path = PathBuf::from(deduped_path);
|
|
|
|
|
|
|
|
Route {
|
|
|
|
method: m,
|
|
|
|
n_components: path.components().count(),
|
2016-03-22 05:04:39 +00:00
|
|
|
handler: handler,
|
|
|
|
path: path,
|
2016-03-21 04:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 05:04:39 +00:00
|
|
|
#[inline]
|
2016-03-21 04:27:17 +00:00
|
|
|
pub fn component_count(&self) -> usize {
|
|
|
|
self.n_components
|
|
|
|
}
|
|
|
|
|
2016-03-22 05:04:39 +00:00
|
|
|
// FIXME: This is dirty (the comp_to_str and the RootDir thing). Might need
|
|
|
|
// to have my own wrapper arround path strings.
|
|
|
|
// FIXME: Decide whether a component has to be fully variable or not. That
|
|
|
|
// is, whether you can have: /a<a>b/
|
|
|
|
// TODO: Don't return a Vec...take in an &mut [&'a str] (no alloc!)
|
|
|
|
pub fn get_params<'a>(&self, uri: &'a str) -> Vec<&'a str> {
|
|
|
|
let mut result = Vec::with_capacity(self.component_count());
|
|
|
|
let route_components = self.path.components();
|
|
|
|
let uri_components = Path::new(uri).components();
|
|
|
|
|
|
|
|
for (route_comp, uri_comp) in route_components.zip(uri_components) {
|
|
|
|
if comp_to_str!(&route_comp).starts_with("<") {
|
|
|
|
result.push(comp_to_str!(&uri_comp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
fn dedup(base: &'static str, route: &'static str) -> String {
|
|
|
|
let mut deduped = String::with_capacity(base.len() + route.len() + 1);
|
|
|
|
|
|
|
|
let mut last = '\0';
|
|
|
|
for c in base.chars().chain("/".chars()).chain(route.chars()) {
|
|
|
|
if !(last == '/' && c == '/') {
|
|
|
|
deduped.push(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
last = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
deduped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 09:22:22 +00:00
|
|
|
impl fmt::Display for Route {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{} {:?}", Green.paint(&self.method), Blue.paint(&self.path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
impl Collider for Path {
|
2016-03-22 05:04:39 +00:00
|
|
|
// TODO: It's expensive to compute the number of components: O(n) per path
|
2016-03-21 09:22:22 +00:00
|
|
|
// where n == number of chars.
|
2016-03-21 04:27:17 +00:00
|
|
|
//
|
|
|
|
// Idea: Create a `CachedPath` type that caches the number of components
|
|
|
|
// similar to the way `Route` does it.
|
|
|
|
fn collides_with(&self, b: &Path) -> bool {
|
|
|
|
if self.components().count() != b.components().count() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut a_components = self.components();
|
|
|
|
let mut b_components = b.components();
|
|
|
|
while let Some(ref c1) = a_components.next() {
|
|
|
|
if let Some(ref c2) = b_components.next() {
|
|
|
|
if !c1.collides_with(c2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Collider for Route {
|
|
|
|
fn collides_with(&self, b: &Route) -> bool {
|
|
|
|
if self.n_components != b.n_components || self.method != b.method {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.path.collides_with(&b.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Collider<Route> for &'a str {
|
|
|
|
fn collides_with(&self, other: &Route) -> bool {
|
|
|
|
let path = Path::new(self);
|
|
|
|
path.collides_with(&other.path)
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 09:22:22 +00:00
|
|
|
|
|
|
|
impl Collider<str> for Route {
|
|
|
|
fn collides_with(&self, other: &str) -> bool {
|
|
|
|
other.collides_with(self)
|
|
|
|
}
|
|
|
|
}
|