diff options
Diffstat (limited to 'src/router.rs')
-rw-r--r-- | src/router.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/router.rs b/src/router.rs new file mode 100644 index 0000000..0bba091 --- /dev/null +++ b/src/router.rs @@ -0,0 +1,27 @@ +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 + } + } +} + |