Optimize type casting

This commit is contained in:
spikecodes 2021-03-08 18:49:06 -08:00
parent 213babb057
commit bf783c2f3a
No known key found for this signature in database
GPG key ID: 004CECFF9B463BCB
8 changed files with 32 additions and 28 deletions

View file

@ -1,5 +1,5 @@
// CRATES
use crate::utils::*;
use crate::utils::{Author, Comment, Flags, Flair, FlairPart, Media, Post, Preferences, cookie, error, format_num, format_url, param, request, rewrite_urls, template, time, val};
use tide::Request;
use async_recursion::async_recursion;
@ -68,7 +68,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
let post: &serde_json::Value = &json["data"]["children"][0];
// Grab UTC time as unix timestamp
let (rel_time, created) = time(post["data"]["created_utc"].as_f64().unwrap_or_default());
let (rel_time, created) = time(post["data"]["created_utc"].as_i64().unwrap_or_default());
// Parse post score and upvote ratio
let score = post["data"]["score"].as_i64().unwrap_or_default();
let ratio: f64 = post["data"]["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0;
@ -149,10 +149,10 @@ async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author:
let kind = comment["kind"].as_str().unwrap_or_default().to_string();
let data = &comment["data"];
let unix_time = data["created_utc"].as_f64().unwrap_or_default();
let unix_time = data["created_utc"].as_i64().unwrap_or_default();
let (rel_time, created) = time(unix_time);
let edited = match data["edited"].as_f64() {
let edited = match data["edited"].as_i64() {
Some(stamp) => time(stamp),
None => (String::new(), String::new()),
};
@ -196,7 +196,7 @@ async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author:
distinguished: val(&comment, "distinguished"),
},
score: if data["score_hidden"].as_bool().unwrap_or_default() {
"".to_string()
"\u{2022}".to_string()
} else {
format_num(score)
},