From eb735a42fede5ee79024ffedb41cc63e94de91c6 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Mon, 11 Jan 2021 18:05:13 -0800 Subject: [PATCH] Handle comment parsing errors --- src/post.rs | 17 ++++++++++------- src/utils.rs | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/post.rs b/src/post.rs index d658db3..f84d424 100644 --- a/src/post.rs +++ b/src/post.rs @@ -115,7 +115,10 @@ async fn parse_post(json: &serde_json::Value) -> Post { #[async_recursion] 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(); + let comment_data = match json["data"]["children"].as_array() { + Some(f) => f.to_owned(), + None => { let v = Vec::new(); v } + }; let mut comments: Vec = Vec::new(); @@ -127,7 +130,7 @@ async fn parse_comments(json: &serde_json::Value) -> Vec { } let score = comment["data"]["score"].as_i64().unwrap_or(0); - let body = rewrite_url(&val(comment, "body_html")); + let body = rewrite_url(&val(&comment, "body_html")); let replies: Vec = if comment["data"]["replies"].is_object() { parse_comments(&comment["data"]["replies"]).await @@ -136,16 +139,16 @@ async fn parse_comments(json: &serde_json::Value) -> Vec { }; comments.push(Comment { - id: val(comment, "id"), + id: val(&comment, "id"), body, - author: val(comment, "author"), + author: val(&comment, "author"), score: format_num(score), time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"), replies, flair: Flair( - val(comment, "author_flair_text"), - val(comment, "author_flair_background_color"), - val(comment, "author_flair_text_color"), + val(&comment, "author_flair_text"), + val(&comment, "author_flair_background_color"), + val(&comment, "author_flair_text_color"), ), }); } diff --git a/src/utils.rs b/src/utils.rs index e925bb7..443e6d0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -172,7 +172,7 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { post_type = "image"; match preview["variants"]["mp4"].as_object() { Some(gif) => format_url(gif["source"]["url"].as_str().unwrap_or_default()), - None => format_url(preview["source"]["url"].as_str().unwrap_or_default()) + None => format_url(preview["source"]["url"].as_str().unwrap_or_default()), } } else if data["is_self"].as_bool().unwrap_or_default() { post_type = "self"; @@ -182,7 +182,7 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { data["url"].as_str().unwrap_or_default().to_string() }; - (post_type.to_string(), url.to_string()) + (post_type.to_string(), url) } //