]> git.r.bdr.sh - rbdr/page/blame_incremental - src/file_handler/file_strategies/layout.rs
Reformat
[rbdr/page] / src / file_handler / file_strategies / layout.rs
... / ...
CommitLineData
1pub struct Strategy {}
2
3use std::path::Path;
4
5use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy};
6
7impl FileHandlerStrategy for Strategy {
8 fn is(&self, path: &Path) -> bool {
9 !path.is_dir() && path.ends_with("_layout.html")
10 }
11
12 fn identify(&self) -> FileType {
13 FileType::Layout
14 }
15
16 fn can_handle(&self, file_type: &FileType) -> bool {
17 matches!(file_type, FileType::Layout)
18 }
19
20 // We don't implement handling for layout, as we assume there's only one
21 // and it got handled before.
22 fn handle_html(&self, _s: &Path, _d: &Path, _f: &File, _l: &str) {}
23 fn handle_gemini(&self, _s: &Path, _d: &Path, _f: &File) {}
24}
25
26#[cfg(test)]
27mod tests {
28 use std::fs::create_dir_all;
29 use std::path::PathBuf;
30
31 use super::*;
32
33 use test_utilities::*;
34
35 #[test]
36 fn identifies_layout_file() {
37 let test_dir = setup_test_dir();
38 create_test_file(&test_dir.join("_layout.html"), "");
39 let strategy = Strategy {};
40 assert!(strategy.is(&test_dir.join("_layout.html")));
41 }
42
43 #[test]
44 fn rejects_non_layout_html() {
45 let test_dir = setup_test_dir();
46 create_test_file(&test_dir.join("regular.html"), "");
47 let strategy = Strategy {};
48 assert!(!strategy.is(&test_dir.join("regular.html")));
49 }
50
51 #[test]
52 fn rejects_layout_with_different_extension() {
53 let test_dir = setup_test_dir();
54 create_test_file(&test_dir.join("_layout.txt"), "");
55 let strategy = Strategy {};
56 assert!(!strategy.is(&test_dir.join("_layout.txt")));
57 }
58
59 #[test]
60 fn rejects_layout_with_prefix() {
61 let test_dir = setup_test_dir();
62 create_test_file(&test_dir.join("prefix_layout.txt"), "");
63 let strategy = Strategy {};
64 assert!(!strategy.is(&test_dir.join("prefix_layout.txt")));
65 }
66
67 #[test]
68 fn rejects_directory_named_layout() {
69 let test_dir = setup_test_dir();
70 let layout_dir = test_dir.join("_layout");
71 create_dir_all(&layout_dir).expect("Could not create _layout test directory");
72 let strategy = Strategy {};
73 assert!(!strategy.is(&layout_dir));
74 }
75
76 #[test]
77 fn identifies_layout_type() {
78 let strategy = Strategy {};
79 assert!(matches!(strategy.identify(), FileType::Layout));
80 }
81
82 #[test]
83 fn handles_layout_type() {
84 let strategy = Strategy {};
85 assert!(strategy.can_handle(&FileType::Layout));
86 }
87
88 #[test]
89 fn rejects_non_layout_types() {
90 let strategy = Strategy {};
91 assert!(!strategy.can_handle(&FileType::File));
92 assert!(!strategy.can_handle(&FileType::Gemini));
93 assert!(!strategy.can_handle(&FileType::Unknown));
94 }
95
96 #[test]
97 fn handle_html_does_nothing() {
98 let strategy = Strategy {};
99 let file = File {
100 path: PathBuf::from("_layout.html"),
101 file_type: FileType::Layout,
102 };
103
104 strategy.handle_html(
105 &PathBuf::from("source"),
106 &PathBuf::from("dest"),
107 &file,
108 "layout content",
109 );
110 }
111
112 #[test]
113 fn handle_gemini_does_nothing() {
114 let strategy = Strategy {};
115 let file = File {
116 path: PathBuf::from("test.gmi"),
117 file_type: FileType::Layout,
118 };
119
120 // Should not panic
121 strategy.handle_gemini(&PathBuf::from("source"), &PathBuf::from("dest"), &file);
122 }
123}