aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-05-21 08:41:57 +0000
committerFuwn <[email protected]>2025-05-21 08:41:57 +0000
commit0fd7b223c2f73b9f7ba41e395ab4de0a01bc9c04 (patch)
tree72162b1998794387819c3eefdb3c0616195d462b /src
parentchore(just): Use base Cargo Justfile from Fuwn/justfiles (diff)
downloadseptember-0fd7b223c2f73b9f7ba41e395ab4de0a01bc9c04.tar.xz
september-0fd7b223c2f73b9f7ba41e395ab4de0a01bc9c04.zip
refactor(src): Remove unnecessary allocations
Diffstat (limited to 'src')
-rw-r--r--src/html.rs53
-rw-r--r--src/main.rs3
-rw-r--r--src/response.rs53
-rw-r--r--src/response/configuration.rs28
4 files changed, 87 insertions, 50 deletions
diff --git a/src/html.rs b/src/html.rs
index 933ce8e..609be98 100644
--- a/src/html.rs
+++ b/src/html.rs
@@ -1,4 +1,4 @@
-use {germ::ast::Node, std::env::var, url::Url};
+use {germ::ast::Node, std::env::var, std::fmt::Write, url::Url};
fn link_from_host_href(url: &Url, href: &str) -> Option<String> {
Some(format!(
@@ -98,7 +98,9 @@ pub fn from_gemini(
}
match node {
- Node::Text(text) => html.push_str(&format!("<p>{}</p>", safe(text))),
+ Node::Text(text) => {
+ let _ = write!(&mut html, "<p>{}</p>", safe(text));
+ }
Node::Link { to, text } => {
let mut href = to.to_string();
let mut surface = false;
@@ -202,18 +204,20 @@ pub fn from_gemini(
|| extension == "svg"
{
if embed_images == "1" {
- html.push_str(&format!(
- "<p><a href=\"{}\">{}</a> <i>Embedded below</i></p>\n",
+ let _ = writeln!(
+ &mut html,
+ "<p><a href=\"{}\">{}</a> <i>Embedded below</i></p>",
href,
safe(text.as_ref().unwrap_or(to)),
- ));
+ );
}
- html.push_str(&format!(
- "<p><img src=\"{}\" alt=\"{}\" /></p>\n",
+ let _ = writeln!(
+ &mut html,
+ "<p><img src=\"{}\" alt=\"{}\" /></p>",
safe(&href),
safe(text.as_ref().unwrap_or(to)),
- ));
+ );
continue;
}
@@ -222,12 +226,13 @@ pub fn from_gemini(
previous_link = true;
- html.push_str(&format!(
+ let _ = write!(
+ &mut html,
r#"{}<a href="{}">{}</a>"#,
if condense_links { "" } else { GEMINI_FRAGMENT },
href,
safe(text.as_ref().unwrap_or(to)).trim(),
- ));
+ );
}
Node::Heading { level, text } => {
if !condensible_headings.contains(&node.to_gemtext().as_str()) {
@@ -238,7 +243,8 @@ pub fn from_gemini(
title = safe(text).to_string();
}
- html.push_str(&format!(
+ let _ = write!(
+ &mut html,
"<{}>{}</{0}>",
match level {
1 => "h1",
@@ -247,25 +253,28 @@ pub fn from_gemini(
_ => "p",
},
safe(text),
- ));
+ );
+ }
+ Node::List(items) => {
+ let _ = write!(
+ &mut html,
+ "<ul>{}</ul>",
+ items
+ .iter()
+ .map(|i| format!("<li>{}</li>", safe(i)))
+ .collect::<Vec<String>>()
+ .join("\n")
+ );
}
- Node::List(items) => html.push_str(&format!(
- "<ul>{}</ul>",
- items
- .iter()
- .map(|i| format!("<li>{}</li>", safe(i)))
- .collect::<Vec<String>>()
- .join("\n")
- )),
Node::Blockquote(text) => {
- html.push_str(&format!("<blockquote>{}</blockquote>", safe(text)));
+ let _ = write!(&mut html, "<blockquote>{}</blockquote>", safe(text));
}
Node::PreformattedText { text, .. } => {
let mut new_text = text.to_string();
new_text.pop();
- html.push_str(&format!("<pre>{new_text}</pre>"));
+ let _ = write!(&mut html, "<pre>{new_text}</pre>");
}
Node::Whitespace => {}
}
diff --git a/src/main.rs b/src/main.rs
index a7374c3..9fe7910 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -14,7 +14,8 @@ mod html;
mod response;
mod url;
-#[macro_use] extern crate log;
+#[macro_use]
+extern crate log;
use {actix_web::web, response::default, std::env::var};
diff --git a/src/response.rs b/src/response.rs
index 2e73ff3..9a86f65 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -3,7 +3,7 @@ pub mod configuration;
use {
crate::url::from_path as url_from_path,
actix_web::{Error, HttpResponse},
- std::{env::var, time::Instant},
+ std::{env::var, fmt::Write, time::Instant},
};
const CSS: &str = include_str!("../default.css");
@@ -137,26 +137,35 @@ pub async fn default(
css.split(',').filter(|s| !s.is_empty()).collect::<Vec<_>>();
for stylesheet in stylesheets {
- html_context.push_str(&format!(
+ let _ = write!(
+ &mut html_context,
"<link rel=\"stylesheet\" type=\"text/css\" href=\"{stylesheet}\">",
- ));
+ );
}
} else if !configuration.is_no_css() {
- html_context.push_str(&format!(r#"<link rel="stylesheet" href="https://latex.vercel.app/style.css"><style>{CSS}</style>"#));
+ let _ = write!(
+ &mut html_context,
+ r#"<link rel="stylesheet" href="https://latex.vercel.app/style.css"><style>{CSS}</style>"#
+ );
if let Ok(primary) = var("PRIMARY_COLOUR") {
- html_context
- .push_str(&format!("<style>:root {{ --primary: {primary} }}</style>"));
+ let _ = write!(
+ &mut html_context,
+ "<style>:root {{ --primary: {primary} }}</style>"
+ );
} else {
- html_context
- .push_str("<style>:root { --primary: var(--base0D); }</style>");
+ let _ = write!(
+ &mut html_context,
+ "<style>:root {{ --primary: var(--base0D); }}</style>"
+ );
}
}
if let Ok(favicon) = var("FAVICON_EXTERNAL") {
- html_context.push_str(&format!(
+ let _ = write!(
+ &mut html_context,
"<link rel=\"icon\" type=\"image/x-icon\" href=\"{favicon}\">",
- ));
+ );
}
if var("MATHJAX").unwrap_or_else(|_| "true".to_string()).to_lowercase()
@@ -173,13 +182,15 @@ pub async fn default(
html_context.push_str(&head);
}
- html_context.push_str(&format!("<title>{gemini_title}</title>"));
- html_context.push_str("</head><body>");
+ let _ = write!(&mut html_context, "<title>{gemini_title}</title>");
+ let _ = write!(&mut html_context, "</head><body>");
if !http_request.path().starts_with("/proxy") {
if let Ok(header) = var("HEADER") {
- html_context
- .push_str(&format!("<big><blockquote>{header}</blockquote></big>"));
+ let _ = write!(
+ &mut html_context,
+ "<big><blockquote>{header}</blockquote></big>"
+ );
}
}
@@ -188,7 +199,8 @@ pub async fn default(
if let (Some(status), Some(url)) =
(redirect_response_status, redirect_url)
{
- html_context.push_str(&format!(
+ let _ = write!(
+ &mut html_context,
"<blockquote>This page {} redirects to <a \
href=\"{}\">{}</a>.</blockquote>",
if status == germ::request::Status::PermanentRedirect {
@@ -198,15 +210,18 @@ pub async fn default(
},
url,
url
- ));
+ );
}
html_context.push_str(&gemini_html.1);
}
- _ => html_context.push_str(&format!("<p>{}</p>", response.meta())),
+ _ => {
+ let _ = write!(&mut html_context, "<p>{}</p>", response.meta());
+ }
}
- html_context.push_str(&format!(
+ let _ = write!(
+ &mut html_context,
"<details>\n<summary>Proxy Information</summary>
<dl>
<dt>Original URL</dt><dd><a \
@@ -233,7 +248,7 @@ pub async fn default(
convert_time_taken.as_nanos() as f64 / 1_000_000.0,
format_args!("/tree/{}", env!("VERGEN_GIT_SHA")),
env!("VERGEN_GIT_SHA").get(0..5).unwrap_or("UNKNOWN"),
- ));
+ );
if let Ok(plain_texts) = var("PLAIN_TEXT_ROUTE") {
if plain_texts.split(',').any(|r| {
diff --git a/src/response/configuration.rs b/src/response/configuration.rs
index 4278516..42b42f1 100644
--- a/src/response/configuration.rs
+++ b/src/response/configuration.rs
@@ -1,6 +1,6 @@
pub struct Configuration {
- is_proxy: bool,
- is_raw: bool,
+ is_proxy: bool,
+ is_raw: bool,
is_no_css: bool,
}
@@ -9,15 +9,27 @@ impl Configuration {
Self { is_proxy: false, is_raw: false, is_no_css: false }
}
- pub const fn is_proxy(&self) -> bool { self.is_proxy }
+ pub const fn is_proxy(&self) -> bool {
+ self.is_proxy
+ }
- pub const fn is_raw(&self) -> bool { self.is_raw }
+ pub const fn is_raw(&self) -> bool {
+ self.is_raw
+ }
- pub const fn is_no_css(&self) -> bool { self.is_no_css }
+ pub const fn is_no_css(&self) -> bool {
+ self.is_no_css
+ }
- pub fn set_proxy(&mut self, is_proxy: bool) { self.is_proxy = is_proxy; }
+ pub fn set_proxy(&mut self, is_proxy: bool) {
+ self.is_proxy = is_proxy;
+ }
- pub fn set_raw(&mut self, is_raw: bool) { self.is_raw = is_raw; }
+ pub fn set_raw(&mut self, is_raw: bool) {
+ self.is_raw = is_raw;
+ }
- pub fn set_no_css(&mut self, is_no_css: bool) { self.is_no_css = is_no_css; }
+ pub fn set_no_css(&mut self, is_no_css: bool) {
+ self.is_no_css = is_no_css;
+ }
}