aboutsummaryrefslogtreecommitdiff
path: root/src/generator/mod.rs
blob: 1ba40368f1790eb763f62ea786043c317f5ed46c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
mod html;
mod rss;
mod static_files;
mod txt;

use crate::post::Post;
use crate::template::Context;
use std::io::Result;
use std::path::Path;

type Generator = fn(&Path, &Path, &Path, &Context) -> Result<()>;

pub fn generate(
    static_directory: &Path,
    template_directory: &Path,
    output_directory: &Path,
    posts: &[Post],
) -> Result<()> {
    let generators = available_generators();
    let context = Post::to_template_context(posts);
    for generator in generators {
        generator(
            static_directory,
            template_directory,
            output_directory,
            &context,
        )?;
    }
    Ok(())
}

fn available_generators() -> Vec<Generator> {
    vec![
        static_files::generate,
        // These three are actually the same. Can generalize, don't know how in rust yet.
        html::generate,
        rss::generate,
        txt::generate,
    ]
}

#[cfg(test)]
mod tests {
    use std::fs::create_dir_all;

    use super::*;

    use crate::metadata::Metadata;
    use crate::post::Post;

    use test_utilities::*;

    #[test]
    fn test_generates() {
        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 }}
{{~}}
",
        );
        create_test_file(
            &template_dir.join("feed.xml"),
            "\
{{~ posts:post }}
    {{= post.id }}
{{~}}
",
        );
        create_test_file(
            &template_dir.join("index.txt"),
            "\
{{~ posts:post }}
    {{= post.raw }}
{{~}}
",
        );
        let posts = vec![
            Post {
                metadata: Metadata {
                    id: "1736035200000".to_string(),
                    created_on: 1_736_035_200_000,
                },
                index: 9,
                html: "<p>Generatorial</p>".to_string(),
                raw: "electricity generator".to_string(),
            },
            Post {
                metadata: Metadata {
                    id: "1736045200000".to_string(),
                    created_on: 1_736_045_200_000,
                },
                index: 10,
                html: "<p>Generation</p>".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"),
            "\
<p>Generatorial</p>

    <p>Generation</p>
",
        );
        assert_file_contents(
            &output_dir.join("feed.xml"),
            "\
1736035200000

    1736045200000
",
        );
        cleanup_test_dir(&test_dir);
    }

    #[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());
        cleanup_test_dir(&test_dir);
    }

    #[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: "<p>Generatorial</p>".to_string(),
                raw: "electricity generator".to_string(),
            },
            Post {
                metadata: Metadata {
                    id: "1736045200000".to_string(),
                    created_on: 1_736_045_200_000,
                },
                index: 10,
                html: "<p>Generation</p>".to_string(),
                raw: "kero kero".to_string(),
            },
        ];

        let result = generate(&static_dir, &template_dir, &output_dir, &posts);

        assert!(result.is_err());
        cleanup_test_dir(&test_dir);
    }
}