A static site generator written in Rust
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

post.rs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use std::fs;
  2. use std::path;
  3. use regex::Regex;
  4. #[derive(Debug)]
  5. pub struct Post {
  6. pub title: String,
  7. pub body: String,
  8. pub slug: String,
  9. }
  10. pub fn read_posts_dir(cwd: &path::PathBuf) -> fs::ReadDir {
  11. match fs::read_dir(cwd) {
  12. Ok(posts) => posts,
  13. Err(err) => panic!(err),
  14. }
  15. }
  16. pub fn parse_post(path: path::PathBuf) -> Post {
  17. let contents = fs::read_to_string(&path).expect("Couldn't read post file");
  18. lazy_static! {
  19. static ref re: Regex = Regex::new(r"^# (?P<title>.*)\n\n(?P<body>.*)").unwrap();
  20. static ref slug_re: Regex = Regex::new(r"(?P<slug>\S+).md").unwrap();
  21. }
  22. let title = &re.captures(&contents).expect("Couldn't parse title")["title"];
  23. let body = &re.captures(&contents).expect("Couldn't parse body")["body"];
  24. let filename = &path.file_name().unwrap().to_str().unwrap();
  25. let slug = &slug_re.captures(filename).expect("Couldn't parse slug")["slug"];
  26. Post {
  27. title: String::from(title),
  28. body: String::from(body),
  29. slug: String::from(slug),
  30. }
  31. }
  32. mod tests {
  33. #[allow(unused_imports)]
  34. use super::*;
  35. #[allow(unused_imports)]
  36. #[allow(unused_imports)]
  37. use std::{env, fs, path};
  38. #[allow(unused_imports)]
  39. use uuid::Uuid;
  40. #[test]
  41. fn test_read_posts_dir() {
  42. let temp_dir = env::temp_dir();
  43. let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
  44. fs::create_dir(&working_dir).unwrap();
  45. env::set_current_dir(&working_dir).unwrap();
  46. let cwd = env::current_dir().unwrap();
  47. fs::create_dir(cwd.join("posts")).unwrap();
  48. let post_body = "# This is a post\n\nHere is some content that goes in the post";
  49. let mut uuids: Vec<String> = vec![];
  50. for _ in 1..11 {
  51. let uuid = String::from(Uuid::new_v4().to_string());
  52. uuids.push(uuid.clone());
  53. fs::write(
  54. cwd.join("posts").join(format!("{}.md", &uuid)),
  55. &String::from(post_body),
  56. ).unwrap();
  57. }
  58. let mut expected_paths: Vec<String> = uuids
  59. .into_iter()
  60. .map(|uuid| {
  61. String::from(
  62. cwd.join("posts")
  63. .join(format!("{}.md", uuid))
  64. .to_str()
  65. .unwrap(),
  66. )
  67. }).collect();
  68. expected_paths.sort();
  69. let mut actual_paths: Vec<String> = read_posts_dir(&cwd.join("posts"))
  70. .into_iter()
  71. .map(|dir_entry| String::from(dir_entry.unwrap().path().to_str().unwrap()))
  72. .collect();
  73. actual_paths.sort();
  74. assert_eq!(expected_paths, actual_paths);
  75. fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
  76. }
  77. #[test]
  78. fn test_parse_post() {
  79. let temp_dir = env::temp_dir();
  80. let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
  81. fs::create_dir(&working_dir).unwrap();
  82. env::set_current_dir(&working_dir).unwrap();
  83. let cwd = env::current_dir().unwrap();
  84. fs::create_dir(cwd.join("posts")).unwrap();
  85. let slug = Uuid::new_v4().to_string();
  86. let filename = format!("{}.md", slug);
  87. fs::write(
  88. cwd.join("posts").join(&filename),
  89. "# This is a post\n\nHere is some content that goes in the post",
  90. ).unwrap();
  91. let post = parse_post(cwd.join("posts").join(&filename));
  92. assert_eq!("This is a post", post.title);
  93. assert_eq!("Here is some content that goes in the post", post.body);
  94. assert_eq!(slug, post.slug);
  95. fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
  96. }
  97. }