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

@ -1 +1,4 @@
tab_spaces = 2
edition = "2018"
tab_spaces = 2
hard_tabs = true
max_width = 200

View File

@ -1,12 +1,12 @@
// Import Crates
use actix_files::NamedFile;
use actix_web::{get, App, HttpServer, HttpResponse, Result};
use actix_web::{get, App, HttpResponse, HttpServer, Result};
// Reference local files
mod user;
mod popular;
mod post;
mod subreddit;
mod user;
// Create Services
#[get("/style.css")]
@ -20,29 +20,28 @@ async fn favicon() -> HttpResponse {
HttpResponse::Ok().body("")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// start http server
// start http server
HttpServer::new(|| {
App::new()
// GENERAL SERVICES
.service(style)
.service(favicon)
// POST SERVICES
.service(post::short)
.service(post::page)
.service(post::sorted)
// SUBREDDIT SERVICES
.service(subreddit::page)
.service(subreddit::sorted)
// POPULAR SERVICES
.service(popular::page)
// .service(popular::sorted)
// USER SERVICES
.service(user::page)
})
.bind("127.0.0.1:8080")?
.run()
.await
}
App::new()
// GENERAL SERVICES
.service(style)
.service(favicon)
// POST SERVICES
.service(post::short)
.service(post::page)
.service(post::sorted)
// SUBREDDIT SERVICES
.service(subreddit::page)
.service(subreddit::sorted)
// POPULAR SERVICES
.service(popular::page)
// .service(popular::sorted)
// USER SERVICES
.service(user::page)
})
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,52 +1,37 @@
// CRATES
use actix_web::{get, web, HttpResponse, Result};
use serde::Deserialize;
use askama::Template;
use serde::Deserialize;
#[path = "subreddit.rs"] mod subreddit;
#[path = "subreddit.rs"]
mod subreddit;
// STRUCTS
#[derive(Template)]
#[template(path = "popular.html", escape = "none")]
struct PopularTemplate {
posts: Vec<subreddit::Post>,
sort: String
sort: String,
}
#[derive(Deserialize)]
pub struct Params {
sort: Option<String>
}
#[get("/")]
pub async fn page(params: web::Query<Params>) -> Result<HttpResponse> {
match &params.sort {
Some(sort) => render("popular".to_string(), sort.to_string()).await,
None => render("popular".to_string(), "hot".to_string()).await,
}
sort: Option<String>,
}
// RENDER
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {
let posts: Vec<subreddit::Post> = subreddit::posts(sub_name, &sort).await;
let s = PopularTemplate {
posts: posts,
sort: sort
}
.render()
.unwrap();
let s = PopularTemplate { posts: posts, sort: sort }.render().unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
// #[get("/?<sort>")]
// pub fn sorted(sort: String) -> Template {
// println!("{}", sort);
// let posts: Vec<subreddit::Post> = subreddit::posts(&"popular".to_string(), &sort).unwrap();
// let mut context = std::collections::HashMap::new();
// context.insert("about", String::new());
// context.insert("sort", sort);
// context.insert("posts", subreddit::posts_html(posts));
// Template::render("popular", context)
// }
// SERVICES
#[get("/")]
pub async fn page(params: web::Query<Params>) -> Result<HttpResponse> {
match &params.sort {
Some(sort) => render("popular".to_string(), sort.to_string()).await,
None => render("popular".to_string(), "hot".to_string()).await,
}
}

View File

@ -1,8 +1,8 @@
// CRATES
use actix_web::{get, web, HttpResponse, Result};
use askama::Template;
use pulldown_cmark::{Parser, Options, html};
use chrono::{TimeZone, Utc};
use pulldown_cmark::{html, Options, Parser};
// STRUCTS
#[derive(Template)]
@ -10,7 +10,7 @@ use chrono::{TimeZone, Utc};
struct PostTemplate {
comments: Vec<Comment>,
post: Post,
sort: String
sort: String,
}
pub struct Post {
@ -21,31 +21,31 @@ pub struct Post {
pub url: String,
pub score: String,
pub media: String,
pub time: String
pub time: String,
}
pub struct Comment {
pub body: String,
pub author: String,
pub score: String,
pub time: String
pub time: String,
}
async fn render(id: String, sort: String) -> Result<HttpResponse> {
println!("id: {}", id);
let post: Post = fetch_post(&id).await;
let comments: Vec<Comment> = fetch_comments(id, &sort).await.unwrap();
let s = PostTemplate {
comments: comments,
post: post,
sort: sort
sort: sort,
}
.render()
.unwrap();
// println!("{}", s);
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
@ -66,20 +66,28 @@ async fn sorted(web::Path((_sub, id, _title, sort)): web::Path<(String, String,
}
// 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(""))
}
async fn media(data: &serde_json::Value) -> String {
let post_hint: &str = data["data"]["post_hint"].as_str().unwrap_or("");
let has_media: bool = data["data"]["media"].is_object();
let media: String = if !has_media { format!(r#"<h4 class="post_body"><a href="{u}">{u}</a></h4>"#, u=data["data"]["url"].as_str().unwrap()) }
else { format!(r#"<img class="post_image" src="{}.png"/>"#, data["data"]["url"].as_str().unwrap()) };
let media: String = if !has_media {
format!(r#"<h4 class="post_body"><a href="{u}">{u}</a></h4>"#, u = data["data"]["url"].as_str().unwrap())
} else {
format!(r#"<img class="post_image" src="{}.png"/>"#, data["data"]["url"].as_str().unwrap())
};
match post_hint {
"hosted:video" => format!(r#"<video class="post_image" src="{}" controls/>"#, data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()),
"hosted:video" => format!(
r#"<video class="post_image" src="{}" controls/>"#,
data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()
),
"image" => format!(r#"<img class="post_image" src="{}"/>"#, data["data"]["url"].as_str().unwrap()),
"self" => String::from(""),
_ => media
_ => media,
}
}
@ -98,12 +106,12 @@ async fn markdown_to_html(md: &str) -> String {
}
// POSTS
async fn fetch_post (id: &String) -> Post {
async fn fetch_post(id: &String) -> Post {
let url: String = format!("https://reddit.com/{}.json", id);
let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap();
let data: serde_json::Value = serde_json::from_str(resp.as_str()).expect("Failed to parse JSON");
let post_data: &serde_json::Value = &data[0]["data"]["children"][0];
let unix_time: i64 = post_data["data"]["created_utc"].as_f64().unwrap().round() as i64;
@ -112,40 +120,40 @@ async fn fetch_post (id: &String) -> Post {
Post {
title: val(post_data, "title").await,
community: val(post_data, "subreddit").await,
body: markdown_to_html(post_data["data"]["selftext"].as_str().unwrap()).await, //markdown_to_html(post_data["data"]["selftext"].as_str().unwrap(), &ComrakOptions::default()),
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: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
media: media(post_data).await,
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string()
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
}
}
// COMMENTS
async fn fetch_comments (id: String, sort: &String) -> Result<Vec<Comment>, Box<dyn std::error::Error>> {
async fn fetch_comments(id: String, sort: &String) -> Result<Vec<Comment>, Box<dyn std::error::Error>> {
let url: String = format!("https://reddit.com/{}.json?sort={}", id, sort);
let resp: String = reqwest::get(&url).await?.text().await?;
let data: serde_json::Value = serde_json::from_str(resp.as_str())?;
let comment_data = data[1]["data"]["children"].as_array().unwrap();
let mut comments: Vec<Comment> = Vec::new();
for comment in comment_data.iter() {
let unix_time: i64 = comment["data"]["created_utc"].as_f64().unwrap_or(0.0).round() as i64;
let score = comment["data"]["score"].as_i64().unwrap_or(0);
let body = markdown_to_html(comment["data"]["body"].as_str().unwrap_or("")).await;// markdown_to_html(comment["data"]["body"].as_str().unwrap_or(""), &ComrakOptions::default());
let body = markdown_to_html(comment["data"]["body"].as_str().unwrap_or("")).await;
// println!("{}", body);
comments.push(Comment {
body: body,
author: val(comment, "author").await,
score: if score>1000 {format!("{}k",score/1000)} else {score.to_string()},
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string()
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
});
}
Ok(comments)
}
}

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
}

View File

@ -9,7 +9,7 @@ use chrono::{TimeZone, Utc};
struct UserTemplate {
user: User,
posts: Vec<Post>,
sort: String
sort: String,
}
pub struct Post {
@ -19,7 +19,7 @@ pub struct Post {
pub score: String,
pub image: String,
pub url: String,
pub time: String
pub time: String,
}
pub struct User {
@ -27,21 +27,14 @@ pub struct User {
pub icon: String,
pub karma: i64,
pub banner: String,
pub description: String
pub description: String,
}
async fn render(username: String, sort: String) -> Result<HttpResponse> {
let user: User = user(&username).await;
let posts: Vec<Post> = posts(username, &sort).await;
let s = UserTemplate {
user: user,
posts: posts,
sort: sort
}
.render()
.unwrap();
let s = UserTemplate { user: user, posts: posts, sort: sort }.render().unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
@ -57,8 +50,12 @@ async fn sorted(web::Path((username, sort)): web::Path<(String, String)>) -> Res
}
// UTILITIES
async fn user_val (j: &serde_json::Value, k: &str) -> String { String::from(j["data"]["subreddit"][k].as_str().unwrap()) }
async fn post_val (j: &serde_json::Value, k: &str) -> String { String::from(j["data"][k].as_str().unwrap_or("Comment")) }
async fn user_val(j: &serde_json::Value, k: &str) -> String {
String::from(j["data"]["subreddit"][k].as_str().unwrap())
}
async fn post_val(j: &serde_json::Value, k: &str) -> String {
String::from(j["data"][k].as_str().unwrap_or("Comment"))
}
// USER
async fn user(name: &String) -> User {
@ -66,13 +63,13 @@ async fn user(name: &String) -> User {
let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap();
let data: serde_json::Value = serde_json::from_str(resp.as_str()).expect("Failed to parse JSON");
User {
name: name.to_string(),
icon: user_val(&data, "icon_img").await,
karma: data["data"]["total_karma"].as_i64().unwrap(),
banner: user_val(&data, "banner_img").await,
description: user_val(&data, "public_description").await
description: user_val(&data, "public_description").await,
}
}
@ -80,26 +77,30 @@ async fn user(name: &String) -> User {
async fn posts(sub: String, sort: &String) -> Vec<Post> {
let url: String = format!("https://www.reddit.com/u/{}/.json?sort={}", 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 post_val(post, "thumbnail").await.starts_with("https:/") { post_val(post, "thumbnail").await } else { String::new() };
let img = if post_val(post, "thumbnail").await.starts_with("https:/") {
post_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: post_val(post, "title").await,
community: post_val(post, "subreddit").await,
author: post_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: post_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
}
}