Browse Source

Restructure entry parsing, should help with speed

master
Dylan Baker 5 years ago
parent
commit
1fee1972ea
2 changed files with 28 additions and 28 deletions
  1. 25
    25
      src/entry.rs
  2. 3
    3
      src/lib.rs

+ 25
- 25
src/entry.rs View File

@@ -50,7 +50,7 @@ pub fn read_entry_dir(cwd: &path::Path) -> Vec<fs::DirEntry> {
50 50
     }
51 51
 }
52 52
 
53
-pub fn parse_entry(contents: &str, path: &path::Path) -> Entry {
53
+pub fn parse_entry(kind: EntryKind, contents: &str, path: &path::Path) -> Entry {
54 54
     lazy_static! {
55 55
         static ref re_with_date: Regex =
56 56
             Regex::new(r"^# (?P<title>.*) \| (?P<date>\d{4}-\d{2}-\d{2})\n\n(?s)(?P<body>.*)")
@@ -61,40 +61,38 @@ pub fn parse_entry(contents: &str, path: &path::Path) -> Entry {
61 61
     }
62 62
 
63 63
     let filename = &path.file_name().unwrap().to_str().unwrap();
64
-    let slug = &slug_re
64
+    let slug = slug_re
65 65
         .captures(filename)
66
-        .expect("Couldn't parse slug from filename")["slug"];
66
+        .expect("Couldn't parse slug from filename")["slug"]
67
+        .to_string();
67 68
 
68
-    if let Some(date_string) = &re_with_date.captures(&contents) {
69
-        let title = &re_with_date
69
+    if kind == EntryKind::Post {
70
+        let captures = &re_with_date
70 71
             .captures(&contents)
71
-            .expect("Couldn't parse title")["title"];
72
-        let body = &re_with_date
73
-            .captures(&contents)
74
-            .expect("Couldn't parse title")["body"];
72
+            .expect("Couldn't parse post");
73
+        let title = captures["title"].to_string();
74
+        let body = captures["body"].to_string();
75 75
         let date = Some(
76
-            NaiveDate::parse_from_str(&date_string["date"], "%Y-%m-%d")
77
-                .expect("Couldn't parse date"),
76
+            NaiveDate::parse_from_str(&captures["date"], "%Y-%m-%d").expect("Couldn't parse date"),
78 77
         );
79 78
         Entry {
80
-            kind: EntryKind::Post,
81
-            title: String::from(title),
82
-            body: String::from(body),
83
-            slug: String::from(slug),
84
-            date: date,
79
+            kind,
80
+            title,
81
+            body,
82
+            slug,
83
+            date,
85 84
         }
86 85
     } else {
87
-        let title = &re_without_date
88
-            .captures(&contents)
89
-            .expect("Couldn't parse title")["title"];
90
-        let body = &re_without_date
86
+        let captures = &re_without_date
91 87
             .captures(&contents)
92
-            .expect("Couldn't parse title")["body"];
88
+            .expect("Couldn't parse page");
89
+        let title = captures["title"].to_string();
90
+        let body = captures["body"].to_string();
93 91
         Entry {
94
-            kind: EntryKind::Page,
95
-            title: String::from(title),
96
-            body: String::from(body),
97
-            slug: String::from(slug),
92
+            kind,
93
+            title,
94
+            body,
95
+            slug,
98 96
             date: None,
99 97
         }
100 98
     }
@@ -358,6 +356,7 @@ mod tests {
358 356
     #[test]
359 357
     fn test_parse_post_entry() {
360 358
         let post = parse_entry(
359
+            EntryKind::Post,
361 360
             "# Test Title | 2000-01-01\n\nThis is the body",
362 361
             path::PathBuf::from("posts/one.md").as_path(),
363 362
         );
@@ -372,6 +371,7 @@ mod tests {
372 371
     #[test]
373 372
     fn test_parse_page_entry() {
374 373
         let post = parse_entry(
374
+            EntryKind::Page,
375 375
             "# Test Title\n\nThis is the body",
376 376
             path::PathBuf::from("pages/one.md").as_path(),
377 377
         );

+ 3
- 3
src/lib.rs View File

@@ -17,7 +17,7 @@ use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
17 17
 use toml::Value;
18 18
 
19 19
 use config::Config;
20
-use entry::{parse_entry, read_entry_dir, write_entry, write_entry_listing, Entry};
20
+use entry::{parse_entry, read_entry_dir, write_entry, write_entry_listing, Entry, EntryKind};
21 21
 
22 22
 mod config;
23 23
 mod entry;
@@ -73,7 +73,7 @@ pub fn build(include_drafts: bool, cwd: &path::Path) {
73 73
         .map(|entry| {
74 74
             let path = entry.path();
75 75
             let contents = fs::read_to_string(&path).expect("Couldn't read post file");
76
-            parse_entry(&contents, &path)
76
+            parse_entry(EntryKind::Post, &contents, &path)
77 77
         })
78 78
         .collect::<Vec<Entry>>();
79 79
 
@@ -86,7 +86,7 @@ pub fn build(include_drafts: bool, cwd: &path::Path) {
86 86
     for entry in page_paths.into_iter() {
87 87
         let path = entry.path();
88 88
         let contents = fs::read_to_string(&path).expect("Couldn't read page file");
89
-        let page = parse_entry(&contents, &path);
89
+        let page = parse_entry(EntryKind::Page, &contents, &path);
90 90
         write_entry(&cwd, &layout_template, &page_template, &page, &config);
91 91
     }
92 92
 

Loading…
Cancel
Save