Move nested_val() to user.rs
This commit is contained in:
parent
09c98c8da6
commit
740641cb4e
2 changed files with 8 additions and 10 deletions
13
src/user.rs
13
src/user.rs
|
@ -1,5 +1,5 @@
|
||||||
// CRATES
|
// 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 actix_web::{HttpRequest, HttpResponse, Result};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
|
@ -57,16 +57,19 @@ async fn user(name: &str) -> Result<User, &'static str> {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
// Grab creation date as unix timestamp
|
// Grab creation date as unix timestamp
|
||||||
let created: i64 = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64;
|
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
|
// Parse the JSON output into a User struct
|
||||||
Ok(User {
|
Ok(User {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
title: nested_val(&res, "subreddit", "title"),
|
title: about("title"),
|
||||||
icon: format_url(nested_val(&res, "subreddit", "icon_img").as_str()),
|
icon: format_url(about("icon_img").as_str()),
|
||||||
karma: res["data"]["total_karma"].as_i64().unwrap_or(0),
|
karma: res["data"]["total_karma"].as_i64().unwrap_or(0),
|
||||||
created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"),
|
created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"),
|
||||||
banner: nested_val(&res, "subreddit", "banner_img"),
|
banner: about("banner_img"),
|
||||||
description: nested_val(&res, "subreddit", "public_description"),
|
description: about("public_description"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// If the Reddit API returns an error, exit this function
|
// If the Reddit API returns an error, exit this function
|
||||||
|
|
|
@ -244,11 +244,6 @@ pub fn val(j: &serde_json::Value, k: &str) -> String {
|
||||||
String::from(j["data"][k].as_str().unwrap_or_default())
|
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
|
// 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<Post>, String), &'static str> {
|
pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post>, String), &'static str> {
|
||||||
let res;
|
let res;
|
||||||
|
|
Loading…
Reference in a new issue