]> git.r.bdr.sh - rbdr/blog/blob - src/archiver/mod.rs
Address more of the formatter comments
[rbdr/blog] / src / archiver / mod.rs
1 mod gemini;
2 mod gopher;
3 mod raw;
4
5 use crate::template::{Context, Value};
6 use std::collections::HashMap;
7 use std::fs::read_dir;
8 use std::io::Result;
9 use std::path::{Path, PathBuf};
10 use time::{format_description::FormatItem, macros::format_description, OffsetDateTime};
11
12 const DATE_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");
13
14 struct ArchiveEntry {
15 id: String,
16 slug: String,
17 }
18
19 type Archiver = fn(&Path, &Path, &Path, &Context) -> Result<()>;
20
21 impl ArchiveEntry {
22 pub fn to_template_context(archive_entries: &[ArchiveEntry]) -> Context {
23 let mut context = HashMap::new();
24
25 let archive_entries_collection = archive_entries
26 .iter()
27 .map(ArchiveEntry::to_template_value)
28 .collect();
29
30 context.insert(
31 "archive_length".to_string(),
32 Value::Unsigned(archive_entries.len().try_into().unwrap()),
33 );
34 context.insert(
35 "posts".to_string(),
36 Value::Collection(archive_entries_collection),
37 );
38
39 context
40 }
41
42 pub fn to_template_value(&self) -> Context {
43 let mut context = HashMap::new();
44
45 context.insert("id".to_string(), Value::String(self.id.clone()));
46
47 context.insert("slug".to_string(), Value::String(self.slug.clone()));
48
49 if let Some(title) = self.title() {
50 context.insert("title".to_string(), Value::String(title));
51 }
52
53 context
54 }
55
56 fn title(&self) -> Option<String> {
57 let date = OffsetDateTime::from_unix_timestamp_nanos(
58 (self.id.parse::<u64>().ok()? * 1_000_000).into(),
59 )
60 .ok()?;
61 let short_date = date.format(&DATE_FORMAT).ok()?;
62 let title = self.slug.replace('-', " ");
63 Some(format!("{short_date} {title}"))
64 }
65 }
66
67 fn read_archive(archive_directory: &PathBuf) -> Vec<ArchiveEntry> {
68 let mut archive_entries = Vec::new();
69 if let Ok(entries) = read_dir(archive_directory) {
70 for entry in entries.filter_map(Result::ok) {
71 let entry_path = entry.path();
72 let post_id = entry.file_name();
73 if let Ok(entry_type) = entry.file_type() {
74 if entry_type.is_dir() {
75 if let Ok(candidates) = read_dir(&entry_path) {
76 for candidate in candidates.filter_map(Result::ok) {
77 let candidate_path = candidate.path();
78 match candidate_path.extension() {
79 Some(extension) => {
80 if extension == "gmi" {
81 if let Some(slug) = candidate_path.file_stem() {
82 if let (Some(post_id), Some(slug)) =
83 (post_id.to_str(), slug.to_str())
84 {
85 archive_entries.push(ArchiveEntry {
86 id: post_id.to_string(),
87 slug: slug.to_string(),
88 });
89 }
90 }
91 }
92 }
93 _ => continue,
94 }
95 }
96 }
97 }
98 }
99 }
100 }
101
102 archive_entries.sort_by(|a, b| b.id.cmp(&a.id));
103 archive_entries
104 }
105
106 pub fn archive(
107 archive_directory: &PathBuf,
108 template_directory: &Path,
109 output_directory: &Path,
110 ) -> Result<()> {
111 let archivers = available_archivers();
112 let archive_entries = read_archive(archive_directory);
113 let context = ArchiveEntry::to_template_context(&archive_entries);
114 for archiver in archivers {
115 archiver(
116 archive_directory,
117 template_directory,
118 output_directory,
119 &context,
120 )?;
121 }
122 Ok(())
123 }
124
125 fn available_archivers() -> Vec<Archiver> {
126 vec![raw::archive, gemini::archive, gopher::archive]
127 }
128
129 #[cfg(test)]
130 mod tests {
131 use std::fs::create_dir_all;
132
133 use super::*;
134 use crate::template::Value;
135
136 use test_utilities::*;
137
138 // Archive template values
139
140 #[test]
141 fn test_creates_context_with_empty_archive_entries() {
142 let context = ArchiveEntry::to_template_context(&[]);
143
144 assert_eq!(context["archive_length"], Value::Unsigned(0));
145 if let Value::Collection(posts) = &context["posts"] {
146 assert!(posts.is_empty());
147 } else {
148 panic!("The posts context was not the right type.");
149 }
150 }
151
152 #[test]
153 fn test_creates_context_with_post_slice() {
154 let archive_entry = ArchiveEntry {
155 id: "you-know-what-is-cool".to_string(),
156 slug: "the-archiving-shelves-with-a-spinning-lever-to-move-them".to_string(),
157 };
158
159 let context = ArchiveEntry::to_template_context(&[archive_entry]);
160
161 assert_eq!(context["archive_length"], Value::Unsigned(1));
162 if let Value::Collection(posts) = &context["posts"] {
163 if let Some(post) = posts.first() {
164 assert_eq!(
165 post["id"],
166 Value::String("you-know-what-is-cool".to_string())
167 );
168 } else {
169 panic!("The template context had no posts");
170 }
171 } else {
172 panic!("The posts context was not the right type.");
173 }
174 }
175
176 #[test]
177 fn test_converts_archive_entry_without_title_to_context() {
178 let archive_entry = ArchiveEntry {
179 id: "they-always-show-them-in-spy-films".to_string(),
180 slug: "or-like-universities".to_string(),
181 };
182
183 let context = archive_entry.to_template_value();
184
185 assert_eq!(
186 context["id"],
187 Value::String("they-always-show-them-in-spy-films".to_string())
188 );
189 assert_eq!(
190 context["slug"],
191 Value::String("or-like-universities".to_string())
192 );
193 assert!(!context.contains_key("title"));
194 }
195
196 #[test]
197 fn test_converts_archive_entry_with_title_to_context() {
198 let archive_entry = ArchiveEntry {
199 id: "1736035200000".to_string(),
200 slug: "need-a-warehouse".to_string(),
201 };
202
203 let context = archive_entry.to_template_value();
204
205 assert_eq!(context["id"], Value::String("1736035200000".to_string()));
206 assert_eq!(
207 context["slug"],
208 Value::String("need-a-warehouse".to_string())
209 );
210 assert_eq!(
211 context["title"],
212 Value::String("2025-01-05 need a warehouse".to_string())
213 );
214 }
215
216 // Archive Finder
217 #[test]
218 fn test_finds() {
219 let test_dir = setup_test_dir();
220 let archive_dir = test_dir.join("archive");
221 let template_dir = test_dir.join("templates");
222 let output_dir = test_dir.join("output");
223
224 let first_post_dir = archive_dir.join("1736035200000");
225 create_dir_all(&first_post_dir).expect("Could not first create post test directory");
226 create_test_file(
227 &first_post_dir.join("my-very-cool-post.gmi"),
228 "is full of flowers",
229 );
230
231 let second_post_dir = archive_dir.join("1736121600000");
232 create_dir_all(&second_post_dir).expect("Could not create second post test directory");
233 create_test_file(
234 &second_post_dir.join("my-very-sad-post.gmi"),
235 "is full of regrets",
236 );
237
238 let bad_file_dir = archive_dir.join("1736121600001");
239 create_dir_all(&bad_file_dir).expect("Could not create bad file test directory");
240 create_test_file(&bad_file_dir.join("my-very-bad-file"), "is full of lies");
241
242 create_dir_all(&template_dir).expect("Could not create template test directory");
243 create_dir_all(&output_dir).expect("Could not create output test directory");
244
245 archive(&archive_dir, &template_dir, &output_dir).expect("Archive failed");
246
247 assert_file_contains(
248 &output_dir.join("index.gmi"),
249 "\n=> ./1736035200000/my-very-cool-post.gmi 2025-01-05 my very cool post",
250 );
251 assert_file_contains(
252 &output_dir.join("index.gmi"),
253 "\n=> ./1736121600000/my-very-sad-post.gmi 2025-01-06 my very sad post",
254 );
255 assert_file_contents(
256 &output_dir.join("1736035200000/my-very-cool-post.gmi"),
257 "is full of flowers",
258 );
259 assert_file_contents(
260 &output_dir.join("1736121600000/my-very-sad-post.gmi"),
261 "is full of regrets",
262 );
263 assert!(&output_dir.join("1736121600001/my-very-bad-file").exists());
264 assert_file_does_not_contain(
265 &output_dir.join("index.gmi"),
266 "1736121600001/my-very-bad-file",
267 );
268 }
269 }