mirror of
https://github.com/redlib-org/redlib.git
synced 2025-05-04 20:30:37 +00:00
Optimize type casting
This commit is contained in:
parent
213babb057
commit
bf783c2f3a
8 changed files with 32 additions and 28 deletions
10
src/post.rs
10
src/post.rs
|
@ -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)
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue