From 9f19d729d13303252a1870327c4bf7d200620944 Mon Sep 17 00:00:00 2001 From: Scoder12 <34356756+Scoder12@users.noreply.github.com> Date: Mon, 7 Dec 2020 10:32:46 -0800 Subject: [PATCH 1/2] Add number format utility --- src/post.rs | 6 +++--- src/subreddit.rs | 4 ++-- src/utils.rs | 8 +++++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/post.rs b/src/post.rs index 879a400..7613f88 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{format_url, request, val, Comment, ErrorTemplate, Flair, Params, Post}; +use crate::utils::{format_url, request, val, Comment, ErrorTemplate, Flair, Params, Post, format_num}; use actix_web::{get, http::StatusCode, web, HttpResponse, Result}; use askama::Template; use chrono::{TimeZone, Utc}; @@ -115,7 +115,7 @@ async fn parse_post(json: serde_json::Value) -> Result { body: markdown_to_html(post_data["data"]["selftext"].as_str().unwrap()).await, author: val(post_data, "author").await, url: val(post_data, "permalink").await, - score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() }, + score: format_num(score), post_type: media.0, media: media.1, time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(), @@ -151,7 +151,7 @@ async fn parse_comments(json: serde_json::Value) -> Result, &'stati comments.push(Comment { body: body, author: val(comment, "author").await, - score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() }, + score: format_num(score), time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(), }); } diff --git a/src/subreddit.rs b/src/subreddit.rs index 10de48c..e430eba 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -89,8 +89,8 @@ async fn subreddit(sub: &String) -> Result { title: val(&res, "title").await, description: val(&res, "public_description").await, icon: format_url(val(&res, "icon_img").await.as_str()).await, - members: if members > 1000 { format!("{}k", members / 1000) } else { members.to_string() }, - active: if active > 1000 { format!("{}k", active / 1000) } else { active.to_string() }, + members: format_num(members), + active: format_num(active), }; Ok(sub) diff --git a/src/utils.rs b/src/utils.rs index eafae7f..063e8a0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -136,7 +136,7 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec 1000 { format!("{}k", score / 1000) } else { score.to_string() }, + score: format_score(score), post_type: "link".to_string(), media: img, url: val(post, "permalink").await, @@ -202,3 +202,9 @@ pub async fn request(url: String) -> Result { Ok(json) } } + + +// FORMATTING +pub fn format_num(num: i64) -> String { + return if num > 1000 { format!("{}k", num / 1000) } else { num.to_string() }; +} From 312d162c09f071ff603cc77880b8d73e56156870 Mon Sep 17 00:00:00 2001 From: Scoder12 <34356756+Scoder12@users.noreply.github.com> Date: Mon, 7 Dec 2020 10:53:22 -0800 Subject: [PATCH 2/2] Fix mistakes --- src/subreddit.rs | 7 ++++--- src/utils.rs | 14 ++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/subreddit.rs b/src/subreddit.rs index e430eba..859e642 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -1,7 +1,8 @@ // CRATES -use crate::utils::{fetch_posts, format_url, request, val, ErrorTemplate, Params, Post, Subreddit}; +use crate::utils::{fetch_posts, format_url, request, val, ErrorTemplate, Params, Post, Subreddit, format_num}; use actix_web::{get, http::StatusCode, web, HttpResponse, Result}; use askama::Template; +use std::convert::TryInto; // STRUCTS #[derive(Template)] @@ -89,8 +90,8 @@ async fn subreddit(sub: &String) -> Result { title: val(&res, "title").await, description: val(&res, "public_description").await, icon: format_url(val(&res, "icon_img").await.as_str()).await, - members: format_num(members), - active: format_num(active), + members: format_num(members.try_into().unwrap()), + active: format_num(active.try_into().unwrap()), }; Ok(sub) diff --git a/src/utils.rs b/src/utils.rs index 063e8a0..1657878 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -76,7 +76,7 @@ pub struct ErrorTemplate { } // -// URL HANDLING +// FORMATTING // pub async fn format_url(url: &str) -> String { @@ -87,6 +87,10 @@ pub async fn format_url(url: &str) -> String { return url.to_string(); } +pub fn format_num(num: i64) -> String { + return if num > 1000 { format!("{}k", num / 1000) } else { num.to_string() }; +} + // // JSON PARSING // @@ -136,7 +140,7 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec Result { Ok(json) } } - - -// FORMATTING -pub fn format_num(num: i64) -> String { - return if num > 1000 { format!("{}k", num / 1000) } else { num.to_string() }; -}