Skip to content

Commit ef2cc01

Browse files
refactor(utils): avoid redundant String conversions & use match (#347)
* refactor(utils): avoid redundant String conversions & use match * ci: fix clippy lint
1 parent 7930b19 commit ef2cc01

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

src/utils.rs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ pub struct Preferences {
638638
pub hide_score: String,
639639
}
640640

641-
fn serialize_vec_with_plus<S>(vec: &Vec<String>, serializer: S) -> Result<S::Ok, S::Error>
641+
fn serialize_vec_with_plus<S>(vec: &[String], serializer: S) -> Result<S::Ok, S::Error>
642642
where
643643
S: Serializer,
644644
{
@@ -915,11 +915,12 @@ pub fn setting_or_default(req: &Request<Body>, name: &str, default: String) -> S
915915
// Detect and redirect in the event of a random subreddit
916916
pub async fn catch_random(sub: &str, additional: &str) -> Result<Response<Body>, String> {
917917
if sub == "random" || sub == "randnsfw" {
918-
let new_sub = json(format!("/r/{sub}/about.json?raw_json=1"), false).await?["data"]["display_name"]
919-
.as_str()
920-
.unwrap_or_default()
921-
.to_string();
922-
Ok(redirect(&format!("/r/{new_sub}{additional}")))
918+
Ok(redirect(&format!(
919+
"/r/{}{additional}",
920+
json(format!("/r/{sub}/about.json?raw_json=1"), false).await?["data"]["display_name"]
921+
.as_str()
922+
.unwrap_or_default()
923+
)))
923924
} else {
924925
Err("No redirect needed".to_string())
925926
}
@@ -1019,8 +1020,7 @@ static REDLIB_PREVIEW_TEXT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r">(.*?)
10191020
pub fn rewrite_urls(input_text: &str) -> String {
10201021
let mut text1 =
10211022
// Rewrite Reddit links to Redlib
1022-
REDDIT_REGEX.replace_all(input_text, r#"href="/"#)
1023-
.to_string();
1023+
REDDIT_REGEX.replace_all(input_text, r#"href="/"#).to_string();
10241024

10251025
loop {
10261026
if REDDIT_EMOJI_REGEX.find(&text1).is_none() {
@@ -1042,49 +1042,44 @@ pub fn rewrite_urls(input_text: &str) -> String {
10421042
} else {
10431043
let formatted_url = format_url(REDDIT_PREVIEW_REGEX.find(&text1).map(|x| x.as_str()).unwrap_or_default());
10441044

1045-
let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string();
1046-
let mut image_caption = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string();
1045+
let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str());
1046+
let mut image_caption = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str());
10471047

10481048
/* As long as image_caption isn't empty remove first and last four characters of image_text to leave us with just the text in the caption without any HTML.
10491049
This makes it possible to enclose it in a <figcaption> later on without having stray HTML breaking it */
10501050
if !image_caption.is_empty() {
1051-
image_caption = image_caption[1..image_caption.len() - 4].to_string();
1051+
image_caption = &image_caption[1..image_caption.len() - 4];
10521052
}
10531053

10541054
// image_url contains > at the end of it, and right above this we remove image_text's front >, leaving us with just a single > between them
10551055
let image_to_replace = format!("<p><a href=\"{image_url}{image_caption}</a></p>");
10561056

1057-
// _image_replacement needs to be in scope for the replacement at the bottom of the loop
1058-
let mut _image_replacement = String::new();
1059-
10601057
/* We don't want to show a caption that's just the image's link, so we check if we find a Reddit preview link within the image's caption.
10611058
If we don't find one we must have actual text, so we include a <figcaption> block that contains it.
10621059
Otherwise we don't include the <figcaption> block as we don't need it. */
1063-
if REDDIT_PREVIEW_REGEX.find(&image_caption).is_none() {
1060+
let _image_replacement = if REDDIT_PREVIEW_REGEX.find(image_caption).is_none() {
10641061
// Without this " would show as \" instead. "\&quot;" is how the quotes are formatted within image_text beforehand
1065-
image_caption = image_caption.replace("\\&quot;", "\"");
1066-
1067-
_image_replacement = format!("<figure><a href=\"{image_url}<img loading=\"lazy\" src=\"{image_url}</a><figcaption>{image_caption}</figcaption></figure>");
1062+
format!(
1063+
"<figure><a href=\"{image_url}<img loading=\"lazy\" src=\"{image_url}</a><figcaption>{}</figcaption></figure>",
1064+
image_caption.replace("\\&quot;", "\"")
1065+
)
10681066
} else {
1069-
_image_replacement = format!("<figure><a href=\"{image_url}<img loading=\"lazy\" src=\"{image_url}</a></figure>");
1070-
}
1067+
format!("<figure><a href=\"{image_url}<img loading=\"lazy\" src=\"{image_url}</a></figure>")
1068+
};
10711069

10721070
/* In order to know if we're dealing with a normal or external preview we need to take a look at the first capture group of REDDIT_PREVIEW_REGEX
10731071
if it's preview we're dealing with something that needs /preview/pre, external-preview is /preview/external-pre, and i is /img */
1074-
let reddit_preview_regex_capture = REDDIT_PREVIEW_REGEX.captures(&text1).unwrap().get(1).map_or("", |m| m.as_str()).to_string();
1075-
let mut _preview_type = String::new();
1076-
if reddit_preview_regex_capture == "preview" {
1077-
_preview_type = "/preview/pre".to_string();
1078-
} else if reddit_preview_regex_capture == "external-preview" {
1079-
_preview_type = "/preview/external-pre".to_string();
1080-
} else {
1081-
_preview_type = "/img".to_string();
1082-
}
1072+
let reddit_preview_regex_capture = REDDIT_PREVIEW_REGEX.captures(&text1).unwrap().get(1).map_or("", |m| m.as_str());
1073+
1074+
let _preview_type = match reddit_preview_regex_capture {
1075+
"preview" => "/preview/pre",
1076+
"external-preview" => "/preview/external-pre",
1077+
_ => "/img",
1078+
};
10831079

10841080
text1 = REDDIT_PREVIEW_REGEX
10851081
.replace(&text1, format!("{_preview_type}$2"))
10861082
.replace(&image_to_replace, &_image_replacement)
1087-
.to_string()
10881083
}
10891084
}
10901085
}
@@ -1158,7 +1153,7 @@ pub fn rewrite_emotes(media_metadata: &Value, comment: String) -> String {
11581153
);
11591154

11601155
// Inside the comment replace the ID we found with the string that will embed the image
1161-
comment = comment.replace(&id, &to_replace_with).to_string();
1156+
comment = comment.replace(&id, &to_replace_with);
11621157
}
11631158
}
11641159
}

0 commit comments

Comments
 (0)