2017-12-08 01:22:26 +01:00
|
|
|
use regex::RegexSet;
|
2017-12-02 19:17:15 +01:00
|
|
|
|
2017-10-17 04:21:24 +02:00
|
|
|
pub struct RouteRecognizer<T> {
|
2017-12-05 22:31:06 +01:00
|
|
|
re: RegexSet,
|
2017-12-08 01:22:26 +01:00
|
|
|
routes: Vec<T>,
|
2017-10-17 04:21:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RouteRecognizer<T> {
|
2017-10-24 04:28:23 +02:00
|
|
|
|
2017-12-08 01:22:26 +01:00
|
|
|
pub fn new<U, K>(routes: U) -> Self
|
|
|
|
where U: IntoIterator<Item=(K, T)>, K: Into<String>,
|
2017-10-24 07:02:42 +02:00
|
|
|
{
|
2017-10-17 04:21:24 +02:00
|
|
|
let mut paths = Vec::new();
|
2017-12-08 01:22:26 +01:00
|
|
|
let mut routes = Vec::new();
|
2017-10-17 04:21:24 +02:00
|
|
|
for item in routes {
|
2017-12-08 01:22:26 +01:00
|
|
|
let pattern = parse(&item.0.into());
|
|
|
|
paths.push(pattern);
|
|
|
|
routes.push(item.1);
|
2017-10-17 04:21:24 +02:00
|
|
|
};
|
|
|
|
let regset = RegexSet::new(&paths);
|
|
|
|
|
|
|
|
RouteRecognizer {
|
2017-12-05 22:31:06 +01:00
|
|
|
re: regset.unwrap(),
|
2017-12-08 01:22:26 +01:00
|
|
|
routes: routes,
|
2017-10-17 04:21:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 01:22:26 +01:00
|
|
|
pub fn recognize(&self, path: &str) -> Option<&T> {
|
|
|
|
if path.is_empty() {
|
2017-12-05 22:31:06 +01:00
|
|
|
if let Some(idx) = self.re.matches("/").into_iter().next() {
|
2017-12-08 01:22:26 +01:00
|
|
|
return Some(&self.routes[idx])
|
2017-10-28 04:26:53 +02:00
|
|
|
}
|
2017-12-08 01:22:26 +01:00
|
|
|
} else if let Some(idx) = self.re.matches(path).into_iter().next() {
|
|
|
|
return Some(&self.routes[idx])
|
2017-10-17 04:21:24 +02:00
|
|
|
}
|
2017-10-28 04:26:53 +02:00
|
|
|
None
|
2017-10-17 04:21:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 01:22:26 +01:00
|
|
|
fn parse(pattern: &str) -> String {
|
2017-11-04 20:33:14 +01:00
|
|
|
const DEFAULT_PATTERN: &str = "[^/]+";
|
2017-10-17 04:21:24 +02:00
|
|
|
|
|
|
|
let mut re = String::from("^/");
|
|
|
|
let mut in_param = false;
|
|
|
|
let mut in_param_pattern = false;
|
|
|
|
let mut param_name = String::new();
|
|
|
|
let mut param_pattern = String::from(DEFAULT_PATTERN);
|
|
|
|
|
|
|
|
for (index, ch) in pattern.chars().enumerate() {
|
|
|
|
// All routes must have a leading slash so its optional to have one
|
|
|
|
if index == 0 && ch == '/' {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if in_param {
|
|
|
|
// In parameter segment: `{....}`
|
|
|
|
if ch == '}' {
|
2017-12-04 22:34:55 +01:00
|
|
|
re.push_str(&format!(r"(?P<{}>{})", ¶m_name, ¶m_pattern));
|
2017-10-17 04:21:24 +02:00
|
|
|
|
|
|
|
param_name.clear();
|
|
|
|
param_pattern = String::from(DEFAULT_PATTERN);
|
|
|
|
|
|
|
|
in_param_pattern = false;
|
|
|
|
in_param = false;
|
|
|
|
} else if ch == ':' {
|
|
|
|
// The parameter name has been determined; custom pattern land
|
|
|
|
in_param_pattern = true;
|
|
|
|
param_pattern.clear();
|
|
|
|
} else if in_param_pattern {
|
|
|
|
// Ignore leading whitespace for pattern
|
|
|
|
if !(ch == ' ' && param_pattern.is_empty()) {
|
|
|
|
param_pattern.push(ch);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
param_name.push(ch);
|
|
|
|
}
|
|
|
|
} else if ch == '{' {
|
|
|
|
in_param = true;
|
|
|
|
} else {
|
|
|
|
re.push(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
re.push('$');
|
2017-12-08 01:22:26 +01:00
|
|
|
re
|
2017-10-17 04:21:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 00:48:09 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use regex::Regex;
|
|
|
|
use super::*;
|
2017-12-02 19:43:14 +01:00
|
|
|
use std::iter::FromIterator;
|
|
|
|
|
2017-12-01 04:34:33 +01:00
|
|
|
#[test]
|
|
|
|
fn test_recognizer() {
|
|
|
|
let routes = vec![
|
2017-12-05 20:43:41 +01:00
|
|
|
("/name", None, 1),
|
|
|
|
("/name/{val}", None, 2),
|
|
|
|
("/name/{val}/index.html", None, 3),
|
|
|
|
("/v{val}/{val2}/index.html", None, 4),
|
|
|
|
("/v/{tail:.*}", None, 5),
|
2017-12-01 04:34:33 +01:00
|
|
|
];
|
2017-12-07 01:26:27 +01:00
|
|
|
let rec = RouteRecognizer::new("", routes);
|
2017-12-01 04:34:33 +01:00
|
|
|
|
|
|
|
let (params, val) = rec.recognize("/name").unwrap();
|
|
|
|
assert_eq!(*val, 1);
|
|
|
|
assert!(params.unwrap().is_empty());
|
|
|
|
|
|
|
|
let (params, val) = rec.recognize("/name/value").unwrap();
|
|
|
|
assert_eq!(*val, 2);
|
|
|
|
assert!(!params.as_ref().unwrap().is_empty());
|
|
|
|
assert_eq!(params.as_ref().unwrap().get("val").unwrap(), "value");
|
2017-12-02 19:17:15 +01:00
|
|
|
assert_eq!(¶ms.as_ref().unwrap()["val"], "value");
|
2017-12-01 04:34:33 +01:00
|
|
|
|
|
|
|
let (params, val) = rec.recognize("/name/value2/index.html").unwrap();
|
|
|
|
assert_eq!(*val, 3);
|
|
|
|
assert!(!params.as_ref().unwrap().is_empty());
|
|
|
|
assert_eq!(params.as_ref().unwrap().get("val").unwrap(), "value2");
|
|
|
|
assert_eq!(params.as_ref().unwrap().by_idx(0).unwrap(), "value2");
|
|
|
|
|
|
|
|
let (params, val) = rec.recognize("/vtest/ttt/index.html").unwrap();
|
|
|
|
assert_eq!(*val, 4);
|
|
|
|
assert!(!params.as_ref().unwrap().is_empty());
|
|
|
|
assert_eq!(params.as_ref().unwrap().get("val").unwrap(), "test");
|
|
|
|
assert_eq!(params.as_ref().unwrap().get("val2").unwrap(), "ttt");
|
|
|
|
assert_eq!(params.as_ref().unwrap().by_idx(0).unwrap(), "test");
|
|
|
|
assert_eq!(params.as_ref().unwrap().by_idx(1).unwrap(), "ttt");
|
|
|
|
|
|
|
|
let (params, val) = rec.recognize("/v/blah-blah/index.html").unwrap();
|
|
|
|
assert_eq!(*val, 5);
|
|
|
|
assert!(!params.as_ref().unwrap().is_empty());
|
|
|
|
assert_eq!(params.as_ref().unwrap().get("tail").unwrap(), "blah-blah/index.html");
|
|
|
|
}
|
|
|
|
|
2017-12-01 00:48:09 +01:00
|
|
|
fn assert_parse(pattern: &str, expected_re: &str) -> Regex {
|
2017-12-05 20:43:41 +01:00
|
|
|
let (re_str, _) = parse(pattern);
|
2017-12-01 00:48:09 +01:00
|
|
|
assert_eq!(&*re_str, expected_re);
|
|
|
|
Regex::new(&re_str).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_static() {
|
|
|
|
let re = assert_parse("/", r"^/$");
|
|
|
|
assert!(re.is_match("/"));
|
|
|
|
assert!(!re.is_match("/a"));
|
|
|
|
|
|
|
|
let re = assert_parse("/name", r"^/name$");
|
|
|
|
assert!(re.is_match("/name"));
|
|
|
|
assert!(!re.is_match("/name1"));
|
|
|
|
assert!(!re.is_match("/name/"));
|
|
|
|
assert!(!re.is_match("/name~"));
|
|
|
|
|
|
|
|
let re = assert_parse("/name/", r"^/name/$");
|
|
|
|
assert!(re.is_match("/name/"));
|
|
|
|
assert!(!re.is_match("/name"));
|
|
|
|
assert!(!re.is_match("/name/gs"));
|
|
|
|
|
|
|
|
let re = assert_parse("/user/profile", r"^/user/profile$");
|
|
|
|
assert!(re.is_match("/user/profile"));
|
|
|
|
assert!(!re.is_match("/user/profile/profile"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_param() {
|
|
|
|
let re = assert_parse("/user/{id}", r"^/user/(?P<id>[^/]+)$");
|
|
|
|
assert!(re.is_match("/user/profile"));
|
|
|
|
assert!(re.is_match("/user/2345"));
|
|
|
|
assert!(!re.is_match("/user/2345/"));
|
|
|
|
assert!(!re.is_match("/user/2345/sdg"));
|
|
|
|
|
|
|
|
let captures = re.captures("/user/profile").unwrap();
|
|
|
|
assert_eq!(captures.get(1).unwrap().as_str(), "profile");
|
|
|
|
assert_eq!(captures.name("id").unwrap().as_str(), "profile");
|
|
|
|
|
|
|
|
let captures = re.captures("/user/1245125").unwrap();
|
|
|
|
assert_eq!(captures.get(1).unwrap().as_str(), "1245125");
|
|
|
|
assert_eq!(captures.name("id").unwrap().as_str(), "1245125");
|
|
|
|
|
|
|
|
let re = assert_parse(
|
|
|
|
"/v{version}/resource/{id}",
|
|
|
|
r"^/v(?P<version>[^/]+)/resource/(?P<id>[^/]+)$",
|
|
|
|
);
|
|
|
|
assert!(re.is_match("/v1/resource/320120"));
|
|
|
|
assert!(!re.is_match("/v/resource/1"));
|
|
|
|
assert!(!re.is_match("/resource"));
|
|
|
|
|
|
|
|
let captures = re.captures("/v151/resource/adahg32").unwrap();
|
|
|
|
assert_eq!(captures.get(1).unwrap().as_str(), "151");
|
|
|
|
assert_eq!(captures.name("version").unwrap().as_str(), "151");
|
|
|
|
assert_eq!(captures.name("id").unwrap().as_str(), "adahg32");
|
|
|
|
}
|
|
|
|
}
|