From 7b14add5eee90468c8ed8dd60b6afe648e4a822b Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 23 Jul 2024 17:38:05 -0700 Subject: [PATCH] refactor(response): move configuration into struct --- src/html.rs | 11 ++++++++--- src/main.rs | 3 +-- src/response.rs | 20 +++++++++----------- src/response/configuration.rs | 23 +++++++++++++++++++++++ src/url.rs | 16 +++++++--------- 5 files changed, 48 insertions(+), 25 deletions(-) create mode 100644 src/response/configuration.rs diff --git a/src/html.rs b/src/html.rs index 9203656..81012ed 100644 --- a/src/html.rs +++ b/src/html.rs @@ -13,7 +13,7 @@ fn link_from_host_href(url: &Url, href: &str) -> Option { pub fn from_gemini( response: &germ::request::Response, url: &Url, - is_proxy: bool, + configuration: &crate::response::configuration::Configuration, ) -> Option<(String, String)> { let ast_tree = germ::ast::Ast::from_string( response.content().as_ref().map_or_else(String::default, String::clone), @@ -92,7 +92,8 @@ pub fn from_gemini( && href.contains("gemini://") && !surface { - if is_proxy + if (configuration.is_proxy()) + || configuration.is_no_css() || href .trim_start_matches("gemini://") .trim_end_matches('/') @@ -102,7 +103,11 @@ pub fn from_gemini( .unwrap() != &url.host().unwrap().to_string().as_str() { - href = format!("/proxy/{}", href.trim_start_matches("gemini://")); + href = format!( + "/{}/{}", + if configuration.is_no_css() { "nocss" } else { "proxy" }, + href.trim_start_matches("gemini://") + ); } else { href = href.trim_start_matches("gemini://").replacen( &if let Some(host) = url.host() { diff --git a/src/main.rs b/src/main.rs index 9fe7910..a7374c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,8 +14,7 @@ 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 eceaa30..7b9a1d3 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,3 +1,5 @@ +pub mod configuration; + use { crate::url::from_path as url_from_path, actix_web::{Error, HttpResponse}, @@ -22,9 +24,7 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< ); } - let mut is_proxy = false; - let mut is_raw = false; - let mut is_nocss = false; + let mut configuration = configuration::Configuration::new(); let url = match url_from_path( &format!("{}{}", req.path(), { if !req.query_string().is_empty() || req.uri().to_string().ends_with('?') @@ -35,9 +35,7 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< } }), false, - &mut is_proxy, - &mut is_raw, - &mut is_nocss, + &mut configuration, ) { Ok(url) => url, Err(e) => { @@ -94,7 +92,7 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< timer = Instant::now(); - let mut html_context = if is_raw { + let mut html_context = if configuration.is_raw() { String::new() } else { format!( @@ -107,11 +105,11 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< ) }; let gemini_html = - crate::html::from_gemini(&response, &url, is_proxy).unwrap(); + crate::html::from_gemini(&response, &url, &configuration).unwrap(); let gemini_title = gemini_html.0; let convert_time_taken = timer.elapsed(); - if is_raw { + if configuration.is_raw() { html_context.push_str( &response.content().as_ref().map_or_else(String::default, String::clone), ); @@ -123,7 +121,7 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< ); } - if is_nocss { + if configuration.is_no_css() { html_context.push_str(&gemini_html.1); return Ok( @@ -142,7 +140,7 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< "", )); } - } else if !is_nocss { + } else if !configuration.is_no_css() { html_context.push_str(&format!(r#""#)); if let Ok(primary) = var("PRIMARY_COLOUR") { diff --git a/src/response/configuration.rs b/src/response/configuration.rs new file mode 100644 index 0000000..4278516 --- /dev/null +++ b/src/response/configuration.rs @@ -0,0 +1,23 @@ +pub struct Configuration { + is_proxy: bool, + is_raw: bool, + is_no_css: bool, +} + +impl Configuration { + pub const fn new() -> Self { + 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_raw(&self) -> bool { self.is_raw } + + 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_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; } +} diff --git a/src/url.rs b/src/url.rs index 29cb99c..f6863a5 100644 --- a/src/url.rs +++ b/src/url.rs @@ -3,14 +3,12 @@ use url::Url; pub fn from_path( path: &str, fallback: bool, - is_proxy: &mut bool, - is_raw: &mut bool, - is_nocss: &mut bool, + configuration: &mut crate::response::configuration::Configuration, ) -> Result { Ok( #[allow(clippy::blocks_in_conditions)] match Url::try_from(&*if path.starts_with("/proxy") { - *is_proxy = true; + configuration.set_proxy(true); format!( "gemini://{}{}", @@ -18,7 +16,7 @@ pub fn from_path( if fallback { "/" } else { "" } ) } else if path.starts_with("/x") { - *is_proxy = true; + configuration.set_proxy(true); format!( "gemini://{}{}", @@ -26,8 +24,8 @@ pub fn from_path( if fallback { "/" } else { "" } ) } else if path.starts_with("/raw") { - *is_proxy = true; - *is_raw = true; + configuration.set_proxy(true); + configuration.set_raw(true); format!( "gemini://{}{}", @@ -35,8 +33,8 @@ pub fn from_path( if fallback { "/" } else { "" } ) } else if path.starts_with("/nocss") { - *is_proxy = true; - *is_nocss = true; + configuration.set_proxy(true); + configuration.set_no_css(true); format!( "gemini://{}{}",