From ea5297364f8a1b2c4e684140024b60a83b087b50 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sun, 16 Apr 2023 13:46:46 +0200 Subject: Add static file copying --- src/file_handler/file_strategies/file.rs | 9 +++++++-- src/file_handler/file_strategies/gemini.rs | 2 +- src/file_handler/file_strategies/layout.rs | 4 +++- src/file_handler/mod.rs | 4 ++-- 4 files changed, 13 insertions(+), 6 deletions(-) (limited to 'src/file_handler') diff --git a/src/file_handler/file_strategies/file.rs b/src/file_handler/file_strategies/file.rs index b1de596..42b87d5 100644 --- a/src/file_handler/file_strategies/file.rs +++ b/src/file_handler/file_strategies/file.rs @@ -1,6 +1,7 @@ pub struct Strategy {} use std::path::PathBuf; +use std::fs::{copy, create_dir_all}; use crate::file_handler::{File, FileType, FileHandlerStrategy}; @@ -20,7 +21,11 @@ impl FileHandlerStrategy for Strategy { } } - fn handle(&self, file: &File) { - println!("Should copy {}", file.path.display()) + fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) { + let relative_path = file.path.strip_prefix(&source).unwrap(); + let complete_destination = destination.join(relative_path); + let destination_parent = complete_destination.parent().unwrap(); + create_dir_all(destination_parent).unwrap(); + copy(&file.path, &complete_destination).unwrap(); } } diff --git a/src/file_handler/file_strategies/gemini.rs b/src/file_handler/file_strategies/gemini.rs index 905e1a9..2427422 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -23,7 +23,7 @@ impl FileHandlerStrategy for Strategy { } } - fn handle(&self, file: &File) { + fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) { println!("Should parse and copy {}", file.path.display()) } } diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs index bac1059..21ce9ba 100644 --- a/src/file_handler/file_strategies/layout.rs +++ b/src/file_handler/file_strategies/layout.rs @@ -20,5 +20,7 @@ impl FileHandlerStrategy for Strategy { } } - fn handle(&self, _file: &File) {} + // We don't implement handling for layout, as we assume there's only one + // and it got handled before. + fn handle(&self, _s: &PathBuf, _d: &PathBuf, _f: &File) {} } diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs index 53aaba9..64224ee 100644 --- a/src/file_handler/mod.rs +++ b/src/file_handler/mod.rs @@ -58,7 +58,7 @@ impl FileHandler { pub fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) { for strategy in self.strategies.iter() { if strategy.can_handle(&file.file_type) { - return strategy.handle(file); + return strategy.handle(source, destination, file); } } } @@ -68,7 +68,7 @@ pub trait FileHandlerStrategy { fn is(&self, path: &PathBuf) -> bool; fn identify(&self) -> FileType; fn can_handle(&self, file_type: &FileType) -> bool; - fn handle(&self, file: &File); + fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File); } pub enum FileType { -- cgit