Browse Source

Take IO out of entry parsing

master
Dylan Baker 5 years ago
parent
commit
38fbd6bb67
2 changed files with 42 additions and 9 deletions
  1. 8
    2
      src/commands.rs
  2. 34
    7
      src/entry.rs

+ 8
- 2
src/commands.rs View File

@@ -58,7 +58,11 @@ pub fn build(include_drafts: bool) {
58 58
 
59 59
     let mut posts: Vec<Entry> = post_paths
60 60
         .into_iter()
61
-        .map(|entry| parse_entry(entry.path()))
61
+        .map(|entry| {
62
+            let path = entry.path();
63
+            let contents = fs::read_to_string(&path).expect("Couldn't read post file");
64
+            parse_entry(&contents, path)
65
+        })
62 66
         .collect::<Vec<Entry>>();
63 67
 
64 68
     posts.sort_by(|a, b| b.date.cmp(&a.date));
@@ -68,7 +72,9 @@ pub fn build(include_drafts: bool) {
68 72
     }
69 73
 
70 74
     for entry in page_paths.into_iter() {
71
-        let page = parse_entry(entry.path());
75
+        let path = entry.path();
76
+        let contents = fs::read_to_string(&path).expect("Couldn't read page file");
77
+        let page = parse_entry(&contents, path);
72 78
         write_entry(&cwd, &layout_template, &page_template, &page, &config);
73 79
     }
74 80
 

+ 34
- 7
src/entry.rs View File

@@ -7,7 +7,7 @@ use std::path;
7 7
 
8 8
 use config::Config;
9 9
 
10
-#[derive(Debug)]
10
+#[derive(Debug, PartialEq)]
11 11
 pub enum EntryKind {
12 12
     Page,
13 13
     Post,
@@ -45,9 +45,7 @@ pub fn read_entry_dir(cwd: &path::PathBuf) -> Vec<fs::DirEntry> {
45 45
     }
46 46
 }
47 47
 
48
-pub fn parse_entry(path: path::PathBuf) -> Entry {
49
-    let contents = fs::read_to_string(&path).expect("Couldn't read post file");
50
-
48
+pub fn parse_entry(contents: &str, path: path::PathBuf) -> Entry {
51 49
     lazy_static! {
52 50
         static ref re_with_date: Regex =
53 51
             Regex::new(r"^# (?P<title>.*) \| (?P<date>\d{4}-\d{2}-\d{2})\n\n(?s)(?P<body>.*)")
@@ -171,12 +169,13 @@ pub fn render_post_listing(
171 169
 
172 170
 #[cfg(test)]
173 171
 mod tests {
174
-
175
-    use super::{render_post_listing, write_entry, write_entry_listing, Config, Entry, EntryKind};
176 172
     use chrono::NaiveDate;
177
-    use std::{env, fs};
178 173
     use uuid::Uuid;
179 174
 
175
+    use std::{env, fs, path};
176
+
177
+    use super::*;
178
+
180 179
     #[test]
181 180
     fn test_render_post() {
182 181
         let config = Config {
@@ -351,4 +350,32 @@ mod tests {
351 350
 
352 351
         fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
353 352
     }
353
+
354
+    #[test]
355
+    fn test_parse_post_entry() {
356
+        let post = parse_entry(
357
+            "# Test Title | 2000-01-01\n\nThis is the body",
358
+            path::PathBuf::from("posts/one.md"),
359
+        );
360
+
361
+        assert_eq!(post.kind, EntryKind::Post);
362
+        assert_eq!(post.slug, "one");
363
+        assert_eq!(post.title, "Test Title");
364
+        assert_eq!(post.body, "This is the body");
365
+        assert_eq!(post.date, Some(NaiveDate::from_ymd(2000, 1, 1)));
366
+    }
367
+
368
+    #[test]
369
+    fn test_parse_page_entry() {
370
+        let post = parse_entry(
371
+            "# Test Title\n\nThis is the body",
372
+            path::PathBuf::from("pages/one.md"),
373
+        );
374
+
375
+        assert_eq!(post.kind, EntryKind::Page);
376
+        assert_eq!(post.slug, "one");
377
+        assert_eq!(post.title, "Test Title");
378
+        assert_eq!(post.body, "This is the body");
379
+        assert_eq!(post.date, None);
380
+    }
354 381
 }

Loading…
Cancel
Save