From b22c9ff84285b3b82a482af820ebc62df055675b Mon Sep 17 00:00:00 2001 From: Tokarak <63452145+Tokarak@users.noreply.github.com> Date: Sat, 3 Jun 2023 02:00:18 +0100 Subject: [PATCH] Refactor Config.pushshift to be Option-al Hopefully I caught all the unwraps that were here before --- src/config.rs | 13 +++++++------ src/instance_info.rs | 2 +- src/post.rs | 8 ++------ src/utils.rs | 6 +----- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index e72c780..a02ea88 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,10 @@ use std::{env::var, fs::read_to_string}; // first request) and contains the instance settings. pub(crate) static CONFIG: Lazy = Lazy::new(Config::load); +// This serves as the frontend for the Pushshift API - on removed comments, this URL will +// be the base of a link, to display removed content (on another site). +pub(crate) static DEFAULT_PUSHSHIFT_FRONTEND: &str = "www.unddit.com"; + /// Stores the configuration parsed from the environment variables and the /// config file. `Config::Default()` contains None for each setting. /// When adding more config settings, add it to `Config::load`, @@ -59,7 +63,7 @@ pub struct Config { pub(crate) banner: Option, #[serde(rename = "LIBREDDIT_PUSHSHIFT_FRONTEND")] - pub(crate) pushshift: String, + pub(crate) pushshift: Option, } impl Config { @@ -75,9 +79,6 @@ impl Config { // both are `None`, return a `None` via the `map_or_else` function let parse = |key: &str| -> Option { var(key).ok().map_or_else(|| get_setting_from_config(key, &config), Some) }; - // This serves as the frontend for the Pushshift API - on removed comments, this URL will - // be the base of a link, to display removed content (on another site). - let default_pushshift_frontend = String::from("www.unddit.com"); Self { sfw_only: parse("LIBREDDIT_SFW_ONLY"), @@ -94,7 +95,7 @@ impl Config { default_hide_awards: parse("LIBREDDIT_DEFAULT_HIDE_AWARDS"), default_subscriptions: parse("LIBREDDIT_DEFAULT_SUBSCRIPTIONS"), banner: parse("LIBREDDIT_BANNER"), - pushshift: parse("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap_or(default_pushshift_frontend), + pushshift: parse("LIBREDDIT_PUSHSHIFT_FRONTEND"), } } } @@ -115,7 +116,7 @@ fn get_setting_from_config(name: &str, config: &Config) -> Option { "LIBREDDIT_DEFAULT_HIDE_AWARDS" => config.default_hide_awards.clone(), "LIBREDDIT_DEFAULT_SUBSCRIPTIONS" => config.default_subscriptions.clone(), "LIBREDDIT_BANNER" => config.banner.clone(), - "LIBREDDIT_PUSHSHIFT_FRONTEND" => Some(config.pushshift.clone()), + "LIBREDDIT_PUSHSHIFT_FRONTEND" => config.pushshift.clone(), _ => None, } } diff --git a/src/instance_info.rs b/src/instance_info.rs index 479fba5..4df314a 100644 --- a/src/instance_info.rs +++ b/src/instance_info.rs @@ -122,7 +122,7 @@ impl InstanceInfo { ["Deploy timestamp", &self.deploy_unix_ts.to_string()], ["Compile mode", &self.compile_mode], ["SFW only", &convert(&self.config.sfw_only)], - ["Pushshift frontend", &convert(&Some(self.config.pushshift.clone()))], + ["Pushshift frontend", &convert(&self.config.pushshift)], ]) .with_header_row(["Settings"]), ); diff --git a/src/post.rs b/src/post.rs index 970dc89..c2e3d4f 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,6 +1,6 @@ // CRATES use crate::client::json; -use crate::config::get_setting; +use crate::config::{get_setting, DEFAULT_PUSHSHIFT_FRONTEND}; use crate::server::RequestExt; use crate::subreddit::{can_access_quarantine, quarantine}; use crate::utils::{ @@ -126,11 +126,7 @@ fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str, let body = if (val(&comment, "author") == "[deleted]" && val(&comment, "body") == "[removed]") || val(&comment, "body") == "[ Removed by Reddit ]" { format!( "

[removed] — view removed comment

", - // Safe to unwrap: The get_setting function only returns an option by design, - // for this specific setting, it is a String, not an Option. See - // get_setting_from_config() in config.rs - when requesting this specific - // setting, it wraps it in a Some, just to match the type signature. - get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap(), + get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap_or(String::from(DEFAULT_PUSHSHIFT_FRONTEND)), post_link, id ) diff --git a/src/utils.rs b/src/utils.rs index e56214b..06d7f92 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -594,11 +594,7 @@ pub async fn parse_post(post: &serde_json::Value) -> Post { let body = if val(post, "removed_by_category") == "moderator" { format!( "

[removed] — view removed post

", - // Safe to unwrap: The get_setting function only returns an option by design, - // for this specific setting, it is a String, not an Option. See - // get_setting_from_config() in config.rs - when requesting this specific - // setting, it wraps it in a Some, just to match the type signature. - get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap(), + get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap_or(String::from(crate::config::DEFAULT_PUSHSHIFT_FRONTEND)), permalink ) } else {