-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.rs
100 lines (89 loc) · 2.53 KB
/
post.rs
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
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use handlebars::{Handlebars, RenderError};
use serde_derive::{Deserialize, Serialize};
use serde_json::json;
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Post {
title: String,
pub created_date_time: String,
parent_dir: PathBuf,
pub dir: String,
pub file_name: String,
contents: String,
pub tags: Vec<String>,
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Header {
pub title: String,
pub date_time: String,
tags: String,
}
impl Header {
pub fn generate_url(&self) -> String {
let date = self.date_time.split_whitespace().next().unwrap();
let v: Vec<&str> = date.split('-').collect();
format!("{}/{}/{}", v[0], v[1], v[2])
}
pub fn build_tags(&self) -> Vec<String> {
self.tags
.split_whitespace()
.map(|x| x.to_string())
.collect()
}
}
impl Post {
pub fn new(parent_dir: &Path, header: &Header, file_name: String, contents: String) -> Post {
Post {
title: header.title.clone(),
created_date_time: header.date_time.clone(),
parent_dir: parent_dir.to_path_buf(),
dir: header.generate_url(),
file_name,
contents,
tags: header.build_tags(),
}
}
pub fn render(&self, hbs: &Handlebars) -> Result<(), RenderError> {
let file_dir = self.parent_dir.join(&self.dir);
fs::create_dir_all(&file_dir).unwrap();
let mut f = file_dir.join(&self.file_name);
f.set_extension("html");
let file = File::create(f).unwrap();
hbs.render_to_write(
"post",
&json!({
"parent": "layout",
"post": self}),
file,
)?;
Ok(())
}
}
#[test]
fn test_new_header() {
let header = Header {
title: String::from("test"),
date_time: String::from("aa"),
tags: String::from("hello world"),
};
let v1: Vec<&str> = header.tags.split_whitespace().collect();
assert_eq!("hello", v1[0]);
assert_eq!("world", v1[1]);
}
#[test]
fn test_new_post() {
let header = Header {
title: String::from("test"),
date_time: String::from("2019-2-25"),
tags: String::from("hello world"),
};
let post = Post::new(
Path::new("a"),
&header,
String::from("ssssss"),
String::from("sss"),
);
assert_eq!("test", post.title);
assert_eq!("world", post.tags[1]);
}