@@ -13,7 +13,7 @@ use once_cell::sync::Lazy;
1313use regex:: Regex ;
1414use rinja:: Template ;
1515use rust_embed:: RustEmbed ;
16- use serde:: Serialize ;
16+ use serde:: { Serialize , Serializer } ;
1717use serde_json:: Value ;
1818use serde_json_path:: { JsonPath , JsonPathExt } ;
1919use std:: collections:: { HashMap , HashSet } ;
@@ -601,8 +601,9 @@ pub struct Params {
601601 pub before : Option < String > ,
602602}
603603
604- #[ derive( Default ) ]
604+ #[ derive( Default , Serialize ) ]
605605pub struct Preferences {
606+ #[ serde( skip) ]
606607 pub available_themes : Vec < String > ,
607608 pub theme : String ,
608609 pub front_page : String ,
@@ -620,12 +621,21 @@ pub struct Preferences {
620621 pub disable_visit_reddit_confirmation : String ,
621622 pub comment_sort : String ,
622623 pub post_sort : String ,
624+ #[ serde( serialize_with = "serialize_vec_with_plus" ) ]
623625 pub subscriptions : Vec < String > ,
626+ #[ serde( serialize_with = "serialize_vec_with_plus" ) ]
624627 pub filters : Vec < String > ,
625628 pub hide_awards : String ,
626629 pub hide_score : String ,
627630}
628631
632+ fn serialize_vec_with_plus < S > ( vec : & Vec < String > , serializer : S ) -> Result < S :: Ok , S :: Error >
633+ where
634+ S : Serializer ,
635+ {
636+ serializer. serialize_str ( & vec. join ( "+" ) )
637+ }
638+
629639#[ derive( RustEmbed ) ]
630640#[ folder = "static/themes/" ]
631641#[ include = "*.css" ]
@@ -665,6 +675,10 @@ impl Preferences {
665675 hide_score : setting ( req, "hide_score" ) ,
666676 }
667677 }
678+
679+ pub fn to_urlencoded ( & self ) -> Result < String , String > {
680+ serde_urlencoded:: to_string ( self ) . map_err ( |e| e. to_string ( ) )
681+ }
668682}
669683
670684/// Gets a `HashSet` of filters from the cookie in the given `Request`.
@@ -1277,7 +1291,7 @@ pub fn get_post_url(post: &Post) -> String {
12771291
12781292#[ cfg( test) ]
12791293mod tests {
1280- use super :: { format_num, format_url, rewrite_urls} ;
1294+ use super :: { format_num, format_url, rewrite_urls, Preferences } ;
12811295
12821296 #[ test]
12831297 fn format_num_works ( ) {
@@ -1344,6 +1358,35 @@ mod tests {
13441358 assert_eq ! ( format_url( "nsfw" ) , "" ) ;
13451359 assert_eq ! ( format_url( "spoiler" ) , "" ) ;
13461360 }
1361+ #[ test]
1362+ fn serialize_prefs ( ) {
1363+ let prefs = Preferences {
1364+ available_themes : vec ! [ ] ,
1365+ theme : "laserwave" . to_owned ( ) ,
1366+ front_page : "default" . to_owned ( ) ,
1367+ layout : "compact" . to_owned ( ) ,
1368+ wide : "on" . to_owned ( ) ,
1369+ blur_spoiler : "on" . to_owned ( ) ,
1370+ show_nsfw : "off" . to_owned ( ) ,
1371+ blur_nsfw : "on" . to_owned ( ) ,
1372+ hide_hls_notification : "off" . to_owned ( ) ,
1373+ video_quality : "best" . to_owned ( ) ,
1374+ hide_sidebar_and_summary : "off" . to_owned ( ) ,
1375+ use_hls : "on" . to_owned ( ) ,
1376+ autoplay_videos : "on" . to_owned ( ) ,
1377+ fixed_navbar : "on" . to_owned ( ) ,
1378+ disable_visit_reddit_confirmation : "on" . to_owned ( ) ,
1379+ comment_sort : "confidence" . to_owned ( ) ,
1380+ post_sort : "top" . to_owned ( ) ,
1381+ subscriptions : vec ! [ "memes" . to_owned( ) , "mildlyinteresting" . to_owned( ) ] ,
1382+ filters : vec ! [ ] ,
1383+ hide_awards : "off" . to_owned ( ) ,
1384+ hide_score : "off" . to_owned ( ) ,
1385+ } ;
1386+ let urlencoded = serde_urlencoded:: to_string ( prefs) . expect ( "Failed to serialize Prefs" ) ;
1387+
1388+ assert_eq ! ( urlencoded, "theme=laserwave&front_page=default&layout=compact&wide=on&blur_spoiler=on&show_nsfw=off&blur_nsfw=on&hide_hls_notification=off&video_quality=best&hide_sidebar_and_summary=off&use_hls=on&autoplay_videos=on&fixed_navbar=on&disable_visit_reddit_confirmation=on&comment_sort=confidence&post_sort=top&subscriptions=memes%2Bmildlyinteresting&filters=&hide_awards=off&hide_score=off" )
1389+ }
13471390}
13481391
13491392#[ test]
0 commit comments