diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/blog.rs | 32 | ||||
-rw-r--r-- | src/router.rs | 11 |
2 files changed, 20 insertions, 23 deletions
diff --git a/src/blog.rs b/src/blog.rs index 6bbda49..0fa9543 100644 --- a/src/blog.rs +++ b/src/blog.rs @@ -1,9 +1,9 @@ +use chrono::NaiveDate; +use regex::Regex; use rust_embed::RustEmbed; use sailfish::TemplateOnce; -use chrono::NaiveDate; -use regex::{Regex}; +use std::cmp::{Eq, Ord, PartialEq, PartialOrd}; use std::str; -use std::cmp::{PartialOrd, Ord, PartialEq, Eq}; const BLOG_REGEX: &str = r"(?P<date>[\d]{4}-[\d]{2}-[\d]{2})(?P<title>[a-zA-Z0-9-_]*)"; @@ -11,7 +11,6 @@ const BLOG_REGEX: &str = r"(?P<date>[\d]{4}-[\d]{2}-[\d]{2})(?P<title>[a-zA-Z0-9 #[folder = "content/posts/"] struct PostAsset; - #[derive(TemplateOnce)] #[template(path = "index.html")] struct IndexTemplate { @@ -23,7 +22,7 @@ struct IndexTemplate { struct PostTemplate { content: String, title: String, - date: String + date: String, } #[derive(PartialEq, Eq, PartialOrd, Ord)] @@ -43,12 +42,11 @@ impl BlogEntry { BlogEntry { title: String::from(title), file: String::from(path), - datetime: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap() + datetime: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap(), } } pub fn read_assets() -> Vec<BlogEntry> { - let mut entries: Vec<BlogEntry> = PostAsset::iter() .map(|e| format!("{}", e)) .map(|e| BlogEntry::new(&e)) @@ -61,29 +59,27 @@ impl BlogEntry { } fn get_file_content(path: &str) -> String { - let buffer = PostAsset::get(path) - .unwrap() - .data - .into_owned(); + let buffer = PostAsset::get(path).unwrap().data.into_owned(); return String::from_utf8(buffer).unwrap(); } - pub fn render_post_page(path: &String) -> String { let blog = BlogEntry::new(path); PostTemplate { content: get_file_content(path), title: blog.title, - date: blog.datetime.format("%Y-%m-%d").to_string() + date: blog.datetime.format("%Y-%m-%d").to_string(), } - .render_once() - .unwrap() + .render_once() + .unwrap() } pub fn render_index_page() -> String { - IndexTemplate { posts: BlogEntry::read_assets() } - .render_once() - .unwrap() + IndexTemplate { + posts: BlogEntry::read_assets(), + } + .render_once() + .unwrap() } diff --git a/src/router.rs b/src/router.rs index 0bba091..35fdf3e 100644 --- a/src/router.rs +++ b/src/router.rs @@ -1,4 +1,4 @@ -use regex::{Regex}; +use regex::Regex; const ACTION_REGEX: &str = r"/{0,1}(?P<action>\w*)/(?P<id>.+)"; @@ -14,14 +14,15 @@ impl Router { let caps = re.captures(path); let action = match caps { Some(ref value) => &value["action"], - None => "index" + None => "index", }; match action { - "posts" => Router::Post { page: caps.unwrap()["id"].to_string() }, + "posts" => Router::Post { + page: caps.unwrap()["id"].to_string(), + }, "index" => Router::Index, - _ => Router::NotFound + _ => Router::NotFound, } } } - |