From 7f8c4ee79784ec7ae5983d40ad309e337986e7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 21 Dec 2025 16:51:25 +0100 Subject: [PATCH 1/8] Simplify URL creation Use a single format!() invocation for each case and avoid mutable variable. --- crates/wastebin_server/src/handlers/insert/form.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/wastebin_server/src/handlers/insert/form.rs b/crates/wastebin_server/src/handlers/insert/form.rs index 1cad21e6..78d2af80 100644 --- a/crates/wastebin_server/src/handlers/insert/form.rs +++ b/crates/wastebin_server/src/handlers/insert/form.rs @@ -67,14 +67,17 @@ pub async fn post( entry.uid = Some(uid); let id = Id::rand(); - let mut url = id.to_url_path(&entry); - if entry.burn_after_reading.unwrap_or(false) { - url = format!("burn/{url}"); - } + let url = { + let url_path = id.to_url_path(&entry); + if entry.burn_after_reading.unwrap_or(false) { + format!("/burn/{url_path}") + } else { + format!("/{url_path}") + } + }; db.insert(id, entry).await?; - let url = format!("/{url}"); let cookie = Cookie::build(("uid", uid.to_string())) .http_only(true) From c273cf3ba1a5bd87825c6f7e206ad0a382ac2d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 21 Dec 2025 16:52:02 +0100 Subject: [PATCH 2/8] Use .to_string() instead of bare format! --- crates/wastebin_server/src/handlers/download.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wastebin_server/src/handlers/download.rs b/crates/wastebin_server/src/handlers/download.rs index 8c069e44..a8a12e3d 100644 --- a/crates/wastebin_server/src/handlers/download.rs +++ b/crates/wastebin_server/src/handlers/download.rs @@ -40,7 +40,7 @@ pub async fn get( } fn get_download(key: &Key, data: Data) -> impl IntoResponse { - let filename = data.metadata.title.unwrap_or_else(|| format!("{key}")); + let filename = data.metadata.title.unwrap_or_else(|| key.to_string()); let content_type = "text; charset=utf-8"; let content_disposition = From f1e7f141d6e57805861425147657c8c865e96969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 21 Dec 2025 16:52:58 +0100 Subject: [PATCH 3/8] Reuse string memory for HeaderValue --- crates/wastebin_server/src/handlers/download.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wastebin_server/src/handlers/download.rs b/crates/wastebin_server/src/handlers/download.rs index a8a12e3d..2b728914 100644 --- a/crates/wastebin_server/src/handlers/download.rs +++ b/crates/wastebin_server/src/handlers/download.rs @@ -44,7 +44,7 @@ fn get_download(key: &Key, data: Data) -> impl IntoResponse { let content_type = "text; charset=utf-8"; let content_disposition = - HeaderValue::from_str(&format!(r#"attachment; filename="{filename}""#)) + HeaderValue::try_from(format!(r#"attachment; filename="{filename}""#)) .expect("constructing valid header value"); ( From 01a08529320b15d4c740f2ac019bfd23f03d7cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 21 Dec 2025 16:55:22 +0100 Subject: [PATCH 4/8] Rename syntaxes of Highlighter and tweak sorting Rename to clarify the distinction to syntax_set.syntaxes(). Simplify initial sorting since the keys are simple strings. --- crates/wastebin_highlight/src/highlight.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/crates/wastebin_highlight/src/highlight.rs b/crates/wastebin_highlight/src/highlight.rs index 6284f8d7..60da6784 100644 --- a/crates/wastebin_highlight/src/highlight.rs +++ b/crates/wastebin_highlight/src/highlight.rs @@ -1,4 +1,3 @@ -use std::cmp::Ordering; use std::fmt::Write; use syntect::html::{ClassStyle, line_tokens_to_classed_spans}; @@ -26,7 +25,7 @@ pub struct Html(String); #[derive(Clone)] pub struct Highlighter { syntax_set: SyntaxSet, - syntaxes: Vec, + ordered_syntaxes: Vec, } /// Syntax reference. @@ -41,16 +40,11 @@ impl Default for Highlighter { fn default() -> Self { let syntax_set = two_face::syntax::extra_newlines(); let mut syntaxes = syntax_set.syntaxes().to_vec(); - syntaxes.sort_by(|a, b| { - a.name - .to_lowercase() - .partial_cmp(&b.name.to_lowercase()) - .unwrap_or(Ordering::Less) - }); + syntaxes.sort_unstable_by_key(|s| s.name.to_lowercase()); Self { syntax_set, - syntaxes, + ordered_syntaxes: syntaxes, } } } @@ -244,7 +238,7 @@ impl Highlighter { /// Return iterator over all available [`Syntax`]es with their canonical name and usual file /// extensions. pub fn syntaxes(&self) -> impl Iterator> { - self.syntaxes.iter().map(|syntax| Syntax { + self.ordered_syntaxes.iter().map(|syntax| Syntax { name: syntax.name.as_ref(), extensions: syntax.file_extensions.as_slice(), }) From 66f1237b4a832fef5529a781d6b10ba84c26ec0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 21 Dec 2025 16:55:57 +0100 Subject: [PATCH 5/8] Avoid unreachable branch --- crates/wastebin_highlight/src/highlight.rs | 28 ++++++++++------------ 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/crates/wastebin_highlight/src/highlight.rs b/crates/wastebin_highlight/src/highlight.rs index 60da6784..1e0b2480 100644 --- a/crates/wastebin_highlight/src/highlight.rs +++ b/crates/wastebin_highlight/src/highlight.rs @@ -56,22 +56,18 @@ fn escape(s: &str, buf: &mut String) -> std::fmt::Result { let pile_o_bits = s; let mut last = 0; for (i, ch) in s.bytes().enumerate() { - match ch as char { - '<' | '>' | '&' | '\'' | '"' => { - buf.write_str(&pile_o_bits[last..i])?; - let s = match ch as char { - '>' => ">", - '<' => "<", - '&' => "&", - '\'' => "'", - '"' => """, - _ => unreachable!(), - }; - buf.write_str(s)?; - last = i + 1; - } - _ => {} - } + let escaping = match ch as char { + '>' => ">", + '<' => "<", + '&' => "&", + '\'' => "'", + '"' => """, + _ => continue, + }; + + buf.write_str(&pile_o_bits[last..i])?; + buf.write_str(escaping)?; + last = i + 1; } if last < s.len() { From f51bb44e428d3ce1874172239f3f8d6de0943e82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 21 Dec 2025 16:56:32 +0100 Subject: [PATCH 6/8] Extract common tail function call from if-else branches --- crates/wastebin_highlight/src/highlight.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/wastebin_highlight/src/highlight.rs b/crates/wastebin_highlight/src/highlight.rs index 1e0b2480..3f612488 100644 --- a/crates/wastebin_highlight/src/highlight.rs +++ b/crates/wastebin_highlight/src/highlight.rs @@ -125,10 +125,10 @@ fn line_tokens_to_classed_spans_md( // Insert href and close attribute ... escape(&line[cur_index..i], &mut s)?; s.push_str(r#"">"#); - escape(&line[cur_index..i], &mut s)?; - } else { - escape(&line[cur_index..i], &mut s)?; } + + escape(&line[cur_index..i], &mut s)?; + cur_index = i; } stack.apply_with_hook(op, |basic_op, _| match basic_op { From 70e66d79fceea1a5f7cca67dea812e022180c615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 21 Dec 2025 16:59:14 +0100 Subject: [PATCH 7/8] Preallocate string capacity to minimize intermediate allocations --- crates/wastebin_highlight/src/highlight.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/wastebin_highlight/src/highlight.rs b/crates/wastebin_highlight/src/highlight.rs index 3f612488..e4768d1f 100644 --- a/crates/wastebin_highlight/src/highlight.rs +++ b/crates/wastebin_highlight/src/highlight.rs @@ -215,6 +215,7 @@ impl Highlighter { } // Strip stray newlines that cause vertically stretched lines. + html.reserve(formatted.len()); for c in formatted.chars().filter(|c| *c != '\n') { html.push(c); } From 8bf8bb90c5f562e84b9ad21cdc24ef24f89d55d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 21 Dec 2025 17:01:41 +0100 Subject: [PATCH 8/8] Use format arguments instead to merge string formatting Inline main_colors variable to support rust 1.88. --- crates/wastebin_highlight/src/theme.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/wastebin_highlight/src/theme.rs b/crates/wastebin_highlight/src/theme.rs index 9a8f8e04..59360b2b 100644 --- a/crates/wastebin_highlight/src/theme.rs +++ b/crates/wastebin_highlight/src/theme.rs @@ -114,17 +114,16 @@ fn combined_css(color_scheme: &str, theme: &highlighting::Theme) -> Vec { let fg = theme.settings.foreground.expect("existing color"); let bg = theme.settings.background.expect("existing color"); - let main_colors = format!( - ":root {{ - color-scheme: {color_scheme}; - --main-bg-color: rgb({}, {}, {}, {}); - --main-fg-color: rgb({}, {}, {}, {}); -}}", - bg.r, bg.g, bg.b, bg.a, fg.r, fg.g, fg.b, fg.a - ); - format!( - "{main_colors} {}", + "{} {}", + format_args!( + ":root {{ + color-scheme: {color_scheme}; + --main-bg-color: rgb({}, {}, {}, {}); + --main-fg-color: rgb({}, {}, {}, {}); + }}", + bg.r, bg.g, bg.b, bg.a, fg.r, fg.g, fg.b, fg.a + ), css_for_theme_with_class_style(theme, ClassStyle::Spaced).expect("generating CSS") ) .into_bytes()