From 0cdfefe02d7ade0744adb1bd44796e48440ea52a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 2 Aug 2018 13:57:55 +0200 Subject: Fix clippy issues. --- src/controllers/home_controller.rs | 7 ++++--- src/controllers/login_controller.rs | 5 +++-- src/controllers/posts_controller.rs | 11 ++++++----- src/controllers/users_controller.rs | 11 ++++++----- src/models/post.rs | 22 +++++++++++----------- src/models/user.rs | 17 ++++++++--------- src/utils/mod.rs | 2 +- 7 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs index ba2ea0c..cc0fa8a 100644 --- a/src/controllers/home_controller.rs +++ b/src/controllers/home_controller.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] use models::Post; use posts_controller::ShowPostTemplate; use rocket::{request::FlashMessage, response::NamedFile, Route}; @@ -17,9 +18,9 @@ implement_responder_for!(IndexTemplate); fn index(flash: Option, conn: DbConn) -> Page { Page { title: String::from("Bloggen"), - flash: flash, + flash, content: IndexTemplate { - posts: Post::get_all(conn) + posts: Post::get_all(&conn) .into_iter() .map(|p| ShowPostTemplate { post: p }) .collect(), @@ -29,7 +30,7 @@ fn index(flash: Option, conn: DbConn) -> Page { #[get("/", format = "application/json")] fn index_json(conn: DbConn) -> Json> { - Json(Post::get_all(conn)) + Json(Post::get_all(&conn)) } // diff --git a/src/controllers/login_controller.rs b/src/controllers/login_controller.rs index 6ee5de1..8800c59 100644 --- a/src/controllers/login_controller.rs +++ b/src/controllers/login_controller.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] use models::User; use rocket::{ request::{FlashMessage, Form}, @@ -16,7 +17,7 @@ implement_responder_for!(LoginTemplate); pub fn new(flash: Option) -> Page { Page { title: String::from("Log in"), - flash: flash, + flash, content: LoginTemplate {}, } } @@ -30,7 +31,7 @@ pub struct LoginForm { #[post("/create", data = "")] pub fn create(login: Form, conn: DbConn) -> Flash { let login = login.get(); - if let Ok(user) = User::by_email(&login.email, conn) { + if let Ok(user) = User::by_email(&login.email, &conn) { if user.password == login.password { return Flash::success( Redirect::to("/"), diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 99ff651..781f9f5 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] use comrak::{markdown_to_html, ComrakOptions}; use models::{NewPost, Post}; use rocket::{ @@ -28,7 +29,7 @@ fn new(_conn: DbConn) -> Page { #[post("/create", data = "")] fn create(post: Form, conn: DbConn) -> Flash { - Post::create(post.get(), conn); + Post::create(post.get(), &conn); Flash::success(Redirect::to("/"), "Post successfully created!") } @@ -48,7 +49,7 @@ impl ShowPostTemplate { #[get("/", format = "text/html")] fn show(id: i32, conn: DbConn) -> Page { - let p = Post::get(id, conn); + let p = Post::get(id, &conn); Page { title: p.title.clone(), flash: None, @@ -66,7 +67,7 @@ implement_responder_for!(EditPostTemplate); #[get("//edit", format = "text/html")] fn edit(id: i32, conn: DbConn) -> Page { - let p = Post::get(id, conn); + let p = Post::get(id, &conn); Page { title: String::from("Edit post"), flash: None, @@ -76,13 +77,13 @@ fn edit(id: i32, conn: DbConn) -> Page { #[post("/update", data = "")] fn update(post: Form, conn: DbConn) -> Flash { - Post::update(post.get(), conn); + Post::update(post.get(), &conn); Flash::success(Redirect::to("/"), "Post updated successfully!") } #[get("//delete", format = "text/html")] fn delete(id: i32, conn: DbConn) -> Flash { - Post::delete(id, conn); + Post::delete(id, &conn); Flash::success(Redirect::to("/"), "Post deleted!") } diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs index 4713705..19d5970 100644 --- a/src/controllers/users_controller.rs +++ b/src/controllers/users_controller.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] use models::{NewUser, User}; use rocket::{ request::{FlashMessage, Form}, @@ -18,9 +19,9 @@ implement_responder_for!(UsersTemplate); fn index(flash: Option, conn: DbConn) -> Page { Page { title: String::from("Users"), - flash: flash, + flash, content: UsersTemplate { - users: User::all(conn).unwrap(), + users: User::all(&conn).unwrap(), }, } } @@ -46,7 +47,7 @@ fn edit(id: i32, conn: DbConn) -> Page { #[get("//delete")] fn delete(id: i32, route: &Route, conn: DbConn) -> Flash { - match User::delete(id, conn) { + match User::delete(id, &conn) { Ok(user) => Flash::success( Redirect::to(route.base.path()), format!("User {} was successfully deleted.", user.username), @@ -70,7 +71,7 @@ implement_responder_for!(NewUserTemplate); fn new(flash: Option) -> Page { Page { title: String::from("New user"), - flash: flash, + flash, content: NewUserTemplate { user: Default::default(), }, @@ -120,7 +121,7 @@ impl RegisterUserForm { #[post("/create", data = "")] fn create(user: Form, route: &Route, conn: DbConn) -> Flash { match user.get().new_user() { - Ok(new_user) => match User::create(&new_user, conn) { + Ok(new_user) => match User::create(&new_user, &conn) { Ok(_) => Flash::success( Redirect::to(route.base.path()), "User successfully created!", diff --git a/src/models/post.rs b/src/models/post.rs index bde8e54..c057775 100644 --- a/src/models/post.rs +++ b/src/models/post.rs @@ -20,12 +20,12 @@ pub struct NewPost { } impl Post { - pub fn get_all(conn: DbConn) -> Vec { + pub fn get_all(conn: &DbConn) -> Vec { use schema::posts::dsl::*; posts .filter(published.eq(false)) .limit(5) - .load::(&*conn) + .load::(&**conn) .expect("Error loading posts") } @@ -34,33 +34,33 @@ impl Post { posts .find(post_id) .get_result(&**conn) - .expect(&format!("Unable to find post with id={}", post_id)) + .unwrap_or_else(|_| panic!("Unable to find post with id={}", post_id)) } - pub fn get(post_id: i32, conn: DbConn) -> Post { + pub fn get(post_id: i32, conn: &DbConn) -> Post { Post::get_internal(post_id, &conn) } - pub fn create(new_post: &NewPost, conn: DbConn) { + pub fn create(new_post: &NewPost, conn: &DbConn) { use schema::posts::dsl::*; diesel::insert_into(posts) .values(new_post) - .execute(&*conn) + .execute(&**conn) .expect("Error saving post."); } - pub fn update(updated_post: &Post, conn: DbConn) { + pub fn update(updated_post: &Post, conn: &DbConn) { let p = Post::get_internal(updated_post.id, &conn); diesel::update(&p) .set(updated_post) - .execute(&*conn) + .execute(&**conn) .expect("Error saving post."); } - pub fn delete(post_id: i32, conn: DbConn) { + pub fn delete(post_id: i32, conn: &DbConn) { use schema::posts::dsl::*; diesel::delete(posts.filter(id.eq(post_id))) - .execute(&*conn) - .expect(&format!("Could not delete post with id {}", post_id)); + .execute(&**conn) + .unwrap_or_else(|_| panic!("Could not delete post with id {}", post_id)); } } diff --git a/src/models/user.rs b/src/models/user.rs index 08c54d7..991cb66 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -13,9 +13,9 @@ pub struct User { } impl User { - pub fn all(conn: DbConn) -> QueryResult> { + pub fn all(conn: &DbConn) -> QueryResult> { use schema::users::dsl::*; - users.load::(&*conn) + users.load::(&**conn) } pub fn by_id(user_id: i32, conn: &DbConn) -> QueryResult { @@ -26,28 +26,27 @@ impl User { .map(|ref v| v[0].clone()) } - pub fn by_email(user_email: &str, conn: DbConn) -> QueryResult { + pub fn by_email(user_email: &str, conn: &DbConn) -> QueryResult { use schema::users::dsl::*; users .filter(email.eq(user_email)) - .get_result::(&*conn) + .get_result::(&**conn) } - pub fn create(new_user: &NewUser, conn: DbConn) -> QueryResult { + pub fn create(new_user: &NewUser, conn: &DbConn) -> QueryResult { use schema::users::dsl::*; diesel::insert_into(users) .values(new_user) - .get_result(&*conn) + .get_result(&**conn) } pub fn update(user: &User, conn: &DbConn) -> QueryResult { - use schema::users::dsl::*; user.save_changes(&*conn) } - pub fn delete(user_id: i32, conn: DbConn) -> QueryResult { + pub fn delete(user_id: i32, conn: &DbConn) -> QueryResult { use schema::users::dsl::*; - diesel::delete(users.filter(id.eq(user_id))).get_result::(&*conn) + diesel::delete(users.filter(id.eq(user_id))).get_result::(&**conn) } pub fn realname(&self) -> String { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index ad6cff1..23b551e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -7,7 +7,7 @@ use std::fmt::Display; type Pool = r2d2::Pool>; /// Initializes a database pool. -pub fn init_db_pool<'a>(dburl: &'a str) -> Pool { +pub fn init_db_pool(dburl: &str) -> Pool { let manager = ConnectionManager::::new(dburl); r2d2::Pool::builder().build(manager).expect("db pool") } -- cgit v1.2.3