aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-01-21 14:50:53 +0000
committerFuwn <[email protected]>2026-01-21 14:50:53 +0000
commit9b0e10ddaeacda3da9d589f7577bcf364609eb9c (patch)
treecae49b769c7c153495550bff2d3a16152afb9151 /src
parentchore(Cargo): Bump version patch (diff)
downloadseptember-9b0e10ddaeacda3da9d589f7577bcf364609eb9c.tar.xz
september-9b0e10ddaeacda3da9d589f7577bcf364609eb9c.zip
fix(html,url): Resolve clippy pedantic lints and reduce allocations
Diffstat (limited to 'src')
-rw-r--r--src/html.rs31
-rw-r--r--src/url.rs33
2 files changed, 30 insertions, 34 deletions
diff --git a/src/html.rs b/src/html.rs
index 341286c..ae79489 100644
--- a/src/html.rs
+++ b/src/html.rs
@@ -76,14 +76,18 @@ pub fn from_gemini(
let align_adjacent_links = |html: &str| {
if previous_link_count > 0 {
- html
- .chars()
- .rev()
- .collect::<String>()
- .replacen(&GEMINI_FRAGMENT.chars().rev().collect::<String>(), "", 1)
- .chars()
- .rev()
- .collect::<String>()
+ html.rfind(GEMINI_FRAGMENT).map_or_else(
+ || html.to_string(),
+ |position| {
+ let mut result =
+ String::with_capacity(html.len() - GEMINI_FRAGMENT.len());
+
+ result.push_str(&html[..position]);
+ result.push_str(&html[position + GEMINI_FRAGMENT.len()..]);
+
+ result
+ },
+ )
} else {
html.to_string()
}
@@ -121,7 +125,7 @@ pub fn from_gemini(
let _ = write!(&mut html, "<p>{}</p>", safe(text));
}
Node::Link { to, text } => {
- let mut href = to.to_string();
+ let mut href = to.clone();
let mut surface = false;
if href.starts_with("./") || href.starts_with("../") {
@@ -133,7 +137,7 @@ pub fn from_gemini(
if href.contains("://") && !href.starts_with("gemini://") {
surface = true;
} else if !href.contains("://") && href.contains(':') {
- href = href.to_string();
+ // href contains a scheme-like pattern (e.g., mailto:), keep as-is
} else if !href.starts_with("gemini://") && !href.starts_with('/') {
href = format!(
"{}/{}",
@@ -255,7 +259,7 @@ pub fn from_gemini(
}
if title.is_empty() && *level == 1 {
- title = safe(text).to_string();
+ title = safe(text);
}
let _ = write!(
@@ -285,10 +289,7 @@ pub fn from_gemini(
let _ = write!(&mut html, "<blockquote>{}</blockquote>", safe(text));
}
Node::PreformattedText { text, .. } => {
- let mut new_text = text.to_string();
-
- new_text.pop();
-
+ let new_text = text.strip_suffix('\n').unwrap_or(text);
let _ = write!(&mut html, "<pre>{new_text}</pre>");
}
Node::Whitespace => {}
diff --git a/src/url.rs b/src/url.rs
index 5289d28..56858e6 100644
--- a/src/url.rs
+++ b/src/url.rs
@@ -63,11 +63,11 @@ pub fn matches_pattern(pattern: &str, path: &str) -> bool {
return path == pattern;
}
- let parts: Vec<&str> = pattern.split('*').collect();
+ let mut parts = pattern.split('*').peekable();
let mut position = if pattern.starts_with('*') {
0
} else {
- let first = parts.first().unwrap();
+ let first = parts.next().unwrap_or("");
if !path.starts_with(first) {
return false;
@@ -75,25 +75,20 @@ pub fn matches_pattern(pattern: &str, path: &str) -> bool {
first.len()
};
- let before_last = parts.len().saturating_sub(1);
- for part in &parts[1..before_last] {
- if part.is_empty() {
- continue;
- }
+ while let Some(part) = parts.next() {
+ let is_last = parts.peek().is_none();
- if let Some(found) = path[position..].find(part) {
- position += found + part.len();
- } else {
- return false;
- }
- }
-
- if !pattern.ends_with('*') {
- let last = parts.last().unwrap();
-
- if !path[position..].ends_with(last) {
- return false;
+ if is_last {
+ if !pattern.ends_with('*') && !path[position..].ends_with(part) {
+ return false;
+ }
+ } else if !part.is_empty() {
+ if let Some(found) = path[position..].find(part) {
+ position += found + part.len();
+ } else {
+ return false;
+ }
}
}