1use regex::Regex;
2
3const ACTION_REGEX: &str = r"/{0,1}(?P<action>\w*)/(?P<id>.+)";
4
5pub enum Router {
6 NotFound,
7 Index,
8 Post { page: String },
9}
10
11impl Router {
12 pub fn new(path: &str) -> Router {
13 let re = Regex::new(ACTION_REGEX).unwrap();
14 let caps = re.captures(path);
15 let action = match caps {
16 Some(ref value) => &value["action"],
17 None => "index",
18 };
19
20 match action {
21 "posts" => Router::Post {
22 page: caps.unwrap()["id"].to_string(),
23 },
24 "index" => Router::Index,
25 _ => Router::NotFound,
26 }
27 }
28}