Remove .clone() in favor of borrowing

This commit is contained in:
spikecodes 2021-01-01 12:55:09 -08:00
parent d43b49e7e4
commit 59ef30c76d
6 changed files with 16 additions and 21 deletions

View File

@ -38,7 +38,7 @@ async fn main() -> std::io::Result<()> {
} }
// start http server // 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(|| { HttpServer::new(|| {
App::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}/", web::get().to(post::item))
.route("/r/{sub}/comments/{id}/{title}/{comment_id}/", 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)) .unwrap_or_else(|_| panic!("Cannot bind to the address: {}", address))
.run() .run()
.await .await

View File

@ -26,7 +26,7 @@ pub async fn item(req: HttpRequest) -> Result<HttpResponse> {
dbg!(&id); dbg!(&id);
// Send a request to the url, receive JSON in response // 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 the Reddit API returns an error, exit and send error page to user
if req.is_err() { if req.is_err() {
@ -36,8 +36,8 @@ pub async fn item(req: HttpRequest) -> Result<HttpResponse> {
let res = req.unwrap(); let res = req.unwrap();
// Parse the JSON into Post and Comment structs // Parse the JSON into Post and Comment structs
let post = parse_post(res[0].clone()).await.unwrap(); let post = parse_post(&res[0]).await.unwrap();
let comments = parse_comments(res[1].clone()).await.unwrap(); let comments = parse_comments(&res[1]).await.unwrap();
// Use the Post and Comment structs to generate a website to show users // Use the Post and Comment structs to generate a website to show users
let s = PostTemplate { comments, post, sort }.render().unwrap(); let s = PostTemplate { comments, post, sort }.render().unwrap();
@ -66,7 +66,7 @@ async fn media(data: &serde_json::Value) -> (String, String) {
} }
// POSTS // POSTS
async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> { async fn parse_post(json: &serde_json::Value) -> Result<Post, &'static str> {
// Retrieve post (as opposed to comments) from JSON // Retrieve post (as opposed to comments) from JSON
let post_data: &serde_json::Value = &json["data"]["children"][0]; let post_data: &serde_json::Value = &json["data"]["children"][0];
@ -114,7 +114,7 @@ async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
// COMMENTS // COMMENTS
#[async_recursion] #[async_recursion]
async fn parse_comments(json: serde_json::Value) -> Result<Vec<Comment>, &'static str> { async fn parse_comments(json: &serde_json::Value) -> Result<Vec<Comment>, &'static str> {
// Separate the comment JSON into a Vector of comments // Separate the comment JSON into a Vector of comments
let comment_data = json["data"]["children"].as_array().unwrap(); let comment_data = json["data"]["children"].as_array().unwrap();
@ -131,7 +131,7 @@ async fn parse_comments(json: serde_json::Value) -> Result<Vec<Comment>, &'stati
let body = val(comment, "body_html"); let body = val(comment, "body_html");
let replies: Vec<Comment> = if comment["data"]["replies"].is_object() { let replies: Vec<Comment> = 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 { } else {
Vec::new() Vec::new()
}; };

View File

@ -26,7 +26,7 @@ pub async fn find(req: HttpRequest) -> Result<HttpResponse> {
}; };
let sub = req.match_info().get("sub").unwrap_or("").to_string(); 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() { if posts.is_err() {
error(posts.err().unwrap().to_string()).await error(posts.err().unwrap().to_string()).await

View File

@ -26,7 +26,7 @@ pub async fn page(req: HttpRequest) -> Result<HttpResponse> {
} else { } else {
Ok(Subreddit::default()) Ok(Subreddit::default())
}; };
let posts = fetch_posts(path.clone(), String::new()).await; let posts = fetch_posts(&path, String::new()).await;
if posts.is_err() { if posts.is_err() {
error(posts.err().unwrap().to_string()).await error(posts.err().unwrap().to_string()).await
@ -52,7 +52,7 @@ async fn subreddit(sub: &str) -> Result<Subreddit, &'static str> {
let url: String = format!("r/{}/about.json?raw_json=1", sub); let url: String = format!("r/{}/about.json?raw_json=1", sub);
// Send a request to the url, receive JSON in response // 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 the Reddit API returns an error, exit this function
if req.is_err() { if req.is_err() {

View File

@ -24,7 +24,7 @@ pub async fn profile(req: HttpRequest) -> Result<HttpResponse> {
// Request user profile data and user posts/comments from Reddit // Request user profile data and user posts/comments from Reddit
let user = user(&username).await; 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 there is an error show error page
if user.is_err() || posts.is_err() { if user.is_err() || posts.is_err() {
@ -44,18 +44,13 @@ pub async fn profile(req: HttpRequest) -> Result<HttpResponse> {
} }
} }
// SERVICES
// pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
// render(username, params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await
// }
// USER // USER
async fn user(name: &str) -> Result<User, &'static str> { async fn user(name: &str) -> Result<User, &'static str> {
// Build the Reddit JSON API path // Build the Reddit JSON API path
let path: String = format!("user/{}/about.json", name); let path: String = format!("user/{}/about.json", name);
// Send a request to the url, receive JSON in response // 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 the Reddit API returns an error, exit this function
if req.is_err() { if req.is_err() {

View File

@ -139,9 +139,9 @@ pub fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String {
} }
// Fetch posts of a user or subreddit // Fetch posts of a user or subreddit
pub async fn fetch_posts(path: String, fallback_title: String) -> Result<(Vec<Post>, String), &'static str> { pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post>, String), &'static str> {
// Send a request to the url, receive JSON in response // 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 the Reddit API returns an error, exit this function
if req.is_err() { if req.is_err() {
@ -211,7 +211,7 @@ pub async fn error(message: String) -> Result<HttpResponse> {
} }
// Make a request to a Reddit API and parse the JSON response // Make a request to a Reddit API and parse the JSON response
pub async fn request(path: String) -> Result<serde_json::Value, &'static str> { pub async fn request(path: &str) -> Result<serde_json::Value, &'static str> {
let url = format!("https://www.reddit.com/{}", path); let url = format!("https://www.reddit.com/{}", path);
// --- actix-web::client --- // --- actix-web::client ---