Преглед изворни кода

Take IO out of entry parsing

master
Dylan Baker пре 5 година
родитељ
комит
38fbd6bb67
2 измењених фајлова са 42 додато и 9 уклоњено
  1. 8
    2
      src/commands.rs
  2. 34
    7
      src/entry.rs

+ 8
- 2
src/commands.rs Прегледај датотеку

58
 
58
 
59
     let mut posts: Vec<Entry> = post_paths
59
     let mut posts: Vec<Entry> = post_paths
60
         .into_iter()
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
         .collect::<Vec<Entry>>();
66
         .collect::<Vec<Entry>>();
63
 
67
 
64
     posts.sort_by(|a, b| b.date.cmp(&a.date));
68
     posts.sort_by(|a, b| b.date.cmp(&a.date));
68
     }
72
     }
69
 
73
 
70
     for entry in page_paths.into_iter() {
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
         write_entry(&cwd, &layout_template, &page_template, &page, &config);
78
         write_entry(&cwd, &layout_template, &page_template, &page, &config);
73
     }
79
     }
74
 
80
 

+ 34
- 7
src/entry.rs Прегледај датотеку

7
 
7
 
8
 use config::Config;
8
 use config::Config;
9
 
9
 
10
-#[derive(Debug)]
10
+#[derive(Debug, PartialEq)]
11
 pub enum EntryKind {
11
 pub enum EntryKind {
12
     Page,
12
     Page,
13
     Post,
13
     Post,
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
     lazy_static! {
49
     lazy_static! {
52
         static ref re_with_date: Regex =
50
         static ref re_with_date: Regex =
53
             Regex::new(r"^# (?P<title>.*) \| (?P<date>\d{4}-\d{2}-\d{2})\n\n(?s)(?P<body>.*)")
51
             Regex::new(r"^# (?P<title>.*) \| (?P<date>\d{4}-\d{2}-\d{2})\n\n(?s)(?P<body>.*)")
171
 
169
 
172
 #[cfg(test)]
170
 #[cfg(test)]
173
 mod tests {
171
 mod tests {
174
-
175
-    use super::{render_post_listing, write_entry, write_entry_listing, Config, Entry, EntryKind};
176
     use chrono::NaiveDate;
172
     use chrono::NaiveDate;
177
-    use std::{env, fs};
178
     use uuid::Uuid;
173
     use uuid::Uuid;
179
 
174
 
175
+    use std::{env, fs, path};
176
+
177
+    use super::*;
178
+
180
     #[test]
179
     #[test]
181
     fn test_render_post() {
180
     fn test_render_post() {
182
         let config = Config {
181
         let config = Config {
351
 
350
 
352
         fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
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…
Откажи
Сачувај