aboutsummaryrefslogtreecommitdiff
path: root/src/file_handler
diff options
context:
space:
mode:
Diffstat (limited to 'src/file_handler')
-rw-r--r--src/file_handler/file_strategies/file.rs8
-rw-r--r--src/file_handler/file_strategies/gemini.rs52
-rw-r--r--src/file_handler/file_strategies/layout.rs2
-rw-r--r--src/file_handler/mod.rs12
4 files changed, 34 insertions, 40 deletions
diff --git a/src/file_handler/file_strategies/file.rs b/src/file_handler/file_strategies/file.rs
index c3c3016..e5573a2 100644
--- a/src/file_handler/file_strategies/file.rs
+++ b/src/file_handler/file_strategies/file.rs
@@ -3,10 +3,10 @@ pub struct Strategy {}
use std::fs::{copy, create_dir_all};
use std::path::Path;
-use crate::file_handler::{File, FileHandlerStrategy, FileType};
+use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy};
impl Strategy {
- fn handle(&self, source: &Path, destination: &Path, file: &File) {
+ fn handle(source: &Path, destination: &Path, 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();
@@ -29,11 +29,11 @@ impl FileHandlerStrategy for Strategy {
}
fn handle_html(&self, source: &Path, destination: &Path, file: &File, _l: &str) {
- self.handle(source, destination, file)
+ Strategy::handle(source, destination, file);
}
fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) {
- self.handle(source, destination, file)
+ Strategy::handle(source, destination, file);
}
}
diff --git a/src/file_handler/file_strategies/gemini.rs b/src/file_handler/file_strategies/gemini.rs
index 32cb99e..df35d64 100644
--- a/src/file_handler/file_strategies/gemini.rs
+++ b/src/file_handler/file_strategies/gemini.rs
@@ -4,24 +4,24 @@ use std::fs::{create_dir_all, read_to_string, File as IOFile};
use std::io::Write;
use std::path::Path;
-use crate::file_handler::{File, FileHandlerStrategy, FileType};
+use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy};
use crate::gemini_parser::parse;
use crate::html_renderer::render_html;
impl Strategy {
- fn is_title(&self, line: &str) -> bool {
+ fn is_title(line: &str) -> bool {
line.starts_with("--- title:")
}
- fn is_description(&self, line: &str) -> bool {
+ fn is_description(line: &str) -> bool {
line.starts_with("--- description:")
}
- fn get_title<'a>(&self, line: &'a str) -> &'a str {
+ fn get_title(line: &str) -> &str {
line.split_once("--- title:").unwrap().1
}
- fn get_description<'a>(&self, line: &'a str) -> &'a str {
+ fn get_description(line: &str) -> &str {
line.split_once("--- description:").unwrap().1
}
}
@@ -46,19 +46,19 @@ impl FileHandlerStrategy for Strategy {
let gemini_contents = read_to_string(&file.path).unwrap();
// Front matter extraction
- let lines: Vec<&str> = gemini_contents.split("\n").collect();
+ let lines: Vec<&str> = gemini_contents.split('\n').collect();
let mut lines_found = 0;
let mut title = "";
let mut description = "";
if let Some(slice) = lines.get(..2) {
- for line in slice.iter() {
- if self.is_title(line) {
- title = self.get_title(line).trim();
+ for line in slice {
+ if Strategy::is_title(line) {
+ title = Strategy::get_title(line).trim();
lines_found += 1;
continue;
}
- if self.is_description(line) {
- description = self.get_description(line).trim();
+ if Strategy::is_description(line) {
+ description = Strategy::get_description(line).trim();
lines_found += 1;
continue;
}
@@ -66,7 +66,7 @@ impl FileHandlerStrategy for Strategy {
}
let gemini_source = lines[lines_found..].join("\n");
- let content_html = render_html(parse(&gemini_source[..]));
+ let content_html = render_html(&parse(&gemini_source[..]));
let generated_html = layout
.replace("{{ title }}", title)
@@ -89,15 +89,15 @@ impl FileHandlerStrategy for Strategy {
let gemini_contents = read_to_string(&file.path).unwrap();
// Front matter extraction
- let lines: Vec<&str> = gemini_contents.split("\n").collect();
+ let lines: Vec<&str> = gemini_contents.split('\n').collect();
let mut lines_found = 0;
if let Some(slice) = lines.get(..2) {
- for line in slice.iter() {
- if self.is_title(line) {
+ for line in slice {
+ if Strategy::is_title(line) {
lines_found += 1;
continue;
}
- if self.is_description(line) {
+ if Strategy::is_description(line) {
lines_found += 1;
continue;
}
@@ -128,40 +128,34 @@ mod tests {
#[test]
fn detects_title() {
- let strategy = Strategy {};
- assert!(strategy.is_title("--- title: Hello!"));
+ assert!(Strategy::is_title("--- title: Hello!"));
}
#[test]
fn does_not_detect_other_keys_as_title() {
- let strategy = Strategy {};
- assert!(!strategy.is_title("--- description: Hello!"));
+ assert!(!Strategy::is_title("--- description: Hello!"));
}
#[test]
fn detects_description() {
- let strategy = Strategy {};
- assert!(strategy.is_description("--- description: What is this?"));
+ assert!(Strategy::is_description("--- description: What is this?"));
}
#[test]
fn does_not_detect_other_keys_as_description() {
- let strategy = Strategy {};
- assert!(!strategy.is_description("--- title: What is this?"));
+ assert!(!Strategy::is_description("--- title: What is this?"));
}
#[test]
fn extracts_title() {
- let strategy = Strategy {};
- assert_eq!(strategy.get_title("--- title: Hello!").trim(), "Hello!");
+ assert_eq!(Strategy::get_title("--- title: Hello!").trim(), "Hello!");
}
#[test]
fn extracts_description() {
- let strategy = Strategy {};
assert_eq!(
- strategy
- .get_description("--- description: What is this?")
+ Strategy
+ ::get_description("--- description: What is this?")
.trim(),
"What is this?"
);
diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs
index 8d9689c..9a60f12 100644
--- a/src/file_handler/file_strategies/layout.rs
+++ b/src/file_handler/file_strategies/layout.rs
@@ -2,7 +2,7 @@ pub struct Strategy {}
use std::path::Path;
-use crate::file_handler::{File, FileHandlerStrategy, FileType};
+use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy};
impl FileHandlerStrategy for Strategy {
fn is(&self, path: &Path) -> bool {
diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs
index 522bcbf..e8a446b 100644
--- a/src/file_handler/mod.rs
+++ b/src/file_handler/mod.rs
@@ -8,7 +8,7 @@ use std::fs::read_to_string;
use std::path::{Path, PathBuf};
pub struct FileHandler {
- pub strategies: Vec<Box<dyn FileHandlerStrategy>>,
+ pub strategies: Vec<Box<dyn Strategy>>,
pub layout: Option<String>,
}
@@ -27,7 +27,7 @@ impl Default for FileHandler {
impl FileHandler {
pub fn identify(&self, path: &Path) -> FileType {
- for strategy in self.strategies.iter() {
+ for strategy in &self.strategies {
if strategy.is(path) {
return strategy.identify();
}
@@ -53,9 +53,9 @@ impl FileHandler {
gemini_destination: &Path,
files: &[File],
) {
- files.iter().for_each(|file| {
+ for file in files {
self.handle(source, html_destination, gemini_destination, file);
- });
+ }
}
pub fn handle(
@@ -80,7 +80,7 @@ impl FileHandler {
}
}
-pub trait FileHandlerStrategy {
+pub trait Strategy {
fn is(&self, path: &Path) -> bool;
fn identify(&self) -> FileType;
fn can_handle(&self, file_type: &FileType) -> bool;
@@ -182,7 +182,7 @@ mod tests {
file_type: FileType,
}
- impl FileHandlerStrategy for MockStrategy {
+ impl Strategy for MockStrategy {
fn is(&self, _path: &Path) -> bool {
self.is_match
}