Skip to content

Commit

Permalink
refactor(response): move configuration into struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuwn committed Jul 24, 2024
1 parent 8988e23 commit 7b14add
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
11 changes: 8 additions & 3 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn link_from_host_href(url: &Url, href: &str) -> Option<String> {
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),
Expand Down Expand Up @@ -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('/')
Expand All @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
20 changes: 9 additions & 11 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod configuration;

use {
crate::url::from_path as url_from_path,
actix_web::{Error, HttpResponse},
Expand All @@ -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('?')
Expand All @@ -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) => {
Expand Down Expand Up @@ -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!(
Expand All @@ -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),
);
Expand All @@ -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(
Expand All @@ -142,7 +140,7 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".<
"<link rel=\"stylesheet\" type=\"text/css\" href=\"{stylesheet}\">",
));
}
} else if !is_nocss {
} 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>"#));

if let Ok(primary) = var("PRIMARY_COLOUR") {
Expand Down
23 changes: 23 additions & 0 deletions src/response/configuration.rs
Original file line number Diff line number Diff line change
@@ -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; }
}
16 changes: 7 additions & 9 deletions src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,38 @@ 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<Url, url::ParseError> {
Ok(
#[allow(clippy::blocks_in_conditions)]
match Url::try_from(&*if path.starts_with("/proxy") {
*is_proxy = true;
configuration.set_proxy(true);

format!(
"gemini://{}{}",
path.replace("/proxy/", ""),
if fallback { "/" } else { "" }
)
} else if path.starts_with("/x") {
*is_proxy = true;
configuration.set_proxy(true);

format!(
"gemini://{}{}",
path.replace("/x/", ""),
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://{}{}",
path.replace("/raw/", ""),
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://{}{}",
Expand Down

0 comments on commit 7b14add

Please sign in to comment.