diff --git a/src/main.rs b/src/main.rs index f029388..f539e5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,8 +34,7 @@ async fn main() -> std::io::Result<()> { if args.len() > 1 { for arg in args { if arg.starts_with("--address=") || arg.starts_with("-a=") { - let split: Vec<&str> = arg.split('=').collect(); - address = split[1].to_string(); + address = arg.split('=').collect::>()[1].to_string(); } } } @@ -81,6 +80,9 @@ async fn main() -> std::io::Result<()> { .route("/r/{sub}/comments/{id}/{title}/{comment_id}/", web::get().to(post::item)) }) .bind(&address) + .map(|x| { + x + }) .unwrap_or_else(|_| panic!("Cannot bind to the address: {}", address)) .run() .await diff --git a/src/post.rs b/src/post.rs index 381855c..a4c33d5 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,6 +1,6 @@ // CRATES use crate::utils::{cookie, error, format_num, format_url, media, param, request, rewrite_url, val, Comment, Flags, Flair, Post}; -use actix_web::{HttpRequest, HttpResponse, Result}; +use actix_web::{HttpRequest, HttpResponse}; use async_recursion::async_recursion; @@ -30,8 +30,8 @@ pub async fn item(req: HttpRequest) -> HttpResponse { // Otherwise, grab the JSON output from the request Ok(res) => { // Parse the JSON into Post and Comment structs - let post = parse_post(&res[0]).await.unwrap(); - let comments = parse_comments(&res[1]).await.unwrap(); + let post = parse_post(&res[0]).await; + let comments = parse_comments(&res[1]).await; // Use the Post and Comment structs to generate a website to show users let s = PostTemplate { @@ -50,7 +50,7 @@ pub async fn item(req: HttpRequest) -> HttpResponse { } // POSTS -async fn parse_post(json: &serde_json::Value) -> Result { +async fn parse_post(json: &serde_json::Value) -> Post { // Retrieve post (as opposed to comments) from JSON let post: &serde_json::Value = &json["data"]["children"][0]; @@ -61,10 +61,10 @@ async fn parse_post(json: &serde_json::Value) -> Result { let ratio: f64 = post["data"]["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0; // Determine the type of media along with the media URL - let media = media(&post["data"]).await; + let (post_type, media) = media(&post["data"]).await; // Build a post using data parsed from Reddit post API - Ok(Post { + Post { id: val(post, "id"), title: val(post, "title"), community: val(post, "subreddit"), @@ -78,7 +78,7 @@ async fn parse_post(json: &serde_json::Value) -> Result { permalink: val(post, "permalink"), score: format_num(score), upvote_ratio: ratio as i64, - post_type: media.0, + post_type, thumbnail: format_url(val(post, "thumbnail")), flair: Flair( val(post, "link_flair_text"), @@ -93,14 +93,14 @@ async fn parse_post(json: &serde_json::Value) -> Result { nsfw: post["data"]["over_18"].as_bool().unwrap_or(false), stickied: post["data"]["stickied"].as_bool().unwrap_or(false), }, - media: media.1, + media, time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"), - }) + } } // COMMENTS #[async_recursion] -async fn parse_comments(json: &serde_json::Value) -> Result, &'static str> { +async fn parse_comments(json: &serde_json::Value) -> Vec { // Separate the comment JSON into a Vector of comments let comment_data = json["data"]["children"].as_array().unwrap(); @@ -117,7 +117,7 @@ async fn parse_comments(json: &serde_json::Value) -> Result, &'stat let body = rewrite_url(&val(comment, "body_html")); let replies: Vec = if comment["data"]["replies"].is_object() { - parse_comments(&comment["data"]["replies"]).await.unwrap_or_default() + parse_comments(&comment["data"]["replies"]).await } else { Vec::new() }; @@ -137,5 +137,5 @@ async fn parse_comments(json: &serde_json::Value) -> Result, &'stat }); } - Ok(comments) + comments } diff --git a/src/search.rs b/src/search.rs index 89c34f7..74dd207 100644 --- a/src/search.rs +++ b/src/search.rs @@ -33,16 +33,16 @@ pub async fn find(req: HttpRequest) -> HttpResponse { let sub = req.match_info().get("sub").unwrap_or("").to_string(); match fetch_posts(&path, String::new()).await { - Ok(posts) => HttpResponse::Ok().content_type("text/html").body( + Ok((posts, after)) => HttpResponse::Ok().content_type("text/html").body( SearchTemplate { - posts: posts.0, + posts, sub, params: SearchParams { q: param(&path, "q"), sort, t: param(&path, "t"), before: param(&path, "after"), - after: posts.1, + after, restrict_sr: param(&path, "restrict_sr"), }, layout: cookie(req, "layout"), diff --git a/src/subreddit.rs b/src/subreddit.rs index 376877f..9009ba6 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -36,12 +36,12 @@ pub async fn page(req: HttpRequest) -> HttpResponse { }; match fetch_posts(&path, String::new()).await { - Ok(items) => { + Ok((posts,after)) => { let s = SubredditTemplate { sub: sub_result, - posts: items.0, + posts: posts, sort: (sort, param(&path, "t")), - ends: (param(&path, "after"), items.1), + ends: (param(&path, "after"), after), layout: cookie(req, "layout"), } .render() diff --git a/src/user.rs b/src/user.rs index fb74dbf..c43388f 100644 --- a/src/user.rs +++ b/src/user.rs @@ -29,12 +29,12 @@ pub async fn profile(req: HttpRequest) -> HttpResponse { let posts = fetch_posts(&path, "Comment".to_string()).await; match posts { - Ok(items) => { + Ok((posts, after)) => { let s = UserTemplate { user: user.unwrap(), - posts: items.0, + posts, sort: (sort, param(&path, "t")), - ends: (param(&path, "after"), items.1), + ends: (param(&path, "after"), after), layout: cookie(req, "layout"), } .render() diff --git a/src/utils.rs b/src/utils.rs index 69f347a..d183562 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -108,7 +108,7 @@ pub fn param(path: &str, value: &str) -> String { pairs.get(value).unwrap_or(&String::new()).to_owned() } -// Cookie value from request +// Parse Cookie value from request pub fn cookie(req: actix_web::HttpRequest, name: &str) -> String { actix_web::HttpMessage::cookie(&req, name).unwrap_or_else(|| Cookie::new(name, "")).value().to_string() } @@ -172,7 +172,7 @@ pub fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String { String::from(j["data"][n][k].as_str().unwrap_or_default()) } -// Fetch posts of a user or subreddit +// Fetch posts of a user or subreddit and return a vector of posts and the "after" value pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec, String), &'static str> { let res; let post_list; @@ -180,9 +180,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec { - res = response; - } + Ok(response) => { res = response; } // If the Reddit API returns an error, exit this function Err(msg) => return Err(msg), } @@ -203,7 +201,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Result<(Vec