diff --git a/rustfmt.toml b/rustfmt.toml index 6f2e075..e94c318 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1,4 @@ -tab_spaces = 2 \ No newline at end of file +edition = "2018" +tab_spaces = 2 +hard_tabs = true +max_width = 200 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 94a3be2..312981c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 -} \ No newline at end of file + 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 +} diff --git a/src/popular.rs b/src/popular.rs index 889d249..a01a88a 100644 --- a/src/popular.rs +++ b/src/popular.rs @@ -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, - sort: String + sort: String, } #[derive(Deserialize)] pub struct Params { - sort: Option -} - -#[get("/")] -pub async fn page(params: web::Query) -> Result { - match ¶ms.sort { - Some(sort) => render("popular".to_string(), sort.to_string()).await, - None => render("popular".to_string(), "hot".to_string()).await, - } + sort: Option, } +// RENDER async fn render(sub_name: String, sort: String) -> Result { let posts: Vec = 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("/?")] -// pub fn sorted(sort: String) -> Template { -// println!("{}", sort); -// let posts: Vec = 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) -> Result { + match ¶ms.sort { + Some(sort) => render("popular".to_string(), sort.to_string()).await, + None => render("popular".to_string(), "hot".to_string()).await, + } +} diff --git a/src/post.rs b/src/post.rs index 165babf..b252612 100644 --- a/src/post.rs +++ b/src/post.rs @@ -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, 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 { println!("id: {}", id); let post: Post = fetch_post(&id).await; let comments: Vec = 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#"

{u}

"#, u=data["data"]["url"].as_str().unwrap()) } - else { format!(r#""#, data["data"]["url"].as_str().unwrap()) }; + let media: String = if !has_media { + format!(r#"

{u}

"#, u = data["data"]["url"].as_str().unwrap()) + } else { + format!(r#""#, data["data"]["url"].as_str().unwrap()) + }; match post_hint { - "hosted:video" => format!(r#"