From 59ef30c76d1504392fc739a69c73d87289c23cd4 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Fri, 1 Jan 2021 12:55:09 -0800 Subject: [PATCH] Remove .clone() in favor of borrowing --- src/main.rs | 4 ++-- src/post.rs | 12 ++++++------ src/search.rs | 2 +- src/subreddit.rs | 4 ++-- src/user.rs | 9 ++------- src/utils.rs | 6 +++--- 6 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index ddee86c..f45ed70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,7 @@ async fn main() -> std::io::Result<()> { } // start http server - println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), address.clone()); + println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), &address); HttpServer::new(|| { App::new() @@ -71,7 +71,7 @@ async fn main() -> std::io::Result<()> { .route("/r/{sub}/comments/{id}/{title}/", web::get().to(post::item)) .route("/r/{sub}/comments/{id}/{title}/{comment_id}/", web::get().to(post::item)) }) - .bind(address.clone()) + .bind(&address) .unwrap_or_else(|_| panic!("Cannot bind to the address: {}", address)) .run() .await diff --git a/src/post.rs b/src/post.rs index 668912b..98837c9 100644 --- a/src/post.rs +++ b/src/post.rs @@ -26,7 +26,7 @@ pub async fn item(req: HttpRequest) -> Result { dbg!(&id); // Send a request to the url, receive JSON in response - let req = request(path.clone()).await; + let req = request(&path).await; // If the Reddit API returns an error, exit and send error page to user if req.is_err() { @@ -36,8 +36,8 @@ pub async fn item(req: HttpRequest) -> Result { let res = req.unwrap(); // Parse the JSON into Post and Comment structs - let post = parse_post(res[0].clone()).await.unwrap(); - let comments = parse_comments(res[1].clone()).await.unwrap(); + let post = parse_post(&res[0]).await.unwrap(); + let comments = parse_comments(&res[1]).await.unwrap(); // Use the Post and Comment structs to generate a website to show users let s = PostTemplate { comments, post, sort }.render().unwrap(); @@ -66,7 +66,7 @@ async fn media(data: &serde_json::Value) -> (String, String) { } // POSTS -async fn parse_post(json: serde_json::Value) -> Result { +async fn parse_post(json: &serde_json::Value) -> Result { // Retrieve post (as opposed to comments) from JSON let post_data: &serde_json::Value = &json["data"]["children"][0]; @@ -114,7 +114,7 @@ async fn parse_post(json: serde_json::Value) -> Result { // COMMENTS #[async_recursion] -async fn parse_comments(json: serde_json::Value) -> Result, &'static str> { +async fn parse_comments(json: &serde_json::Value) -> Result, &'static str> { // Separate the comment JSON into a Vector of comments let comment_data = json["data"]["children"].as_array().unwrap(); @@ -131,7 +131,7 @@ async fn parse_comments(json: serde_json::Value) -> Result, &'stati let body = val(comment, "body_html"); let replies: Vec = if comment["data"]["replies"].is_object() { - parse_comments(comment["data"]["replies"].clone()).await.unwrap_or_default() + parse_comments(&comment["data"]["replies"]).await.unwrap_or_default() } else { Vec::new() }; diff --git a/src/search.rs b/src/search.rs index 957aef7..3e055f5 100644 --- a/src/search.rs +++ b/src/search.rs @@ -26,7 +26,7 @@ pub async fn find(req: HttpRequest) -> Result { }; let sub = req.match_info().get("sub").unwrap_or("").to_string(); - let posts = fetch_posts(path.clone(), String::new()).await; + let posts = fetch_posts(&path, String::new()).await; if posts.is_err() { error(posts.err().unwrap().to_string()).await diff --git a/src/subreddit.rs b/src/subreddit.rs index ac5ff27..2f76bf1 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -26,7 +26,7 @@ pub async fn page(req: HttpRequest) -> Result { } else { Ok(Subreddit::default()) }; - let posts = fetch_posts(path.clone(), String::new()).await; + let posts = fetch_posts(&path, String::new()).await; if posts.is_err() { error(posts.err().unwrap().to_string()).await @@ -52,7 +52,7 @@ async fn subreddit(sub: &str) -> Result { let url: String = format!("r/{}/about.json?raw_json=1", sub); // Send a request to the url, receive JSON in response - let req = request(url).await; + let req = request(&url).await; // If the Reddit API returns an error, exit this function if req.is_err() { diff --git a/src/user.rs b/src/user.rs index 6324522..ebd6962 100644 --- a/src/user.rs +++ b/src/user.rs @@ -24,7 +24,7 @@ pub async fn profile(req: HttpRequest) -> Result { // Request user profile data and user posts/comments from Reddit let user = user(&username).await; - let posts = fetch_posts(path.clone(), "Comment".to_string()).await; + let posts = fetch_posts(&path, "Comment".to_string()).await; // If there is an error show error page if user.is_err() || posts.is_err() { @@ -44,18 +44,13 @@ pub async fn profile(req: HttpRequest) -> Result { } } -// SERVICES -// pub async fn page(web::Path(username): web::Path, params: web::Query) -> Result { -// render(username, params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await -// } - // USER async fn user(name: &str) -> Result { // Build the Reddit JSON API path let path: String = format!("user/{}/about.json", name); // Send a request to the url, receive JSON in response - let req = request(path).await; + let req = request(&path).await; // If the Reddit API returns an error, exit this function if req.is_err() { diff --git a/src/utils.rs b/src/utils.rs index f559b76..45f582c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -139,9 +139,9 @@ pub fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String { } // Fetch posts of a user or subreddit -pub async fn fetch_posts(path: String, fallback_title: String) -> Result<(Vec, String), &'static str> { +pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec, String), &'static str> { // Send a request to the url, receive JSON in response - let req = request(path.clone()).await; + let req = request(path).await; // If the Reddit API returns an error, exit this function if req.is_err() { @@ -211,7 +211,7 @@ pub async fn error(message: String) -> Result { } // Make a request to a Reddit API and parse the JSON response -pub async fn request(path: String) -> Result { +pub async fn request(path: &str) -> Result { let url = format!("https://www.reddit.com/{}", path); // --- actix-web::client ---