use config::Config; use post::Post; use render::{render_post, render_post_listing}; use std::fs; use std::path; pub fn write_post( cwd: &path::PathBuf, layout: &str, post_template: &str, post: &Post, config: &Config, ) { match fs::create_dir(cwd.join("public").join("posts").join(&post.slug)) { Ok(_) => {} Err(err) => match err.kind() { std::io::ErrorKind::AlreadyExists => {} _ => panic!(err), }, } fs::write( cwd.join("public") .join("posts") .join(&post.slug) .join("index.html"), render_post(layout, post_template, post, config), ) .expect("Unable to write file"); } pub fn write_page( cwd: &path::PathBuf, layout: &str, post_template: &str, post: &Post, config: &Config, ) { match fs::create_dir(cwd.join("public").join(&post.slug)) { Ok(_) => {} Err(err) => match err.kind() { std::io::ErrorKind::AlreadyExists => {} _ => panic!(err), }, } fs::write( cwd.join("public").join(&post.slug).join("index.html"), render_post(layout, post_template, post, config), ) .expect("Unable to write file"); } pub fn write_post_listing( cwd: &path::PathBuf, layout: &str, post_listing_template: &str, post_item_template: &str, posts: &Vec, config: &Config, ) { fs::write( cwd.join("public").join("index.html"), render_post_listing( layout, post_listing_template, post_item_template, posts, config, ), ) .expect("Unable to write file"); } #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; #[allow(unused_imports)] use std::{env, fs}; #[allow(unused_imports)] use uuid::Uuid; #[test] fn test_write_post() { let temp_dir = env::temp_dir(); let working_dir = temp_dir.join(&Uuid::new_v4().to_string()); fs::create_dir(&working_dir).unwrap(); env::set_current_dir(&working_dir).unwrap(); let cwd = env::current_dir().unwrap(); fs::create_dir(cwd.join("public")).unwrap(); fs::create_dir(cwd.join("public").join("posts")).unwrap(); let layout = "{{ page_title }}{{ contents }}"; let post_template = "

{{ title }}

{{ body }}
"; let post = Post { title: String::from("Hello world"), body: String::from("Lorem ipsum dolor sit amet"), slug: String::from("hello-world"), }; let config = Config { site_name: "Test Site".to_string(), }; write_post(&cwd, &layout, &post_template, &post, &config); let content = fs::read_to_string( cwd.join("public") .join("posts") .join("hello-world") .join("index.html"), ) .unwrap(); assert_eq!( "Hello world | Test Site

Hello world

Lorem ipsum dolor sit amet

", content.replace("\n", "") ); fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap(); } #[test] fn test_write_post_listing() { let temp_dir = env::temp_dir(); let working_dir = temp_dir.join(&Uuid::new_v4().to_string()); fs::create_dir(&working_dir).unwrap(); env::set_current_dir(&working_dir).unwrap(); let cwd = env::current_dir().unwrap(); fs::create_dir(cwd.join("public")).unwrap(); let layout = "{{ page_title }}{{ contents }}"; let post_listing_template = ""; let post_item_template = "
  • {{ title }}
  • "; let posts = vec![ Post { title: String::from("First post"), body: String::from("lorem ipsum dolor sit amet"), slug: String::from("first-post"), }, Post { title: String::from("Second post"), body: String::from("lorem ipsum dolor sit amet"), slug: String::from("second-post"), }, Post { title: String::from("Third post"), body: String::from("lorem ipsum dolor sit amet"), slug: String::from("third-post"), }, ]; let config = Config { site_name: "Test Site".to_string(), }; write_post_listing( &cwd, &layout, &post_listing_template, &post_item_template, &posts, &config, ); assert_eq!( "Test Site", fs::read_to_string(&cwd.join("public").join("index.html")) .unwrap() .replace("\n", ""), ); fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap(); } }