3 use file_strategies::file::Strategy as FileStrategy;
4 use file_strategies::gemini::Strategy as GeminiStrategy;
5 use file_strategies::layout::Strategy as LayoutStrategy;
7 use std::path::PathBuf;
8 use std::fs::read_to_string;
10 pub struct FileHandler {
11 pub strategies: Vec<Box<dyn FileHandlerStrategy>>,
12 pub layout: Option<String>
15 impl Default for FileHandler {
16 fn default() -> FileHandler {
19 Box::new(GeminiStrategy{}),
20 Box::new(LayoutStrategy{}),
21 Box::new(FileStrategy{}),
29 pub fn identify(&self, path: &PathBuf) -> FileType {
30 for strategy in self.strategies.iter() {
31 if strategy.is(&path) {
32 return strategy.identify();
38 pub fn get_layout_or_panic(&mut self, files: &[File]) -> Result<(), &str> {
40 match file.file_type {
42 let layout_text = read_to_string(&file.path).unwrap();
43 self.layout = Some(layout_text);
49 Err("No layout found. Please ensure there's a _layout.html file at the root")
52 pub fn handle_all(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, files: &[File]) {
53 files.iter().for_each(|file| {
54 self.handle(source, html_destination, gemini_destination, file);
58 pub fn handle(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, file: &File) {
59 if let Some(strategy) = self.strategies
61 .find(|s| s.can_handle(&file.file_type))
63 let layout = self.layout.as_ref()
64 .expect("Layout should be initialized before handling files");
65 strategy.handle_html(source, html_destination, file, layout);
66 strategy.handle_gemini(source, gemini_destination, file);
72 pub trait FileHandlerStrategy {
73 fn is(&self, path: &PathBuf) -> bool;
74 fn identify(&self) -> FileType;
75 fn can_handle(&self, file_type: &FileType) -> bool;
76 fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &str);
77 fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File);
80 #[derive(Debug, Clone, PartialEq)]
88 #[derive(PartialEq, Debug)]
91 pub file_type: FileType,
97 use std::path::PathBuf;
101 use test_utilities::*;
103 fn create_test_internal_file(path: &str, file_type: FileType) -> File {
105 path: PathBuf::from(path),
111 fn test_identify_gemini_file() {
112 let handler = FileHandler::default();
113 let path = PathBuf::from("test.gmi");
114 assert!(matches!(handler.identify(&path), FileType::Gemini));
118 fn test_identify_layout_file() {
119 let handler = FileHandler::default();
120 let path = PathBuf::from("_layout.html");
121 assert!(matches!(handler.identify(&path), FileType::Layout));
125 fn test_identify_regular_file() {
126 let handler = FileHandler::default();
127 let path = PathBuf::from("regular.html");
128 assert!(matches!(handler.identify(&path), FileType::File));
132 fn test_identify_unknown_file() {
133 let handler = FileHandler::default();
134 let path = PathBuf::from("tests");
135 assert!(matches!(handler.identify(&path), FileType::Unknown));
139 fn test_get_layout_success() {
140 let test_dir = setup_test_dir();
141 let layout_path = test_dir.join("_layout.html");
142 create_test_file(&layout_path, "");
144 let mut handler = FileHandler::default();
146 create_test_internal_file("test.gmi", FileType::Gemini),
147 create_test_internal_file(&layout_path.to_str().expect("Could not encode layout"), FileType::Layout),
148 create_test_internal_file("regular.html", FileType::File),
151 assert!(handler.get_layout_or_panic(&files).is_ok());
155 fn test_get_layout_failure() {
156 let mut handler = FileHandler::default();
158 create_test_internal_file("test.gmi", FileType::Gemini),
159 create_test_internal_file("regular.html", FileType::File),
162 assert!(handler.get_layout_or_panic(&files).is_err());
165 // Mock strategy for testing
166 struct MockStrategy {
171 impl FileHandlerStrategy for MockStrategy {
172 fn is(&self, _path: &PathBuf) -> bool {
176 fn identify(&self) -> FileType {
177 self.file_type.clone()
180 fn can_handle(&self, file_type: &FileType) -> bool {
181 &self.file_type == file_type
184 fn handle_html(&self, _source: &PathBuf, _destination: &PathBuf, _file: &File, _layout: &str) {}
185 fn handle_gemini(&self, _source: &PathBuf, _destination: &PathBuf, _file: &File) {}
189 fn test_custom_strategy() {
190 let mock_strategy = MockStrategy {
192 file_type: FileType::Gemini,
195 let handler = FileHandler {
196 strategies: vec![Box::new(mock_strategy)],
200 let path = PathBuf::from("test.whatever");
201 assert!(matches!(handler.identify(&path), FileType::Gemini));
205 fn test_handle_all_empty_files() {
206 let handler = FileHandler::default();
207 let files: Vec<File> = vec![];
209 // Should not panic with empty vector
211 &PathBuf::from("source"),
212 &PathBuf::from("tests/fixtures/output"),
213 &PathBuf::from("tests/fixtures/output"),
219 fn test_handle_with_layout() {
220 let mut handler = FileHandler::default();
221 handler.layout = Some("test layout".to_string());
223 let file = create_test_internal_file("tests/fixtures/test1.gmi", FileType::Gemini);
225 // Should not panic with valid layout
228 &PathBuf::from("tests/fixtures/output"),
229 &PathBuf::from("tests/fixtures/output"),
235 #[should_panic(expected = "Layout should be initialized before handling files")]
236 fn test_handle_without_layout() {
237 let handler = FileHandler::default();
238 let file = create_test_internal_file("test.gmi", FileType::Gemini);
241 &PathBuf::from("source"),
242 &PathBuf::from("tests/fixtures/output"),
243 &PathBuf::from("tests/fixtures/output"),
249 fn test_slice_handling() {
250 let test_dir = setup_test_dir();
251 let layout_path = test_dir.join("_layout.html");
252 create_test_file(&layout_path, "");
254 let mut handler = FileHandler::default();
256 create_test_internal_file("tests/fixtures/test1.gmi", FileType::Gemini),
257 create_test_internal_file(&layout_path.to_str().expect("Could not encode layout"), FileType::Layout),
258 create_test_internal_file("tests/fixtures/test2.gmi", FileType::Gemini),
259 create_test_internal_file("tests/fixtures/test3.gmi", FileType::Gemini),
262 let _ = handler.get_layout_or_panic(&files[1..]);
267 &PathBuf::from("tests/fixtures/output"),
268 &PathBuf::from("tests/fixtures/output"),
269 &files[1..] // Test with slice of last three elements