@@ -14,10 +14,9 @@ use rust_embed::RustEmbed;
1414use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
1515use serde_json:: Value ;
1616use serde_json_path:: { JsonPath , JsonPathExt } ;
17- use std:: collections:: { HashMap , HashSet } ;
17+ use std:: collections:: HashSet ;
1818use std:: env;
1919use std:: io:: { Read , Write } ;
20- use std:: str:: FromStr ;
2120use std:: string:: ToString ;
2221use std:: sync:: LazyLock ;
2322use time:: { macros:: format_description, Duration , OffsetDateTime } ;
@@ -522,22 +521,16 @@ impl std::fmt::Display for Awards {
522521impl Awards {
523522 /// Convert Reddit awards JSON to Awards struct
524523 pub fn parse ( items : & Value ) -> Self {
525- let parsed = items. as_array ( ) . unwrap_or ( & Vec :: new ( ) ) . iter ( ) . fold ( Vec :: new ( ) , |mut awards, item| {
526- let name = item[ "name" ] . as_str ( ) . unwrap_or_default ( ) . to_string ( ) ;
527- let icon_url = format_url ( item[ "resized_icons" ] [ 0 ] [ "url" ] . as_str ( ) . unwrap_or_default ( ) ) ;
528- let description = item[ "description" ] . as_str ( ) . unwrap_or_default ( ) . to_string ( ) ;
529- let count: i64 = i64:: from_str ( & item[ "count" ] . to_string ( ) ) . unwrap_or ( 1 ) ;
530-
531- awards. push ( Award {
532- name,
533- icon_url,
534- description,
535- count,
524+ let arr = items. as_array ( ) . map ( |v| v. as_slice ( ) ) . unwrap_or_default ( ) ;
525+ let mut parsed = Vec :: with_capacity ( arr. len ( ) ) ;
526+ for item in arr {
527+ parsed. push ( Award {
528+ name : item[ "name" ] . as_str ( ) . unwrap_or_default ( ) . to_string ( ) ,
529+ icon_url : format_url ( item[ "resized_icons" ] [ 0 ] [ "url" ] . as_str ( ) . unwrap_or_default ( ) ) ,
530+ description : item[ "description" ] . as_str ( ) . unwrap_or_default ( ) . to_string ( ) ,
531+ count : item[ "count" ] . as_i64 ( ) . unwrap_or ( 1 ) ,
536532 } ) ;
537-
538- awards
539- } ) ;
540-
533+ }
541534 Self ( parsed)
542535 }
543536}
@@ -896,15 +889,10 @@ pub async fn parse_post(post: &Value) -> Post {
896889
897890/// Grab a query parameter from a url
898891pub fn param ( path : & str , value : & str ) -> Option < String > {
899- Some (
900- Url :: parse ( format ! ( "https://libredd.it/{path}" ) . as_str ( ) )
901- . ok ( ) ?
902- . query_pairs ( )
903- . into_owned ( )
904- . collect :: < HashMap < _ , _ > > ( )
905- . get ( value) ?
906- . clone ( ) ,
907- )
892+ let query = path. splitn ( 2 , '?' ) . nth ( 1 ) . unwrap_or_default ( ) ;
893+ url:: form_urlencoded:: parse ( query. as_bytes ( ) )
894+ . find ( |( k, _) | k == value)
895+ . map ( |( _, v) | v. into_owned ( ) )
908896}
909897
910898/// Retrieve the value of a setting by name
@@ -1092,25 +1080,18 @@ pub fn rewrite_urls(input_text: &str) -> String {
10921080 // Rewrite Reddit links to Redlib
10931081 REDDIT_REGEX . replace_all ( input_text, r#"href="/"# ) . to_string ( ) ;
10941082
1095- loop {
1096- if REDDIT_EMOJI_REGEX . find ( & text1) . is_none ( ) {
1097- break ;
1098- } else {
1099- text1 = REDDIT_EMOJI_REGEX
1100- . replace_all ( & text1, format_url ( REDDIT_EMOJI_REGEX . find ( & text1) . map ( |x| x. as_str ( ) ) . unwrap_or_default ( ) ) )
1101- . to_string ( )
1102- }
1103- }
1083+ text1 = REDDIT_EMOJI_REGEX . replace_all ( & text1, |caps : & regex:: Captures | format_url ( & caps[ 0 ] ) ) . to_string ( ) ;
11041084
11051085 // Remove (html-encoded) "\" from URLs.
11061086 text1 = text1. replace ( "%5C" , "" ) . replace ( "\\ _" , "_" ) ;
11071087
11081088 // Rewrite external media previews to Redlib
11091089 loop {
1110- if REDDIT_PREVIEW_REGEX . find ( & text1) . is_none ( ) {
1090+ let Some ( caps ) = REDDIT_PREVIEW_REGEX . captures ( & text1) else {
11111091 return text1;
1112- } else {
1113- let formatted_url = format_url ( REDDIT_PREVIEW_REGEX . find ( & text1) . map ( |x| x. as_str ( ) ) . unwrap_or_default ( ) ) ;
1092+ } ;
1093+ {
1094+ let formatted_url = format_url ( caps. get ( 0 ) . map_or ( "" , |m| m. as_str ( ) ) ) ;
11141095
11151096 let image_url = REDLIB_PREVIEW_LINK_REGEX . find ( & formatted_url) . map_or ( "" , |m| m. as_str ( ) ) ;
11161097 let mut image_caption = REDLIB_PREVIEW_TEXT_REGEX . find ( & formatted_url) . map_or ( "" , |m| m. as_str ( ) ) ;
@@ -1139,7 +1120,7 @@ pub fn rewrite_urls(input_text: &str) -> String {
11391120
11401121 /* 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
11411122 if it's preview we're dealing with something that needs /preview/pre, external-preview is /preview/external-pre, and i is /img */
1142- let reddit_preview_regex_capture = REDDIT_PREVIEW_REGEX . captures ( & text1 ) . unwrap ( ) . get ( 1 ) . map_or ( "" , |m| m. as_str ( ) ) ;
1123+ let reddit_preview_regex_capture = caps . get ( 1 ) . map_or ( "" , |m| m. as_str ( ) ) ;
11431124
11441125 let _preview_type = match reddit_preview_regex_capture {
11451126 "preview" => "/preview/pre" ,
@@ -1389,12 +1370,14 @@ pub async fn nsfw_landing(req: Request<Body>, req_url: String) -> Result<Respons
13891370
13901371 // Determine from the request URL if the resource is a subreddit, a user
13911372 // page, or a post.
1392- let resource: String = if !req. param ( "name" ) . unwrap_or_default ( ) . is_empty ( ) {
1373+ let name = req. param ( "name" ) . unwrap_or_default ( ) ;
1374+ let id = req. param ( "id" ) . unwrap_or_default ( ) ;
1375+ let resource: String = if !name. is_empty ( ) {
13931376 res_type = ResourceType :: User ;
1394- req . param ( " name" ) . unwrap_or_default ( )
1395- } else if !req . param ( "id" ) . unwrap_or_default ( ) . is_empty ( ) {
1377+ name
1378+ } else if !id . is_empty ( ) {
13961379 res_type = ResourceType :: Post ;
1397- req . param ( "id" ) . unwrap_or_default ( )
1380+ id
13981381 } else {
13991382 res_type = ResourceType :: Subreddit ;
14001383 req. param ( "sub" ) . unwrap_or_default ( )
0 commit comments