From 0fd7b223c2f73b9f7ba41e395ab4de0a01bc9c04 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 21 May 2025 08:41:57 +0000 Subject: refactor(src): Remove unnecessary allocations --- src/html.rs | 53 +++++++++++++++++++++++++------------------ src/main.rs | 3 ++- src/response.rs | 53 +++++++++++++++++++++++++++---------------- src/response/configuration.rs | 28 ++++++++++++++++------- 4 files changed, 87 insertions(+), 50 deletions(-) (limited to 'src') 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 { Some(format!( @@ -98,7 +98,9 @@ pub fn from_gemini( } match node { - Node::Text(text) => html.push_str(&format!("

{}

", safe(text))), + Node::Text(text) => { + let _ = write!(&mut html, "

{}

", 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!( - "

{} Embedded below

\n", + let _ = writeln!( + &mut html, + "

{} Embedded below

", href, safe(text.as_ref().unwrap_or(to)), - )); + ); } - html.push_str(&format!( - "

\"{}\"

\n", + let _ = writeln!( + &mut html, + "

\"{}\"

", 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#"{}{}"#, 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, "<{}>{}", match level { 1 => "h1", @@ -247,25 +253,28 @@ pub fn from_gemini( _ => "p", }, safe(text), - )); + ); + } + Node::List(items) => { + let _ = write!( + &mut html, + "", + items + .iter() + .map(|i| format!("
  • {}
  • ", safe(i))) + .collect::>() + .join("\n") + ); } - Node::List(items) => html.push_str(&format!( - "
      {}
    ", - items - .iter() - .map(|i| format!("
  • {}
  • ", safe(i))) - .collect::>() - .join("\n") - )), Node::Blockquote(text) => { - html.push_str(&format!("
    {}
    ", safe(text))); + let _ = write!(&mut html, "
    {}
    ", safe(text)); } Node::PreformattedText { text, .. } => { let mut new_text = text.to_string(); new_text.pop(); - html.push_str(&format!("
    {new_text}
    ")); + let _ = write!(&mut html, "
    {new_text}
    "); } 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::>(); for stylesheet in stylesheets { - html_context.push_str(&format!( + let _ = write!( + &mut html_context, "", - )); + ); } } else if !configuration.is_no_css() { - html_context.push_str(&format!(r#""#)); + let _ = write!( + &mut html_context, + r#""# + ); if let Ok(primary) = var("PRIMARY_COLOUR") { - html_context - .push_str(&format!("")); + let _ = write!( + &mut html_context, + "" + ); } else { - html_context - .push_str(""); + let _ = write!( + &mut html_context, + "" + ); } } if let Ok(favicon) = var("FAVICON_EXTERNAL") { - html_context.push_str(&format!( + let _ = write!( + &mut html_context, "", - )); + ); } 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!("{gemini_title}")); - html_context.push_str(""); + let _ = write!(&mut html_context, "{gemini_title}"); + let _ = write!(&mut html_context, ""); if !http_request.path().starts_with("/proxy") { if let Ok(header) = var("HEADER") { - html_context - .push_str(&format!("
    {header}
    ")); + let _ = write!( + &mut html_context, + "
    {header}
    " + ); } } @@ -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, "
    This page {} redirects to {}.
    ", 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!("

    {}

    ", response.meta())), + _ => { + let _ = write!(&mut html_context, "

    {}

    ", response.meta()); + } } - html_context.push_str(&format!( + let _ = write!( + &mut html_context, "
    \nProxy Information
    Original URL
    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; + } } -- cgit v1.2.3