From 3902a36ea35ce3ae5e093bed1ade039ab98f88df Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Wed, 25 Nov 2020 13:53:30 -0800 Subject: [PATCH] Reorganize Crate References --- src/main.rs | 1 + src/popular.rs | 9 +++------ src/post.rs | 9 +++------ src/subreddit.rs | 9 +++------ src/user.rs | 9 +++------ src/utils.rs | 10 ++++++---- 6 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index 043e758..09f5789 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod post; mod subreddit; mod user; mod proxy; +mod utils; // Create Services #[get("/style.css")] diff --git a/src/popular.rs b/src/popular.rs index 5dd9760..436a3f2 100644 --- a/src/popular.rs +++ b/src/popular.rs @@ -1,10 +1,7 @@ // CRATES -use actix_web::{get, web, HttpResponse, Result}; +use actix_web::{get, web, HttpResponse, Result, http::StatusCode}; use askama::Template; - -#[path = "utils.rs"] -mod utils; -use utils::{fetch_posts, ErrorTemplate, Params, Post}; +use crate::utils::{fetch_posts, ErrorTemplate, Params, Post}; // STRUCTS #[derive(Template)] @@ -37,7 +34,7 @@ async fn render(sub_name: String, sort: Option, ends: (Option, O } .render() .unwrap(); - Ok(HttpResponse::Ok().status(actix_web::http::StatusCode::NOT_FOUND).content_type("text/html").body(s)) + Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s)) } else { let items = items_result.unwrap(); diff --git a/src/post.rs b/src/post.rs index 31e478a..10499be 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,12 +1,9 @@ // CRATES -use actix_web::{get, web, HttpResponse, Result}; +use actix_web::{get, web, HttpResponse, Result, http::StatusCode}; use askama::Template; use chrono::{TimeZone, Utc}; use pulldown_cmark::{html, Options, Parser}; - -#[path = "utils.rs"] -mod utils; -use utils::{request, val, Comment, ErrorTemplate, Flair, Params, Post}; +use crate::utils::{request, val, Comment, ErrorTemplate, Flair, Params, Post}; // STRUCTS #[derive(Template)] @@ -34,7 +31,7 @@ async fn render(id: String, sort: String) -> Result { } .render() .unwrap(); - return Ok(HttpResponse::Ok().status(actix_web::http::StatusCode::NOT_FOUND).content_type("text/html").body(s)); + return Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s)); } // Otherwise, grab the JSON output from the request diff --git a/src/subreddit.rs b/src/subreddit.rs index 025b2cb..85718db 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -1,10 +1,7 @@ // CRATES -use actix_web::{get, web, HttpResponse, Result}; +use actix_web::{get, web, HttpResponse, Result, http::StatusCode}; use askama::Template; - -#[path = "utils.rs"] -mod utils; -pub use utils::{request, val, fetch_posts, ErrorTemplate, Flair, Params, Post, Subreddit}; +use crate::utils::{request, val, fetch_posts, ErrorTemplate, Params, Post, Subreddit}; // STRUCTS #[derive(Template)] @@ -45,7 +42,7 @@ pub async fn render(sub_name: String, sort: Option, ends: (Option Result { } .render() .unwrap(); - Ok(HttpResponse::Ok().status(actix_web::http::StatusCode::NOT_FOUND).content_type("text/html").body(s)) + Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s)) } else { let s = UserTemplate { user: user.unwrap(), diff --git a/src/utils.rs b/src/utils.rs index 9e498b7..681452f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,6 +2,8 @@ // CRATES // use chrono::{TimeZone, Utc}; +use surf::{get, client, middleware::Redirect}; +use serde_json::{Value, from_str}; // // STRUCTS @@ -157,8 +159,8 @@ pub async fn request(url: String) -> Result { // let body = std::str::from_utf8(res.as_ref())?; // .as_ref converts Bytes to [u8] // --- surf --- - let req = surf::get(&url).header("User-Agent", "libreddit"); - let client = surf::client().with(surf::middleware::Redirect::new(5)); + let req = get(&url).header("User-Agent", "libreddit"); + let client = client().with(Redirect::new(5)); let mut res = client.send(req).await.unwrap(); let success = res.status().is_success(); let body = res.body_string().await.unwrap(); @@ -173,12 +175,12 @@ pub async fn request(url: String) -> Result { // let body = res.text().await.unwrap(); // Parse the response from Reddit as JSON - let json: serde_json::Value = serde_json::from_str(body.as_str()).unwrap_or(serde_json::Value::Null); + let json: Value = from_str(body.as_str()).unwrap_or(Value::Null); if !success { println!("! {} - {}", url, "Page not found"); Err("Page not found") - } else if json == serde_json::Value::Null { + } else if json == Value::Null { println!("! {} - {}", url, "Failed to parse page JSON data"); Err("Failed to parse page JSON data") } else {