aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-01-04 02:20:55 +0100
committerRuben Beltran del Rio <git@r.bdr.sh>2025-01-04 02:20:55 +0100
commit8766e4412b95cfa0288683748cc20aba81a64d08 (patch)
treeb068bbd32915b2c0ca846465edfd6cb9bf718d6c /src
parentb03284133baa6339fe4adf48e4a6499ef3ac287b (diff)
Address pedantic issues
Diffstat (limited to 'src')
-rw-r--r--src/file_finder.rs2
-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
-rw-r--r--src/gemini_parser.rs6
-rw-r--r--src/html_renderer.rs36
-rw-r--r--src/main.rs8
8 files changed, 60 insertions, 66 deletions
diff --git a/src/file_finder.rs b/src/file_finder.rs
index a9b84f8..0512146 100644
--- a/src/file_finder.rs
+++ b/src/file_finder.rs
@@ -17,7 +17,7 @@ fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec<
continue;
}
if path.is_dir() {
- result.append(&mut find_files_recursively(root_path, &path))
+ result.append(&mut find_files_recursively(root_path, &path));
} else {
let file_type = file_handler.identify(&path);
result.push(File { path, file_type });
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
}
diff --git a/src/gemini_parser.rs b/src/gemini_parser.rs
index e6de4ec..49d954a 100644
--- a/src/gemini_parser.rs
+++ b/src/gemini_parser.rs
@@ -8,7 +8,7 @@ pub enum GeminiLine {
ListItem(String),
}
-/// Parses gemtext source code into a vector of GeminiLine elements.
+/// Parses gemtext source code into a vector of `GeminiLine` elements.
///
/// # Arguments
/// * `source` - A string slice that contains the gemtext
@@ -47,7 +47,7 @@ fn parse_line(line: &str) -> GeminiLine {
match line {
s if s.starts_with("###") => GeminiLine::Heading(3, s[3..].to_string()),
s if s.starts_with("##") => GeminiLine::Heading(2, s[2..].to_string()),
- s if s.starts_with("#") => GeminiLine::Heading(1, s[1..].to_string()),
+ s if s.starts_with('#') => GeminiLine::Heading(1, s[1..].to_string()),
s if s.starts_with("=>") => {
let content = s[2..].trim();
match content.split_once(char::is_whitespace) {
@@ -58,7 +58,7 @@ fn parse_line(line: &str) -> GeminiLine {
}
}
s if s.starts_with("* ") => GeminiLine::ListItem(s[2..].to_string()),
- s if s.starts_with(">") => GeminiLine::Quote(s[1..].to_string()),
+ s if s.starts_with('>') => GeminiLine::Quote(s[1..].to_string()),
s if s.starts_with("```") => GeminiLine::PreformattedToggle(true, s[3..].to_string()),
_ => GeminiLine::Text(line.to_string(), false),
}
diff --git a/src/html_renderer.rs b/src/html_renderer.rs
index 1b26bff..7724131 100644
--- a/src/html_renderer.rs
+++ b/src/html_renderer.rs
@@ -1,18 +1,18 @@
use crate::gemini_parser::GeminiLine;
-/// Renders HTML from a vector of GeminiLine elements.
+/// Renders HTML from a vector of `GeminiLine` elements.
///
/// # Arguments
-/// * `lines` - Vector of GeminiLine elements to render
+/// * `lines` - Vector of `GeminiLine` elements to render
///
/// # Returns
/// A String containing the rendered HTML.
-pub fn render_html(lines: Vec<GeminiLine>) -> String {
+pub fn render_html(lines: &[GeminiLine]) -> String {
let mut heading_stack = Vec::new();
let mut last_line: Option<&GeminiLine> = None;
let mut result = String::new();
- for line in &lines {
+ for line in lines {
result.push_str(&line_preamble(line, last_line, &mut heading_stack));
result.push_str(&line_content(line));
result.push('\n');
@@ -61,7 +61,7 @@ fn line_preamble(
}
}
heading_stack.push(*level);
- html.push_str(&format!("<section class=\"h{}\">\n", level));
+ html.push_str(&format!("<section class=\"h{level}\">\n"));
}
GeminiLine::ListItem(_) => match last_line {
Some(GeminiLine::ListItem(_)) => {}
@@ -79,15 +79,15 @@ fn line_preamble(
fn line_content(line: &GeminiLine) -> String {
match line {
- GeminiLine::Text(content, false) => format!("<p>{}</p>", content),
+ GeminiLine::Text(content, false) => format!("<p>{content}</p>"),
GeminiLine::Link(url, text) => {
let display = if text.is_empty() { url } else { text };
- format!("<p class=\"a\"><a href=\"{}\">{}</a></p>", url, display)
+ format!("<p class=\"a\"><a href=\"{url}\">{display}</a></p>")
}
- GeminiLine::Heading(level, content) => format!("<h{}>{}</h{}>", level, content, level),
- GeminiLine::ListItem(content) => format!("<li>{}</li>", content),
+ GeminiLine::Heading(level, content) => format!("<h{level}>{content}</h{level}>"),
+ GeminiLine::ListItem(content) => format!("<li>{content}</li>"),
GeminiLine::PreformattedToggle(true, alt_text) => {
- format!("<pre aria-label=\"{}\">", alt_text)
+ format!("<pre aria-label=\"{alt_text}\">")
}
GeminiLine::PreformattedToggle(false, _) => "</pre>".to_string(),
GeminiLine::Text(content, true) | GeminiLine::Quote(content) => content.to_string(),
@@ -117,7 +117,7 @@ mod tests {
#[test]
fn test_simple_text() {
let input = vec![GeminiLine::Text("Hello world".to_string(), false)];
- assert_eq!(render_html(input), "<p>Hello world</p>\n");
+ assert_eq!(render_html(&input), "<p>Hello world</p>\n");
}
#[test]
@@ -129,7 +129,7 @@ mod tests {
GeminiLine::Heading(2, "Another Sub".to_string()),
];
assert_eq!(
- render_html(input),
+ render_html(&input),
"<section class=\"h1\">\n\
<h1>Top</h1>\n\
<section class=\"h2\">\n\
@@ -154,7 +154,7 @@ mod tests {
GeminiLine::ListItem("New list".to_string()),
];
assert_eq!(
- render_html(input),
+ render_html(&input),
"<ul>\n\
<li>First</li>\n\
<li>Second</li>\n\
@@ -175,7 +175,7 @@ mod tests {
GeminiLine::Quote("New quote".to_string()),
];
assert_eq!(
- render_html(input),
+ render_html(&input),
"<blockquote>\n\
First quote\n\
Still quoting\n\
@@ -195,7 +195,7 @@ mod tests {
GeminiLine::PreformattedToggle(false, String::new()),
];
assert_eq!(
- render_html(input),
+ render_html(&input),
"<pre aria-label=\"code\">\n\
let x = 42;\n\
</pre>\n"
@@ -209,7 +209,7 @@ mod tests {
GeminiLine::Link("https://rust-lang.org".to_string(), "".to_string()),
];
assert_eq!(
- render_html(input),
+ render_html(&input),
"<p class=\"a\"><a href=\"https://example.com\">Example</a></p>\n\
<p class=\"a\"><a href=\"https://rust-lang.org\">https://rust-lang.org</a></p>\n"
);
@@ -227,7 +227,7 @@ mod tests {
GeminiLine::Heading(2, "Another Section".to_string()),
];
assert_eq!(
- render_html(input),
+ render_html(&input),
"<section class=\"h1\">\n\
<h1>Title</h1>\n\
<p>Intro</p>\n\
@@ -251,6 +251,6 @@ mod tests {
#[test]
fn test_empty_input() {
let input = Vec::new();
- assert_eq!(render_html(input), "");
+ assert_eq!(render_html(&input), "");
}
}
diff --git a/src/main.rs b/src/main.rs
index cedd557..6c7b8a9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -15,9 +15,9 @@ fn main() -> Result<()> {
let source = current_dir()?;
let source_name = source.file_name().unwrap().to_string_lossy();
let parent = source.parent().unwrap();
- let gemini_destination_name = format!("{}_gemini", source_name);
+ let gemini_destination_name = format!("{source_name}_gemini");
let gemini_destination = parent.join(gemini_destination_name);
- let html_destination_name = format!("{}_html", source_name);
+ let html_destination_name = format!("{source_name}_html");
let html_destination = parent.join(html_destination_name);
// Step 1. Identify the files
@@ -26,9 +26,9 @@ fn main() -> Result<()> {
// Step 2. Load the layout
let mut file_handler = FileHandler::default();
match file_handler.get_layout_or_panic(&files) {
- Ok(_) => {}
+ Ok(()) => {}
Err(error) => {
- eprintln!("{}", error);
+ eprintln!("{error}");
exit(1);
}
}