]>
Commit | Line | Data |
---|---|---|
1 | use crate::template::{find, parse, Context}; | |
2 | use std::fs::write; | |
3 | use std::io::{Error, ErrorKind::Other, Result}; | |
4 | use std::path::Path; | |
5 | ||
6 | const FILENAME: &str = "index.txt"; | |
7 | ||
8 | pub fn generate( | |
9 | _: &Path, | |
10 | template_directory: &Path, | |
11 | target: &Path, | |
12 | context: &Context, | |
13 | ) -> Result<()> { | |
14 | if let Some(template) = find(template_directory, FILENAME) { | |
15 | let parsed_template = | |
16 | parse(&template).ok_or_else(|| Error::new(Other, "Unable to parse TXT template"))?; | |
17 | let rendered_template = parsed_template.render(context)?; | |
18 | let location = target.join(FILENAME); | |
19 | write(location, rendered_template)?; | |
20 | } | |
21 | Ok(()) | |
22 | } | |
23 | ||
24 | #[cfg(test)] | |
25 | mod tests { | |
26 | use std::collections::HashMap; | |
27 | use std::fs::create_dir_all; | |
28 | ||
29 | use super::*; | |
30 | ||
31 | use crate::template::Value; | |
32 | ||
33 | use test_utilities::*; | |
34 | ||
35 | #[test] | |
36 | fn test_generates_txt_with_default_layout() { | |
37 | let test_dir = setup_test_dir(); | |
38 | let static_dir = test_dir.join("static"); | |
39 | let template_dir = test_dir.join("templates"); | |
40 | let output_dir = test_dir.join("output"); | |
41 | ||
42 | create_dir_all(&static_dir).expect("Could not create static directory"); | |
43 | create_dir_all(&template_dir).expect("Could not create template directory"); | |
44 | create_dir_all(&output_dir).expect("Could not create output directory"); | |
45 | ||
46 | let context = HashMap::from([ | |
47 | ("has_posts".to_string(), Value::Bool(true)), | |
48 | ( | |
49 | "posts".to_string(), | |
50 | Value::Collection(vec![ | |
51 | HashMap::from([( | |
52 | "raw".to_string(), | |
53 | Value::String("Contextualization".to_string()), | |
54 | )]), | |
55 | HashMap::from([( | |
56 | "raw".to_string(), | |
57 | Value::String("Contexternalization".to_string()), | |
58 | )]), | |
59 | ]), | |
60 | ), | |
61 | ]); | |
62 | ||
63 | generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed"); | |
64 | ||
65 | assert_file_contains(&output_dir.join("index.txt"), "Contextualization"); | |
66 | assert_file_contains(&output_dir.join("index.txt"), "Contexternalization"); | |
67 | } | |
68 | ||
69 | #[test] | |
70 | fn test_uses_custom_layout_if_available() { | |
71 | let test_dir = setup_test_dir(); | |
72 | let static_dir = test_dir.join("static"); | |
73 | let template_dir = test_dir.join("templates"); | |
74 | let output_dir = test_dir.join("output"); | |
75 | ||
76 | create_dir_all(&static_dir).expect("Could not create static directory"); | |
77 | create_dir_all(&template_dir).expect("Could not create template directory"); | |
78 | create_dir_all(&output_dir).expect("Could not create output directory"); | |
79 | create_test_file( | |
80 | &template_dir.join("index.txt"), | |
81 | "\ | |
82 | {{~ posts:post }} | |
83 | {{= post.raw }} | |
84 | {{~}} | |
85 | ", | |
86 | ); | |
87 | let context = HashMap::from([( | |
88 | "posts".to_string(), | |
89 | Value::Collection(vec![ | |
90 | HashMap::from([( | |
91 | "raw".to_string(), | |
92 | Value::String("Recontextualization".to_string()), | |
93 | )]), | |
94 | HashMap::from([( | |
95 | "raw".to_string(), | |
96 | Value::String("Recontexternalization".to_string()), | |
97 | )]), | |
98 | ]), | |
99 | )]); | |
100 | ||
101 | generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed"); | |
102 | ||
103 | assert_file_contents( | |
104 | &output_dir.join("index.txt"), | |
105 | "\ | |
106 | Recontextualization | |
107 | ||
108 | Recontexternalization | |
109 | ", | |
110 | ); | |
111 | } | |
112 | ||
113 | #[test] | |
114 | fn test_fails_if_txt_is_malformed() { | |
115 | let test_dir = setup_test_dir(); | |
116 | let static_dir = test_dir.join("static"); | |
117 | let template_dir = test_dir.join("templates"); | |
118 | let output_dir = test_dir.join("output"); | |
119 | ||
120 | create_dir_all(&static_dir).expect("Could not create static directory"); | |
121 | create_dir_all(&template_dir).expect("Could not create template directory"); | |
122 | create_dir_all(&output_dir).expect("Could not create output directory"); | |
123 | create_test_file( | |
124 | &template_dir.join("index.txt"), | |
125 | "\ | |
126 | {{~ posts:post }} | |
127 | {{ post.raw }} | |
128 | {{~}} | |
129 | ", | |
130 | ); | |
131 | let context = HashMap::new(); | |
132 | ||
133 | let result = generate(&static_dir, &template_dir, &output_dir, &context); | |
134 | ||
135 | assert!(result.is_err()); | |
136 | } | |
137 | ||
138 | #[test] | |
139 | fn test_fails_if_output_is_not_writable() { | |
140 | let test_dir = setup_test_dir(); | |
141 | let static_dir = test_dir.join("static"); | |
142 | let template_dir = test_dir.join("templates"); | |
143 | let output_dir = test_dir.join("output"); | |
144 | ||
145 | create_dir_all(&template_dir).expect("Could not create template directory"); | |
146 | create_test_file( | |
147 | &template_dir.join("index.txt"), | |
148 | "\ | |
149 | {{~ posts:post }} | |
150 | {{= post.raw }} | |
151 | {{~}} | |
152 | ", | |
153 | ); | |
154 | let context = HashMap::from([( | |
155 | "posts".to_string(), | |
156 | Value::Collection(vec![ | |
157 | HashMap::from([( | |
158 | "raw".to_string(), | |
159 | Value::String("Recontextualization".to_string()), | |
160 | )]), | |
161 | HashMap::from([( | |
162 | "raw".to_string(), | |
163 | Value::String("Recontexternalization".to_string()), | |
164 | )]), | |
165 | ]), | |
166 | )]); | |
167 | ||
168 | let result = generate(&static_dir, &template_dir, &output_dir, &context); | |
169 | ||
170 | assert!(result.is_err()); | |
171 | } | |
172 | } |