Rustfmt Code Format

This commit is contained in:
spikecodes 2020-10-25 20:57:19 -07:00
parent 1e7bbb385c
commit c5b64e2168
6 changed files with 129 additions and 130 deletions

View file

@ -9,7 +9,7 @@ use chrono::{TimeZone, Utc};
struct SubredditTemplate {
sub: Subreddit,
posts: Vec<Post>,
sort: String
sort: String,
}
pub struct Post {
@ -19,29 +19,27 @@ pub struct Post {
pub score: String,
pub image: String,
pub url: String,
pub time: String
pub time: String,
}
pub struct Subreddit {
pub name: String,
pub title: String,
pub description: String,
pub icon: String
pub icon: String,
}
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {
let mut sub: Subreddit = subreddit(&sub_name).await;
let posts: Vec<Post> = posts(sub_name, &sort).await;
sub.icon = if sub.icon!="" {format!(r#"<img class="subreddit_icon" src="{}">"#, sub.icon)} else {String::new()};
let s = SubredditTemplate {
sub: sub,
posts: posts,
sort: sort
}
.render()
.unwrap();
sub.icon = if sub.icon != "" {
format!(r#"<img class="subreddit_icon" src="{}">"#, sub.icon)
} else {
String::new()
};
let s = SubredditTemplate { sub: sub, posts: posts, sort: sort }.render().unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
@ -59,7 +57,9 @@ async fn sorted(web::Path((sub, sort)): web::Path<(String, String)>) -> Result<H
}
// UTILITIES
async fn val (j: &serde_json::Value, k: &str) -> String { String::from(j["data"][k].as_str().unwrap_or("")) }
async fn val(j: &serde_json::Value, k: &str) -> String {
String::from(j["data"][k].as_str().unwrap_or(""))
}
// SUBREDDIT
async fn subreddit(sub: &String) -> Subreddit {
@ -72,7 +72,7 @@ async fn subreddit(sub: &String) -> Subreddit {
let icon_split: std::str::Split<&str> = icon.split("?");
let icon_parts: Vec<&str> = icon_split.collect();
Subreddit {
Subreddit {
name: val(&data, "display_name").await,
title: val(&data, "title").await,
description: val(&data, "public_description").await,
@ -84,26 +84,29 @@ async fn subreddit(sub: &String) -> Subreddit {
pub async fn posts(sub: String, sort: &String) -> Vec<Post> {
let url: String = format!("https://www.reddit.com/r/{}/{}.json", sub, sort);
let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap();
let popular: serde_json::Value = serde_json::from_str(resp.as_str()).expect("Failed to parse JSON");
let post_list = popular["data"]["children"].as_array().unwrap();
let mut posts: Vec<Post> = Vec::new();
for post in post_list.iter() {
let img = if val(post, "thumbnail").await.starts_with("https:/") { val(post, "thumbnail").await } else { String::new() };
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
let score = post["data"]["score"].as_i64().unwrap();
let img = if val(post, "thumbnail").await.starts_with("https:/") {
val(post, "thumbnail").await
} else {
String::new()
};
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
let score = post["data"]["score"].as_i64().unwrap();
posts.push(Post {
title: val(post, "title").await,
community: val(post, "subreddit").await,
author: val(post, "author").await,
score: if score>1000 {format!("{}k",score/1000)} else {score.to_string()},
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
image: img,
url: val(post, "permalink").await,
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string()
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
});
}
posts
}
}
posts
}