From 740641cb4e5c31fcc742cd2ef43e0c36530f0de6 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Wed, 13 Jan 2021 15:55:10 -0800 Subject: [PATCH] Move nested_val() to user.rs --- src/user.rs | 13 ++++++++----- src/utils.rs | 5 ----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/user.rs b/src/user.rs index ea54f55..4327e2d 100644 --- a/src/user.rs +++ b/src/user.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{error, fetch_posts, format_url, nested_val, param, prefs, request, Post, Preferences, User}; +use crate::utils::{error, fetch_posts, format_url, param, prefs, request, Post, Preferences, User}; use actix_web::{HttpRequest, HttpResponse, Result}; use askama::Template; use time::OffsetDateTime; @@ -57,16 +57,19 @@ async fn user(name: &str) -> Result { Ok(res) => { // Grab creation date as unix timestamp let created: i64 = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64; + + // nested_val function used to parse JSON from Reddit APIs + let about = |item| res["data"]["subreddit"][item].as_str().unwrap_or_default().to_string(); // Parse the JSON output into a User struct Ok(User { name: name.to_string(), - title: nested_val(&res, "subreddit", "title"), - icon: format_url(nested_val(&res, "subreddit", "icon_img").as_str()), + title: about("title"), + icon: format_url(about("icon_img").as_str()), karma: res["data"]["total_karma"].as_i64().unwrap_or(0), created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"), - banner: nested_val(&res, "subreddit", "banner_img"), - description: nested_val(&res, "subreddit", "public_description"), + banner: about("banner_img"), + description: about("public_description"), }) } // If the Reddit API returns an error, exit this function diff --git a/src/utils.rs b/src/utils.rs index ba036a1..c6120ac 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -244,11 +244,6 @@ pub fn val(j: &serde_json::Value, k: &str) -> String { String::from(j["data"][k].as_str().unwrap_or_default()) } -// nested_val() function used to parse JSON from Reddit APIs -pub fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String { - String::from(j["data"][n][k].as_str().unwrap_or_default()) -} - // Fetch posts of a user or subreddit and return a vector of posts and the "after" value pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec, String), &'static str> { let res;