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 @@
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,7 +20,6 @@ async fn favicon() -> HttpResponse {
HttpResponse::Ok().body("")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// start http server

View file

@ -1,23 +1,33 @@
// 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>
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();
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
// SERVICES
#[get("/")]
pub async fn page(params: web::Query<Params>) -> Result<HttpResponse> {
match &params.sort {
@ -25,28 +35,3 @@ pub async fn page(params: web::Query<Params>) -> Result<HttpResponse> {
None => render("popular".to_string(), "hot".to_string()).await,
}
}
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();
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)
// }

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,14 +21,14 @@ 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> {
@ -39,7 +39,7 @@ async fn render(id: String, sort: String) -> Result<HttpResponse> {
let s = PostTemplate {
comments: comments,
post: post,
sort: sort
sort: sort,
}
.render()
.unwrap();
@ -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,7 +106,7 @@ 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();
@ -112,17 +120,17 @@ 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?;
@ -135,15 +143,15 @@ async fn fetch_comments (id: String, sort: &String) -> Result<Vec<Comment>, Box<
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(),
});
}

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()};
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();
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 {
@ -91,19 +91,22 @@ pub async fn posts(sub: String, sort: &String) -> Vec<Post> {
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 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
}

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 {
@ -72,7 +69,7 @@ async fn user(name: &String) -> User {
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,
}
}
@ -87,17 +84,21 @@ async fn posts(sub: String, sort: &String) -> Vec<Post> {
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(),
});
}