aboutsummaryrefslogtreecommitdiff
path: root/src/router.rs
blob: 35fdf3e8bed96a76bda385f0242e4757bfa06553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use regex::Regex;

const ACTION_REGEX: &str = r"/{0,1}(?P<action>\w*)/(?P<id>.+)";

pub enum Router {
    NotFound,
    Index,
    Post { page: String },
}

impl Router {
    pub fn new(path: &str) -> Router {
        let re = Regex::new(ACTION_REGEX).unwrap();
        let caps = re.captures(path);
        let action = match caps {
            Some(ref value) => &value["action"],
            None => "index",
        };

        match action {
            "posts" => Router::Post {
                page: caps.unwrap()["id"].to_string(),
            },
            "index" => Router::Index,
            _ => Router::NotFound,
        }
    }
}