X-Git-Url: https://git.r.bdr.sh/rbdr/blog/blobdiff_plain/d7fef30ac3f539975ef9edbba8e0af4a4e9ff3de..795d79afdbe5bfe5fd80902f08afdb6b9fa4db03:/src/generator/mod.rs?ds=inline diff --git a/src/generator/mod.rs b/src/generator/mod.rs index 293ad7d..55946af 100644 --- a/src/generator/mod.rs +++ b/src/generator/mod.rs @@ -4,18 +4,20 @@ mod static_files; mod txt; use crate::post::Post; -use crate::template::TemplateContext; +use crate::template::Context; use std::io::Result; -use std::path::PathBuf; +use std::path::Path; + +type Generator = fn(&Path, &Path, &Path, &Context) -> Result<()>; pub fn generate( - static_directory: &PathBuf, - template_directory: &PathBuf, - output_directory: &PathBuf, - posts: &Vec, + static_directory: &Path, + template_directory: &Path, + output_directory: &Path, + posts: &[Post], ) -> Result<()> { let generators = available_generators(); - let context = Post::to_template_context(&posts); + let context = Post::to_template_context(posts); for generator in generators { generator( static_directory, @@ -27,7 +29,7 @@ pub fn generate( Ok(()) } -fn available_generators() -> Vec Result<()>> { +fn available_generators() -> Vec { vec![ static_files::generate, // These three are actually the same. Can generalize, don't know how in rust yet. @@ -36,3 +38,150 @@ fn available_generators() -> VecGeneratorial

".to_string(), + raw: "electricity generator".to_string(), + }, + Post { + metadata: Metadata { + id: "1736045200000".to_string(), + created_on: 1_736_045_200_000, + }, + index: 10, + html: "

Generation

".to_string(), + raw: "kero kero".to_string(), + }, + ]; + + generate(&static_dir, &template_dir, &output_dir, &posts).expect("Generate failed"); + + assert_file_contents(&output_dir.join("file.txt"), "wow"); + assert_file_contents( + &output_dir.join("index.html"), + "\ +

Generatorial

+ +

Generation

+", + ); + assert_file_contents( + &output_dir.join("feed.xml"), + "\ +1736035200000 + + 1736045200000 +", + ); + } + + #[test] + fn test_fails_if_directories_do_not_exist() { + let test_dir = setup_test_dir(); + let static_dir = test_dir.join("static"); + let template_dir = test_dir.join("templates"); + let output_dir = test_dir.join("output"); + let posts = vec![]; + + let result = generate(&static_dir, &template_dir, &output_dir, &posts); + + assert!(result.is_err()); + } + + #[test] + fn test_fails_if_a_generator_fails() { + let test_dir = setup_test_dir(); + let static_dir = test_dir.join("static"); + let template_dir = test_dir.join("templates"); + let output_dir = test_dir.join("output"); + + create_dir_all(&static_dir).expect("Could not create static directory"); + create_dir_all(&template_dir).expect("Could not create template directory"); + create_dir_all(&output_dir).expect("Could not create output directory"); + create_test_file(&static_dir.join("file.txt"), "wow"); + create_test_file( + &template_dir.join("index.html"), + "\ +{{~ posts:post }} + {{ post.html }} +{{~}} +", + ); + let posts = vec![ + Post { + metadata: Metadata { + id: "1736035200000".to_string(), + created_on: 1_736_035_200_000, + }, + index: 9, + html: "

Generatorial

".to_string(), + raw: "electricity generator".to_string(), + }, + Post { + metadata: Metadata { + id: "1736045200000".to_string(), + created_on: 1_736_045_200_000, + }, + index: 10, + html: "

Generation

".to_string(), + raw: "kero kero".to_string(), + }, + ]; + + let result = generate(&static_dir, &template_dir, &output_dir, &posts); + + assert!(result.is_err()); + } +}