use crate::assets::PostAsset; use regex::Regex; const ACTION_REGEX: &str = r"/{0,1}(?P\w*)/(?P.+)"; pub enum Router { NotFound, Index, Post { page: String }, } pub fn blog_post_exists(name: &str) -> bool { PostAsset::iter().any(|x| name.eq(&x.to_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", }; // this 7 means the "/posts/" from the full path let trimmed_path: String = path.chars().skip(7).collect(); if action.eq("posts") && !blog_post_exists(&trimmed_path) { return Router::NotFound; } match action { "posts" => Router::Post { page: caps.unwrap()["id"].to_string(), }, "index" => Router::Index, _ => Router::NotFound, } } }