mirror of https://github.com/spikecodes/libreddit
Refactor Config.pushshift to be Option-al
Hopefully I caught all the unwraps that were here before
This commit is contained in:
parent
916a0753ea
commit
b22c9ff842
|
@ -9,6 +9,10 @@ use std::{env::var, fs::read_to_string};
|
||||||
// first request) and contains the instance settings.
|
// first request) and contains the instance settings.
|
||||||
pub(crate) static CONFIG: Lazy<Config> = Lazy::new(Config::load);
|
pub(crate) static CONFIG: Lazy<Config> = 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
|
/// Stores the configuration parsed from the environment variables and the
|
||||||
/// config file. `Config::Default()` contains None for each setting.
|
/// config file. `Config::Default()` contains None for each setting.
|
||||||
/// When adding more config settings, add it to `Config::load`,
|
/// When adding more config settings, add it to `Config::load`,
|
||||||
|
@ -59,7 +63,7 @@ pub struct Config {
|
||||||
pub(crate) banner: Option<String>,
|
pub(crate) banner: Option<String>,
|
||||||
|
|
||||||
#[serde(rename = "LIBREDDIT_PUSHSHIFT_FRONTEND")]
|
#[serde(rename = "LIBREDDIT_PUSHSHIFT_FRONTEND")]
|
||||||
pub(crate) pushshift: String,
|
pub(crate) pushshift: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
@ -75,9 +79,6 @@ impl Config {
|
||||||
// both are `None`, return a `None` via the `map_or_else` function
|
// both are `None`, return a `None` via the `map_or_else` function
|
||||||
let parse = |key: &str| -> Option<String> { var(key).ok().map_or_else(|| get_setting_from_config(key, &config), Some) };
|
let parse = |key: &str| -> Option<String> { 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 {
|
Self {
|
||||||
sfw_only: parse("LIBREDDIT_SFW_ONLY"),
|
sfw_only: parse("LIBREDDIT_SFW_ONLY"),
|
||||||
|
@ -94,7 +95,7 @@ impl Config {
|
||||||
default_hide_awards: parse("LIBREDDIT_DEFAULT_HIDE_AWARDS"),
|
default_hide_awards: parse("LIBREDDIT_DEFAULT_HIDE_AWARDS"),
|
||||||
default_subscriptions: parse("LIBREDDIT_DEFAULT_SUBSCRIPTIONS"),
|
default_subscriptions: parse("LIBREDDIT_DEFAULT_SUBSCRIPTIONS"),
|
||||||
banner: parse("LIBREDDIT_BANNER"),
|
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<String> {
|
||||||
"LIBREDDIT_DEFAULT_HIDE_AWARDS" => config.default_hide_awards.clone(),
|
"LIBREDDIT_DEFAULT_HIDE_AWARDS" => config.default_hide_awards.clone(),
|
||||||
"LIBREDDIT_DEFAULT_SUBSCRIPTIONS" => config.default_subscriptions.clone(),
|
"LIBREDDIT_DEFAULT_SUBSCRIPTIONS" => config.default_subscriptions.clone(),
|
||||||
"LIBREDDIT_BANNER" => config.banner.clone(),
|
"LIBREDDIT_BANNER" => config.banner.clone(),
|
||||||
"LIBREDDIT_PUSHSHIFT_FRONTEND" => Some(config.pushshift.clone()),
|
"LIBREDDIT_PUSHSHIFT_FRONTEND" => config.pushshift.clone(),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,7 +122,7 @@ impl InstanceInfo {
|
||||||
["Deploy timestamp", &self.deploy_unix_ts.to_string()],
|
["Deploy timestamp", &self.deploy_unix_ts.to_string()],
|
||||||
["Compile mode", &self.compile_mode],
|
["Compile mode", &self.compile_mode],
|
||||||
["SFW only", &convert(&self.config.sfw_only)],
|
["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"]),
|
.with_header_row(["Settings"]),
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// CRATES
|
// CRATES
|
||||||
use crate::client::json;
|
use crate::client::json;
|
||||||
use crate::config::get_setting;
|
use crate::config::{get_setting, DEFAULT_PUSHSHIFT_FRONTEND};
|
||||||
use crate::server::RequestExt;
|
use crate::server::RequestExt;
|
||||||
use crate::subreddit::{can_access_quarantine, quarantine};
|
use crate::subreddit::{can_access_quarantine, quarantine};
|
||||||
use crate::utils::{
|
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 ]" {
|
let body = if (val(&comment, "author") == "[deleted]" && val(&comment, "body") == "[removed]") || val(&comment, "body") == "[ Removed by Reddit ]" {
|
||||||
format!(
|
format!(
|
||||||
"<div class=\"md\"><p>[removed] — <a href=\"https://{}{}{}\">view removed comment</a></p></div>",
|
"<div class=\"md\"><p>[removed] — <a href=\"https://{}{}{}\">view removed comment</a></p></div>",
|
||||||
// Safe to unwrap: The get_setting function only returns an option by design,
|
get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap_or(String::from(DEFAULT_PUSHSHIFT_FRONTEND)),
|
||||||
// for this specific setting, it is a String, not an Option<String>. 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(),
|
|
||||||
post_link,
|
post_link,
|
||||||
id
|
id
|
||||||
)
|
)
|
||||||
|
|
|
@ -594,11 +594,7 @@ pub async fn parse_post(post: &serde_json::Value) -> Post {
|
||||||
let body = if val(post, "removed_by_category") == "moderator" {
|
let body = if val(post, "removed_by_category") == "moderator" {
|
||||||
format!(
|
format!(
|
||||||
"<div class=\"md\"><p>[removed] — <a href=\"https://{}{}\">view removed post</a></p></div>",
|
"<div class=\"md\"><p>[removed] — <a href=\"https://{}{}\">view removed post</a></p></div>",
|
||||||
// Safe to unwrap: The get_setting function only returns an option by design,
|
get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap_or(String::from(crate::config::DEFAULT_PUSHSHIFT_FRONTEND)),
|
||||||
// for this specific setting, it is a String, not an Option<String>. 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(),
|
|
||||||
permalink
|
permalink
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue