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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
use std::collections::HashSet;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use markdown::{mdast::Node, Constructs, Options, ParseOptions};
use rocket::http::Status;
use serde::{ser::SerializeStruct, Deserialize, Serialize};
use tokio::fs;
use tokio::io::AsyncReadExt;
use crate::{error::BlossomError, Result};
#[derive(Serialize, Debug)]
enum PostType {
Article,
Note,
}
enum TextFormat {
Markdown,
Plaintext,
Html,
}
#[derive(Debug)]
pub struct Post<D: Content> {
// id: i64,
subject: Option<String>,
created_at: DateTime<Utc>,
updated_at: Option<DateTime<Utc>>,
tags: Vec<String>,
post_type: PostType,
render: Option<String>,
data: D,
}
impl<D: Content + Serialize> Post<D> {
pub async fn render(&mut self) -> Result<()> {
self.render = Some(self.data.render().await?);
Ok(())
}
}
impl<D: Content + Serialize> Serialize for Post<D> {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_struct("Post", 7)?;
state.serialize_field("subject", &self.subject)?;
state.serialize_field("created_at", &self.created_at.to_string())?;
state.serialize_field(
"updated_at",
&self.updated_at.and_then(|time| Some(time.to_string())),
)?;
state.serialize_field("tags", &self.tags)?;
state.serialize_field("post_type", &self.post_type)?;
state.serialize_field("render", &self.render)?;
state.serialize_field("data", &self.data)?;
state.end()
}
}
pub async fn get_blogposts() -> Result<Vec<Post<Article>>> {
let mut blogposts: Vec<Post<Article>> = Vec::new();
let mut articles_dir = fs::read_dir("./articles").await?;
while let Some(file) = articles_dir.next_entry().await? {
let name = file.file_name();
let name = name.to_str().unwrap_or_default()[..name.len() - 3].to_owned();
let blogpost: Article = Article {
path: file.path().to_str().unwrap_or_default().to_owned(),
name,
};
let blogpost = Post::try_from(blogpost).await.unwrap();
blogposts.push(blogpost);
}
Ok(blogposts)
}
pub async fn get_blogpost(post_name: &str) -> Result<Post<Article>> {
let mut articles_dir = fs::read_dir("./articles").await?;
while let Some(file) = articles_dir.next_entry().await? {
let name = file.file_name();
let name = &name.to_str().unwrap_or_default()[..name.len() - 3];
if name == post_name {
let blogpost = Article {
path: file.path().to_str().unwrap_or_default().to_owned(),
name: name.to_owned(),
};
let blogpost = Post::try_from(blogpost).await.unwrap();
return Ok(blogpost);
}
}
Err(BlossomError::NotFound(Status::new(404)))
}
pub fn get_tags<D: Content>(posts: &Vec<Post<D>>) -> HashSet<String> {
posts.into_iter().fold(HashSet::new(), |mut acc, post| {
let tags = &post.tags;
for tag in tags {
acc.insert(tag.to_owned());
}
acc
})
}
#[async_trait]
pub trait Content {
async fn render(&self) -> Result<String>;
}
#[derive(Serialize, Debug)]
pub struct Article {
path: String,
name: String,
}
impl Article {
async fn tree(&self) -> Result<Node> {
let mut file = fs::File::open(&self.path).await?;
let mut buf = String::new();
file.read_to_string(&mut buf).await?;
Ok(markdown::to_mdast(
&buf,
&ParseOptions {
constructs: Constructs {
frontmatter: true,
..Constructs::default()
},
..ParseOptions::default()
},
)
.unwrap())
}
async fn frontmatter(&self) -> Result<String> {
let tree = self.tree().await?;
let children = tree.children();
if let Some(children) = children {
if let Some(toml) = children.into_iter().find_map(|el| match el {
Node::Toml(toml) => Some(toml.value.to_owned()),
_ => None,
}) {
return Ok(toml);
};
}
Err(BlossomError::NoMetadata(Status::new(500)))
}
}
#[async_trait]
impl Content for Article {
async fn render(&self) -> Result<String> {
let mut file = fs::File::open(&self.path).await?;
let mut buf = String::new();
file.read_to_string(&mut buf).await?;
let options = Options {
parse: ParseOptions {
constructs: Constructs {
frontmatter: true,
..Constructs::default()
},
..ParseOptions::default()
},
..Options::default()
};
Ok(markdown::to_html_with_options(&buf, &options).unwrap())
}
}
#[derive(Deserialize)]
struct ArticleMetadata {
title: String,
created_at: String,
updated_at: Option<String>,
tags: Vec<String>,
}
impl Post<Article> {
async fn try_from(article: Article) -> Result<Post<Article>> {
let metadata = article.frontmatter().await?;
let metadata: ArticleMetadata = toml::from_str(&metadata)?;
let updated_at = if let Some(updated_at) = metadata.updated_at {
Some(updated_at.parse::<DateTime<Utc>>()?)
} else {
None
};
Ok(Post {
subject: Some(metadata.title),
created_at: metadata.created_at.parse::<DateTime<Utc>>()?,
updated_at,
tags: metadata.tags,
post_type: PostType::Article,
render: None,
data: article,
})
}
}
|