Skip to content

Commit

Permalink
refactor: remove all explicit clones
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuwn committed Jun 29, 2024
1 parent 3bfc034 commit 4ac7edc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "september"
version = "0.2.23"
version = "0.2.24"
authors = ["Fuwn <[email protected]>"]
edition = "2021"
description = "A simple and efficient Gemini-to-HTTP proxy."
Expand All @@ -21,7 +21,7 @@ opt-level = 3

[dependencies]
# Gemini
germ = { version = "0.4.3", features = ["ast", "meta"] }
germ = { version = "0.4.4", features = ["ast", "meta"] }

# HTTP
actix-web = "4.7.0"
Expand Down
23 changes: 9 additions & 14 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ fn link_from_host_href(url: &Url, href: &str) -> Option<String> {
Some(format!(
"gemini://{}{}{}",
url.domain()?,
{
if href.starts_with('/') {
""
} else {
"/"
}
},
{ if href.starts_with('/') { "" } else { "/" } },
href
))
}
Expand All @@ -21,8 +15,9 @@ pub fn from_gemini(
url: &Url,
is_proxy: bool,
) -> Option<(String, String)> {
let ast_tree =
germ::ast::Ast::from_string(response.content().clone().unwrap_or_default());
let ast_tree = germ::ast::Ast::from_string(
response.content().as_ref().map_or_else(String::default, String::clone),
);
let ast = ast_tree.inner();
let mut html = String::new();
let mut title = String::new();
Expand Down Expand Up @@ -67,7 +62,7 @@ pub fn from_gemini(
match node {
Node::Text(text) => html.push_str(&format!("<p>{}</p>", safe(text))),
Node::Link { to, text } => {
let mut href = to.clone();
let mut href = to.to_string();
let mut surface = false;

if href.contains("://") && !href.starts_with("gemini://") {
Expand Down Expand Up @@ -159,14 +154,14 @@ pub fn from_gemini(
html.push_str(&format!(
"<p><a href=\"{}\">{}</a> <i>Embedded below</i></p>\n",
href,
safe(&text.clone().unwrap_or_else(|| to.clone())),
safe(text.as_ref().unwrap_or(to)),
));
}

html.push_str(&format!(
"<p><img src=\"{}\" alt=\"{}\" /></p>\n",
safe(&href),
safe(&text.clone().unwrap_or_else(|| to.clone())),
safe(text.as_ref().unwrap_or(to)),
));

continue;
Expand All @@ -179,7 +174,7 @@ pub fn from_gemini(
html.push_str(&format!(
"<a href=\"{}\">{}</a>",
href,
safe(&text.clone().unwrap_or_else(|| to.clone())),
safe(text.as_ref().unwrap_or(to)),
));
}
Node::Heading { level, text } => {
Expand All @@ -188,7 +183,7 @@ pub fn from_gemini(
}

if title.is_empty() && *level == 1 {
title = safe(&text.clone()).to_string();
title = safe(text).to_string();
}

html.push_str(&format!(
Expand Down
12 changes: 7 additions & 5 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".<
let convert_time_taken = timer.elapsed();

if is_raw {
html_context.push_str(&response.content().clone().unwrap_or_default());
html_context.push_str(
&response.content().as_ref().map_or_else(String::default, String::clone),
);

return Ok(
HttpResponse::Ok()
Expand Down Expand Up @@ -265,7 +267,7 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".<
</details></body></html>",
url,
response.status(),
i32::from(response.status().clone()),
i32::from(*response.status()),
response.meta(),
response_time_taken.as_nanos() as f64 / 1_000_000.0,
convert_time_taken.as_nanos() as f64 / 1_000_000.0,
Expand All @@ -278,9 +280,9 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".<
path_matches_pattern(r, req.path())
|| path_matches_pattern(r, req.path().trim_end_matches('/'))
}) {
return Ok(
HttpResponse::Ok().body(response.content().clone().unwrap_or_default()),
);
return Ok(HttpResponse::Ok().body(
response.content().as_ref().map_or_else(String::default, String::clone),
));
}
}

Expand Down

0 comments on commit 4ac7edc

Please sign in to comment.