diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2022-06-11 00:00:27 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2022-06-11 00:00:27 +0200 |
commit | 4fb323f69c11557a51c7da0b2031029f63edf789 (patch) | |
tree | 0d08ce52abbe0c5d123b4051f50ec5bc386652ae /src/assets.rs | |
parent | 0e147a780e74b54afbd56ff7438077d855d5c1c2 (diff) | |
download | macroblog.rs-4fb323f69c11557a51c7da0b2031029f63edf789.tar.gz macroblog.rs-4fb323f69c11557a51c7da0b2031029f63edf789.tar.bz2 macroblog.rs-4fb323f69c11557a51c7da0b2031029f63edf789.zip |
feat: Handle 404 result
Now gracefully handle 404, so instead of just panic now it will return a
proper http 404 response.
Diffstat (limited to 'src/assets.rs')
-rw-r--r-- | src/assets.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/assets.rs b/src/assets.rs new file mode 100644 index 0000000..2c39d1b --- /dev/null +++ b/src/assets.rs @@ -0,0 +1,49 @@ +use chrono::NaiveDate; +use regex::Regex; +use rust_embed::RustEmbed; +use sailfish::TemplateOnce; +use std::cmp::{Eq, Ord, PartialEq, PartialOrd}; +use std::str; + +pub const BLOG_REGEX: &str = r"(?P<date>[\d]{4}-[\d]{2}-[\d]{2})(?P<title>[a-zA-Z0-9-_]*)"; + +#[derive(RustEmbed)] +#[folder = "content/posts/"] +pub struct PostAsset; + +#[derive(TemplateOnce)] +#[template(path = "index.html")] +pub struct IndexTemplate { + pub posts: Vec<BlogEntry>, +} + +#[derive(TemplateOnce)] +#[template(path = "post.html")] +pub struct PostTemplate { + pub content: String, + pub title: String, + pub date: String, +} + +#[derive(PartialEq, Eq, PartialOrd, Ord)] +pub struct BlogEntry { + pub title: String, + pub datetime: NaiveDate, + pub file: String, +} + +impl BlogEntry { + pub fn new(path: &String) -> BlogEntry { + let re = Regex::new(BLOG_REGEX).unwrap(); + let caps = re.captures(path).unwrap(); + let date = &caps["date"]; + let title = str::replace(&caps["title"], "_", " "); + + BlogEntry { + title: String::from(title), + file: String::from(path), + datetime: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap(), + } + } + +} |