From b9bce0c426ab258f2c5891ac7eecd944f749e27c Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 1 Aug 2018 14:29:50 +0200 Subject: Run rustfmt on project. --- .rustfmt.toml | 2 + src/controllers/home_controller.rs | 28 ++++--------- src/controllers/login_controller.rs | 23 +++++----- src/controllers/posts_controller.rs | 47 +++++++-------------- src/controllers/users_controller.rs | 83 ++++++++++++++++++------------------- src/main.rs | 21 +++++----- src/models/mod.rs | 4 +- src/models/post.rs | 20 +++++---- src/models/user.rs | 16 +++---- src/schema.rs | 5 +-- src/utils/mod.rs | 17 ++++---- 11 files changed, 124 insertions(+), 142 deletions(-) create mode 100644 .rustfmt.toml diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..b2a448b --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,2 @@ +control_brace_style = "ClosingNextLine" +newline_style = "Unix" diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs index fb88d07..ba2ea0c 100644 --- a/src/controllers/home_controller.rs +++ b/src/controllers/home_controller.rs @@ -1,21 +1,14 @@ use models::Post; -use utils::{ - DbConn, - Page -}; -use rocket::{ - response::NamedFile, - request::FlashMessage, - Route -}; +use posts_controller::ShowPostTemplate; +use rocket::{request::FlashMessage, response::NamedFile, Route}; use rocket_contrib::Json; use std::path::PathBuf; -use posts_controller::ShowPostTemplate; +use utils::{DbConn, Page}; #[derive(BartDisplay)] #[template = "templates/index.html"] struct IndexTemplate { - posts: Vec + posts: Vec, } implement_responder_for!(IndexTemplate); @@ -26,10 +19,11 @@ fn index(flash: Option, conn: DbConn) -> Page { title: String::from("Bloggen"), flash: flash, content: IndexTemplate { - posts: Post::get_all(conn).into_iter() + posts: Post::get_all(conn) + .into_iter() .map(|p| ShowPostTemplate { post: p }) - .collect() - } + .collect(), + }, } } @@ -47,9 +41,5 @@ fn public_file(file: PathBuf) -> Option { } pub fn routes() -> Vec { - routes![ - index, - index_json, - public_file - ] + routes![index, index_json, public_file] } diff --git a/src/controllers/login_controller.rs b/src/controllers/login_controller.rs index 090d299..6ee5de1 100644 --- a/src/controllers/login_controller.rs +++ b/src/controllers/login_controller.rs @@ -1,13 +1,10 @@ +use models::User; use rocket::{ request::{FlashMessage, Form}, response::{Flash, Redirect}, - Route -}; -use models::User; -use utils::{ - DbConn, - Page + Route, }; +use utils::{DbConn, Page}; #[derive(BartDisplay)] #[template = "templates/login.html"] @@ -15,12 +12,12 @@ pub struct LoginTemplate; implement_responder_for!(LoginTemplate); -#[get("/", format="text/html")] +#[get("/", format = "text/html")] pub fn new(flash: Option) -> Page { Page { title: String::from("Log in"), flash: flash, - content: LoginTemplate{} + content: LoginTemplate {}, } } @@ -30,12 +27,18 @@ pub struct LoginForm { password: String, } -#[post("/create", data="")] +#[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 user.password == login.password { - return Flash::success(Redirect::to("/"), format!("{} logged in successfully", user.realname.or(Some(user.username)).unwrap())); + return Flash::success( + Redirect::to("/"), + format!( + "{} logged in successfully", + user.realname.or(Some(user.username)).unwrap() + ), + ); } } diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 7e819b5..99ff651 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -1,22 +1,16 @@ use comrak::{markdown_to_html, ComrakOptions}; +use models::{NewPost, Post}; use rocket::{ - response::{Flash, Redirect}, request::Form, - Route -}; -use models::{ - NewPost, - Post -}; -use utils::{ - DbConn, - Page + response::{Flash, Redirect}, + Route, }; +use utils::{DbConn, Page}; #[derive(BartDisplay)] #[template = "templates/new_post.html"] pub struct NewPostTemplate { - post: NewPost + post: NewPost, } implement_responder_for!(NewPostTemplate); @@ -27,12 +21,12 @@ fn new(_conn: DbConn) -> Page { title: String::from("New post"), flash: None, content: NewPostTemplate { - post: Default::default() - } + post: Default::default(), + }, } } -#[post("/create", data="")] +#[post("/create", data = "")] fn create(post: Form, conn: DbConn) -> Flash { Post::create(post.get(), conn); Flash::success(Redirect::to("/"), "Post successfully created!") @@ -41,7 +35,7 @@ fn create(post: Form, conn: DbConn) -> Flash { #[derive(BartDisplay)] #[template = "templates/show_post.html"] pub struct ShowPostTemplate { - pub post: Post + pub post: Post, } implement_responder_for!(ShowPostTemplate); @@ -58,16 +52,14 @@ fn show(id: i32, conn: DbConn) -> Page { Page { title: p.title.clone(), flash: None, - content: ShowPostTemplate { - post: p - }, + content: ShowPostTemplate { post: p }, } } #[derive(BartDisplay)] #[template = "templates/edit_post.html"] pub struct EditPostTemplate { - post: Post + post: Post, } implement_responder_for!(EditPostTemplate); @@ -78,14 +70,14 @@ fn edit(id: i32, conn: DbConn) -> Page { Page { title: String::from("Edit post"), flash: None, - content: EditPostTemplate { post: p } + content: EditPostTemplate { post: p }, } } -#[post("/update", data="")] +#[post("/update", data = "")] fn update(post: Form, conn: DbConn) -> Flash { - Post::update(post.get(), conn); - Flash::success(Redirect::to("/"), "Post updated successfully!") + Post::update(post.get(), conn); + Flash::success(Redirect::to("/"), "Post updated successfully!") } #[get("//delete", format = "text/html")] @@ -95,12 +87,5 @@ fn delete(id: i32, conn: DbConn) -> Flash { } pub fn routes() -> Vec { - routes![ - new, - create, - show, - edit, - update, - delete - ] + routes![new, create, show, edit, update, delete] } diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs index 0dee68a..4713705 100644 --- a/src/controllers/users_controller.rs +++ b/src/controllers/users_controller.rs @@ -1,21 +1,15 @@ +use models::{NewUser, User}; use rocket::{ - response::{Flash, Redirect}, request::{FlashMessage, Form}, - Route -}; -use models::{ - NewUser, - User -}; -use utils::{ - DbConn, - Page + response::{Flash, Redirect}, + Route, }; +use utils::{DbConn, Page}; #[derive(BartDisplay)] #[template = "templates/users.html"] pub struct UsersTemplate { - users: Vec + users: Vec, } implement_responder_for!(UsersTemplate); @@ -26,15 +20,15 @@ fn index(flash: Option, conn: DbConn) -> Page { title: String::from("Users"), flash: flash, content: UsersTemplate { - users: User::all(conn).unwrap() - } + users: User::all(conn).unwrap(), + }, } } #[derive(BartDisplay)] #[template = "templates/edit_user.html"] pub struct EditUserTemplate { - user: User + user: User, } implement_responder_for!(EditUserTemplate); @@ -45,8 +39,8 @@ fn edit(id: i32, conn: DbConn) -> Page { title: String::from("Edit users"), flash: None, content: EditUserTemplate { - user: User::by_id(id, &conn).unwrap() - } + user: User::by_id(id, &conn).unwrap(), + }, } } @@ -55,19 +49,19 @@ fn delete(id: i32, route: &Route, conn: DbConn) -> Flash { match User::delete(id, conn) { Ok(user) => Flash::success( Redirect::to(route.base.path()), - format!("User {} was successfully deleted.", user.username) + format!("User {} was successfully deleted.", user.username), ), Err(msg) => Flash::error( Redirect::to(route.base.path()), - format!("Error deleting user: {}", msg) - ) + format!("Error deleting user: {}", msg), + ), } } #[derive(BartDisplay)] #[template = "templates/new_user.html"] pub struct NewUserTemplate { - user: NewUser + user: NewUser, } implement_responder_for!(NewUserTemplate); @@ -78,8 +72,8 @@ fn new(flash: Option) -> Page { title: String::from("New user"), flash: flash, content: NewUserTemplate { - user: Default::default() - } + user: Default::default(), + }, } } @@ -89,7 +83,7 @@ struct RegisterUserForm { realname: Option, email: Option, password: String, - password_confirm: String + password_confirm: String, } impl RegisterUserForm { @@ -102,7 +96,7 @@ impl RegisterUserForm { username: self.username.clone(), realname: self.realname.clone(), email: self.email.clone(), - password: self.password.clone() + password: self.password.clone(), }) } } @@ -117,27 +111,33 @@ impl RegisterUserForm { username: self.username.clone(), realname: self.realname.clone(), email: self.email.clone(), - password: self.password.clone() + password: self.password.clone(), }) } } } -#[post("/create", data="")] +#[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(_) => Flash::success(Redirect::to(route.base.path()), "User successfully created!"), - Err(_) => Flash::error(Redirect::to("/"), "Could not create user!") - } + Ok(new_user) => match User::create(&new_user, conn) { + Ok(_) => Flash::success( + Redirect::to(route.base.path()), + "User successfully created!", + ), + Err(_) => Flash::error(Redirect::to("/"), "Could not create user!"), }, - Err(msg) => Flash::error(Redirect::to(&format!("{}/new", route.base.path())), &msg) + Err(msg) => Flash::error(Redirect::to(&format!("{}/new", route.base.path())), &msg), } } -#[post("//update", data="")] -fn update(id: i32, user_form: Form, route: &Route, conn: DbConn) -> Flash { +#[post("//update", data = "")] +fn update( + id: i32, + user_form: Form, + route: &Route, + conn: DbConn, +) -> Flash { match user_form.get().attrs() { Ok(attrs) => { if let Ok(mut user) = User::by_id(id, &conn) { @@ -156,26 +156,25 @@ fn update(id: i32, user_form: Form, route: &Route, conn: DbCon match User::update(&user, &conn) { Ok(user) => Flash::success( Redirect::to(route.base.path()), - format!("User {} updated successfully", user.username) + format!("User {} updated successfully", user.username), ), Err(msg) => Flash::error( Redirect::to(route.base.path()), - format!("User {} was not updated! {}", user.username, msg) - ) - + format!("User {} was not updated! {}", user.username, msg), + ), } } else { Flash::error( Redirect::to(route.base.path()), - "Can't delete users which does not exist" + "Can't delete users which does not exist", ) } - }, + } Err(msg) => Flash::error( Redirect::to(route.base.path()), - format!("Invalid form data: {}", msg) - ) + format!("Invalid form data: {}", msg), + ), } } diff --git a/src/main.rs b/src/main.rs index 4515a2d..b3e45ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,12 @@ #![feature(plugin, custom_derive)] #![plugin(rocket_codegen)] -#[macro_use] extern crate bart_derive; -#[macro_use] extern crate diesel; -#[macro_use] extern crate serde_derive; +#[macro_use] +extern crate bart_derive; +#[macro_use] +extern crate diesel; +#[macro_use] +extern crate serde_derive; extern crate comrak; extern crate dotenv; @@ -12,16 +15,12 @@ extern crate r2d2_diesel; extern crate rocket; extern crate rocket_contrib; -#[macro_use] mod utils; +#[macro_use] +mod utils; +mod controllers; mod models; mod schema; -mod controllers; -use controllers::{ - home_controller, - login_controller, - posts_controller, - users_controller -}; +use controllers::{home_controller, login_controller, posts_controller, users_controller}; fn main() { if let Ok(dburl) = dotenv::var("DATABASE_URL") { diff --git a/src/models/mod.rs b/src/models/mod.rs index f6aba77..3c5acf7 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,7 +1,7 @@ pub mod post; -pub use self::post::Post; pub use self::post::NewPost; +pub use self::post::Post; pub mod user; -pub use self::user::User; pub use self::user::NewUser; +pub use self::user::User; diff --git a/src/models/post.rs b/src/models/post.rs index 17ac74a..be44dda 100644 --- a/src/models/post.rs +++ b/src/models/post.rs @@ -1,6 +1,6 @@ -use schema::posts; -use diesel::prelude::*; use diesel; +use diesel::prelude::*; +use schema::posts; use utils; #[derive(AsChangeset, FromForm, Identifiable, Serialize, Queryable)] @@ -12,7 +12,7 @@ pub struct Post { } #[derive(Default, FromForm, Insertable)] -#[table_name="posts"] +#[table_name = "posts"] pub struct NewPost { pub title: String, pub body: String, @@ -21,16 +21,18 @@ pub struct NewPost { impl Post { pub fn get_all(conn: utils::DbConn) -> Vec { - use ::schema::posts::dsl::*; - posts.filter(published.eq(false)) + use schema::posts::dsl::*; + posts + .filter(published.eq(false)) .limit(5) .load::(&*conn) .expect("Error loading posts") } fn get_internal(post_id: i32, conn: &utils::DbConn) -> Post { - use ::schema::posts::dsl::*; - posts.find(post_id) + use schema::posts::dsl::*; + posts + .find(post_id) .get_result(&**conn) .expect(&format!("Unable to find post with id={}", post_id)) } @@ -40,7 +42,7 @@ impl Post { } pub fn create(new_post: &NewPost, conn: utils::DbConn) { - use ::schema::posts::dsl::*; + use schema::posts::dsl::*; diesel::insert_into(posts) .values(new_post) .execute(&*conn) @@ -56,7 +58,7 @@ impl Post { } pub fn delete(post_id: i32, conn: utils::DbConn) { - use ::schema::posts::dsl::*; + use schema::posts::dsl::*; diesel::delete(posts.filter(id.eq(post_id))) .execute(&*conn) .expect(&format!("Could not delete post with id {}", post_id)); diff --git a/src/models/user.rs b/src/models/user.rs index 82a359b..37c5333 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -9,7 +9,7 @@ pub struct User { pub username: String, pub realname: Option, pub email: Option, - pub password: String + pub password: String, } impl User { @@ -20,31 +20,33 @@ impl User { pub fn by_id(user_id: i32, conn: &utils::DbConn) -> QueryResult { use schema::users::dsl::*; - users.filter(id.eq(user_id)) + users + .filter(id.eq(user_id)) .load::(&**conn) .map(|ref v| v[0].clone()) } pub fn by_email(user_email: &str, conn: utils::DbConn) -> QueryResult { use schema::users::dsl::*; - users.filter(email.eq(user_email)) + users + .filter(email.eq(user_email)) .get_result::(&*conn) } pub fn create(new_user: &NewUser, conn: utils::DbConn) -> QueryResult { - use ::schema::users::dsl::*; + use schema::users::dsl::*; diesel::insert_into(users) .values(new_user) .get_result(&*conn) } pub fn update(user: &User, conn: &utils::DbConn) -> QueryResult { - use ::schema::users::dsl::*; + use schema::users::dsl::*; user.save_changes(&*conn) } pub fn delete(user_id: i32, conn: utils::DbConn) -> QueryResult { - use ::schema::users::dsl::*; + use schema::users::dsl::*; diesel::delete(users.filter(id.eq(user_id))).get_result::(&*conn) } @@ -58,7 +60,7 @@ impl User { } #[derive(Default, Insertable)] -#[table_name="users"] +#[table_name = "users"] pub struct NewUser { pub username: String, pub realname: Option, diff --git a/src/schema.rs b/src/schema.rs index ad122fd..cff43ec 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -17,7 +17,4 @@ table! { } } -allow_tables_to_appear_in_same_query!( - posts, - users, -); +allow_tables_to_appear_in_same_query!(posts, users,); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 5392224..ad6cff1 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -12,10 +12,10 @@ pub fn init_db_pool<'a>(dburl: &'a str) -> Pool { r2d2::Pool::builder().build(manager).expect("db pool") } -use std::ops::Deref; use rocket::http::Status; use rocket::request::{self, FlashMessage, FromRequest}; -use rocket::{Request, State, Outcome}; +use rocket::{Outcome, Request, State}; +use std::ops::Deref; // Connection request guard type: a wrapper around an r2d2 pooled connection. pub struct DbConn(pub r2d2::PooledConnection>); @@ -30,7 +30,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for DbConn { let pool = request.guard::>()?; match pool.get() { Ok(conn) => Outcome::Success(DbConn(conn)), - Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())) + Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())), } } } @@ -49,7 +49,7 @@ impl Deref for DbConn { pub struct Page { pub title: String, pub flash: Option, - pub content: T + pub content: T, } macro_rules! implement_responder_for { @@ -59,14 +59,17 @@ macro_rules! implement_responder_for { // like this: `$template_type<'a>` // So it will have to be passed in to the argument at // the macro incovation instead. - ($template_type:ty) => ( + ($template_type:ty) => { impl<'a> ::rocket::response::Responder<'a> for ::utils::Page<$template_type> { - fn respond_to(self, _: &::rocket::Request) -> Result<::rocket::Response<'static>, ::rocket::http::Status> { + fn respond_to( + self, + _: &::rocket::Request, + ) -> Result<::rocket::Response<'static>, ::rocket::http::Status> { ::rocket::Response::build() .header(::rocket::http::ContentType::HTML) .sized_body(::std::io::Cursor::new(format!("{}", &self))) .ok() } } - ) + }; } -- cgit v1.2.3