@@ -638,7 +638,7 @@ pub struct Preferences {
638
638
pub hide_score : String ,
639
639
}
640
640
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 >
642
642
where
643
643
S : Serializer ,
644
644
{
@@ -915,11 +915,12 @@ pub fn setting_or_default(req: &Request<Body>, name: &str, default: String) -> S
915
915
// Detect and redirect in the event of a random subreddit
916
916
pub async fn catch_random ( sub : & str , additional : & str ) -> Result < Response < Body > , String > {
917
917
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
+ ) ) )
923
924
} else {
924
925
Err ( "No redirect needed" . to_string ( ) )
925
926
}
@@ -1019,8 +1020,7 @@ static REDLIB_PREVIEW_TEXT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r">(.*?)
1019
1020
pub fn rewrite_urls ( input_text : & str ) -> String {
1020
1021
let mut text1 =
1021
1022
// 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 ( ) ;
1024
1024
1025
1025
loop {
1026
1026
if REDDIT_EMOJI_REGEX . find ( & text1) . is_none ( ) {
@@ -1042,49 +1042,44 @@ pub fn rewrite_urls(input_text: &str) -> String {
1042
1042
} else {
1043
1043
let formatted_url = format_url ( REDDIT_PREVIEW_REGEX . find ( & text1) . map ( |x| x. as_str ( ) ) . unwrap_or_default ( ) ) ;
1044
1044
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 ( ) ) ;
1047
1047
1048
1048
/* 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.
1049
1049
This makes it possible to enclose it in a <figcaption> later on without having stray HTML breaking it */
1050
1050
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 ] ;
1052
1052
}
1053
1053
1054
1054
// 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
1055
1055
let image_to_replace = format ! ( "<p><a href=\" {image_url}{image_caption}</a></p>" ) ;
1056
1056
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
-
1060
1057
/* 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.
1061
1058
If we don't find one we must have actual text, so we include a <figcaption> block that contains it.
1062
1059
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 ( ) {
1064
1061
// Without this " would show as \" instead. "\"" is how the quotes are formatted within image_text beforehand
1065
- image_caption = image_caption. replace ( "\\ "" , "\" " ) ;
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( "\\ "" , "\" " )
1065
+ )
1068
1066
} 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
+ } ;
1071
1069
1072
1070
/* 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
1073
1071
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
+ } ;
1083
1079
1084
1080
text1 = REDDIT_PREVIEW_REGEX
1085
1081
. replace ( & text1, format ! ( "{_preview_type}$2" ) )
1086
1082
. replace ( & image_to_replace, & _image_replacement)
1087
- . to_string ( )
1088
1083
}
1089
1084
}
1090
1085
}
@@ -1158,7 +1153,7 @@ pub fn rewrite_emotes(media_metadata: &Value, comment: String) -> String {
1158
1153
) ;
1159
1154
1160
1155
// 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) ;
1162
1157
}
1163
1158
}
1164
1159
}
0 commit comments