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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
mod file_strategies;
use file_strategies::file::Strategy as FileStrategy;
use file_strategies::gemini::Strategy as GeminiStrategy;
use file_strategies::layout::Strategy as LayoutStrategy;
use std::fs::read_to_string;
use std::io::{Error, ErrorKind, Result};
use std::path::{Path, PathBuf};
pub struct FileHandler {
pub strategies: Vec<Box<dyn Strategy>>,
pub layout: Option<String>,
}
impl Default for FileHandler {
fn default() -> FileHandler {
FileHandler {
strategies: vec![
Box::new(GeminiStrategy {}),
Box::new(LayoutStrategy {}),
Box::new(FileStrategy {}),
],
layout: None,
}
}
}
impl FileHandler {
pub fn identify(&self, path: &Path) -> FileType {
for strategy in &self.strategies {
if strategy.is(path) {
return strategy.identify();
}
}
FileType::Unknown
}
pub fn get_layout(&mut self, files: &[File]) -> Result<()> {
for file in files {
if file.file_type == FileType::Layout {
let layout_text = read_to_string(&file.path)?;
self.layout = Some(layout_text);
return Ok(());
}
}
Err(Error::new(
ErrorKind::NotFound,
"No layout found. Please ensure there's a _layout.html file at the root",
))
}
pub fn handle_all(
&self,
source: &Path,
html_destination: &Path,
gemini_destination: &Path,
files: &[File],
) -> Result<()> {
for file in files {
self.handle(source, html_destination, gemini_destination, file)?;
}
Ok(())
}
pub fn handle(
&self,
source: &Path,
html_destination: &Path,
gemini_destination: &Path,
file: &File,
) -> Result<()> {
match self
.strategies
.iter()
.find(|s| s.can_handle(&file.file_type))
{
Some(strategy) => {
let layout = self.layout.as_ref().ok_or_else(|| {
Error::new(
ErrorKind::NotFound,
"Layout should be initialized before handling files",
)
})?;
strategy.handle_html(source, html_destination, file, layout)?;
strategy.handle_gemini(source, gemini_destination, file)?;
Ok(())
}
// We silently ignore files we can't process.
None => Ok(()),
}
}
}
pub trait Strategy {
fn is(&self, path: &Path) -> bool;
fn identify(&self) -> FileType;
fn can_handle(&self, file_type: &FileType) -> bool;
fn handle_html(
&self,
source: &Path,
destination: &Path,
file: &File,
layout: &str,
) -> Result<()>;
fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) -> Result<()>;
}
#[derive(Debug, Clone, PartialEq)]
pub enum FileType {
Gemini,
File,
Layout,
Unknown,
}
#[derive(PartialEq, Debug)]
pub struct File {
pub path: PathBuf,
pub file_type: FileType,
}
#[cfg(test)]
mod tests {
use std::fs::create_dir_all;
use std::path::PathBuf;
use super::*;
use test_utilities::*;
fn create_test_internal_file(path: &str, file_type: FileType) -> File {
File {
path: PathBuf::from(path),
file_type,
}
}
#[test]
fn test_identify_gemini_file() {
let handler = FileHandler::default();
let path = PathBuf::from("test.gmi");
assert!(matches!(handler.identify(&path), FileType::Gemini));
}
#[test]
fn test_identify_layout_file() {
let handler = FileHandler::default();
let path = PathBuf::from("_layout.html");
assert!(matches!(handler.identify(&path), FileType::Layout));
}
#[test]
fn test_identify_regular_file() {
let handler = FileHandler::default();
let path = PathBuf::from("regular.html");
assert!(matches!(handler.identify(&path), FileType::File));
}
#[test]
fn test_identify_unknown_file() {
let handler = FileHandler::default();
let path = PathBuf::from("tests");
assert!(matches!(handler.identify(&path), FileType::Unknown));
}
#[test]
fn test_get_layout_success() {
let test_dir = setup_test_dir();
let layout_path = test_dir.join("_layout.html");
create_test_file(&layout_path, "");
let mut handler = FileHandler::default();
let files = vec![
create_test_internal_file("test.gmi", FileType::Gemini),
create_test_internal_file(
layout_path.to_str().expect("Could not encode layout"),
FileType::Layout,
),
create_test_internal_file("regular.html", FileType::File),
];
assert!(handler.get_layout(&files).is_ok());
}
#[test]
fn test_get_layout_failure() {
let mut handler = FileHandler::default();
let files = vec![
create_test_internal_file("test.gmi", FileType::Gemini),
create_test_internal_file("regular.html", FileType::File),
];
assert!(handler.get_layout(&files).is_err());
}
// Mock strategy for testing
struct MockStrategy {
is_match: bool,
file_type: FileType,
}
impl Strategy for MockStrategy {
fn is(&self, _path: &Path) -> bool {
self.is_match
}
fn identify(&self) -> FileType {
self.file_type.clone()
}
fn can_handle(&self, file_type: &FileType) -> bool {
&self.file_type == file_type
}
fn handle_html(
&self,
_source: &Path,
_destination: &Path,
_file: &File,
_layout: &str,
) -> Result<()> {
Ok(())
}
fn handle_gemini(&self, _source: &Path, _destination: &Path, _file: &File) -> Result<()> {
Ok(())
}
}
// Mock strategy for testing
struct ErroringMockStrategy {
is_match: bool,
file_type: FileType,
}
impl Strategy for ErroringMockStrategy {
fn is(&self, _path: &Path) -> bool {
self.is_match
}
fn identify(&self) -> FileType {
self.file_type.clone()
}
fn can_handle(&self, file_type: &FileType) -> bool {
&self.file_type == file_type
}
fn handle_html(
&self,
_source: &Path,
_destination: &Path,
_file: &File,
_layout: &str,
) -> Result<()> {
Err(Error::new(
ErrorKind::Other,
"Forced to fail by my cruel author.",
))
}
fn handle_gemini(&self, _source: &Path, _destination: &Path, _file: &File) -> Result<()> {
Err(Error::new(
ErrorKind::Other,
"Forced to fail by my benevolent author.",
))
}
}
#[test]
fn test_custom_strategy() {
let mock_strategy = MockStrategy {
is_match: true,
file_type: FileType::Gemini,
};
let handler = FileHandler {
strategies: vec![Box::new(mock_strategy)],
layout: Some("None".to_string()),
};
let path = PathBuf::from("test.whatever");
let file = File {
path: path.clone(),
file_type: FileType::Gemini,
};
assert!(matches!(handler.identify(&path), FileType::Gemini));
assert!(handler.handle(&path, &path, &path, &file).is_ok());
}
#[test]
fn test_failure_propagates_in_handler() {
let mock_strategy = ErroringMockStrategy {
is_match: true,
file_type: FileType::Gemini,
};
let handler = FileHandler {
strategies: vec![Box::new(mock_strategy)],
layout: Some("None".to_string()),
};
let path = PathBuf::from("test.whatever");
let file = File {
path: path.clone(),
file_type: FileType::Gemini,
};
assert!(matches!(handler.identify(&path), FileType::Gemini));
assert!(handler.handle(&path, &path, &path, &file).is_err());
}
#[test]
fn silently_ignore_unhandleable_files() {
let mock_strategy = MockStrategy {
is_match: false,
file_type: FileType::Gemini,
};
let handler = FileHandler {
strategies: vec![Box::new(mock_strategy)],
layout: Some("None".to_string()),
};
let path = PathBuf::from("test.whatever");
let file = File {
path: path.clone(),
file_type: FileType::Layout,
};
assert!(handler.handle(&path, &path, &path, &file).is_ok());
}
#[test]
fn test_handle_all_empty_files() {
let handler = FileHandler::default();
let files: Vec<File> = vec![];
// Should not panic with empty vector
assert!(
handler
.handle_all(
&PathBuf::from("source"),
&PathBuf::from("output_html"),
&PathBuf::from("output_gemini"),
&files,
)
.is_ok()
);
}
#[test]
fn test_handle_with_layout() {
let handler = FileHandler {
layout: Some("test layout".to_string()),
..Default::default()
};
let test_dir = setup_test_dir();
create_dir_all(test_dir.join("output_html"))
.expect("Could not create output html test directory");
create_dir_all(test_dir.join("output_gemini"))
.expect("Could not create output gemini test directory");
let test_path = test_dir.join("test.gmi");
create_test_file(&test_path, "");
let file = create_test_internal_file(
test_path
.to_str()
.expect("Could not encode gemini test file"),
FileType::Gemini,
);
// Should not panic with valid layout
assert!(
handler
.handle(
&test_dir,
&test_dir.join("output_html"),
&test_dir.join("output_gemini"),
&file,
)
.is_ok()
);
}
#[test]
fn test_handle_without_layout() {
let handler = FileHandler::default();
let file = create_test_internal_file("test.gmi", FileType::Gemini);
assert!(
handler
.handle(
&PathBuf::from("source"),
&PathBuf::from("output_html"),
&PathBuf::from("output_gemini"),
&file,
)
.is_err()
);
}
#[test]
fn test_slice_handling() {
let test_dir = setup_test_dir();
let layout_path = test_dir.join("_layout.html");
create_test_file(&layout_path, "");
create_test_file(&test_dir.join("test1.gmi"), "");
create_test_file(&test_dir.join("test2.gmi"), "");
create_test_file(&test_dir.join("test3.gmi"), "");
create_dir_all(test_dir.join("output_html"))
.expect("Could not create output html test directory");
create_dir_all(test_dir.join("output_gemini"))
.expect("Could not create output gemini test directory");
let mut handler = FileHandler::default();
let files = [
create_test_internal_file(
test_dir
.join("test1.gmi")
.to_str()
.expect("Could not encode test1"),
FileType::Gemini,
),
create_test_internal_file(
layout_path.to_str().expect("Could not encode layout"),
FileType::Layout,
),
create_test_internal_file(
test_dir
.join("test2.gmi")
.to_str()
.expect("Could not encode test2"),
FileType::Gemini,
),
create_test_internal_file(
test_dir
.join("test3.gmi")
.to_str()
.expect("Could not encode test3"),
FileType::Gemini,
),
];
let _ = handler.get_layout(&files[1..]);
// Test with slice
assert!(
handler
.handle_all(
&test_dir,
&test_dir.join("output_html"),
&test_dir.join("output_gemini"),
&files[1..], // Test with slice of last three elements
)
.is_ok()
);
}
}
|