From 62df3f239aff3084af9463aee35a6003701c14bc Mon Sep 17 00:00:00 2001 From: ATechnoHazard Date: Fri, 26 Jun 2020 16:44:07 +0530 Subject: [PATCH] cargo fmt Signed-off-by: ATechnoHazard --- src/api/catchers/mod.rs | 47 +++++++++++++++--------- src/api/guards/db.rs | 12 +++---- src/api/guards/mod.rs | 2 +- src/api/misc/mod.rs | 1 + src/api/mod.rs | 2 +- src/api/routes/health.rs | 9 ++--- src/api/routes/mod.rs | 1 - src/api/routes/paste.rs | 71 +++++++++++++++++++++++-------------- src/api/routes/responder.rs | 1 + src/core/mod.rs | 2 +- src/core/paste/entity.rs | 2 +- src/core/paste/mod.rs | 2 +- src/core/paste/postgres.rs | 2 +- src/core/paste/service.rs | 2 +- src/core/users/entity.rs | 2 +- src/core/users/mod.rs | 4 +-- src/core/users/postgres.rs | 6 ++-- src/core/users/service.rs | 30 ++++++++-------- src/main.rs | 11 +++--- src/schema.rs | 5 +-- src/utils/db.rs | 4 +-- src/utils/errors.rs | 37 +++++++++++-------- src/utils/mod.rs | 4 +-- src/utils/phonetic_key.rs | 2 +- 24 files changed, 147 insertions(+), 114 deletions(-) diff --git a/src/api/catchers/mod.rs b/src/api/catchers/mod.rs index ca6e3a4..4eecef0 100644 --- a/src/api/catchers/mod.rs +++ b/src/api/catchers/mod.rs @@ -1,34 +1,47 @@ -use rocket::Rocket; -use rocket::response::status; use rocket::http::Status; +use rocket::response::status; +use rocket::Rocket; use rocket_contrib::json::Json; use serde_json::Value; #[catch(404)] fn not_found() -> status::Custom> { - status::Custom(Status::NotFound, Json(json!({ - "err":"route not found", - "msg": "The given route does not exist" - }))) + status::Custom( + Status::NotFound, + Json(json!({ + "err":"route not found", + "msg": "The given route does not exist" + })), + ) } #[catch(422)] fn unprocessable_entity() -> status::Custom> { - status::Custom(Status::UnprocessableEntity, Json(json!({ - "err":"failed to process entity", - "msg": "The given object could not be processed. This could be due to sending \ - malformed or incomplete JSON objects in the request body." - }))) + status::Custom( + Status::UnprocessableEntity, + Json(json!({ + "err":"failed to process entity", + "msg": "The given object could not be processed. This could be due to sending \ + malformed or incomplete JSON objects in the request body." + })), + ) } #[catch(500)] fn internal_server_error() -> status::Custom> { - status::Custom(Status::NotFound, Json(json!({ - "err":"internal server error", - "msg": "Something went wrong, try again" - }))) + status::Custom( + Status::NotFound, + Json(json!({ + "err":"internal server error", + "msg": "Something went wrong, try again" + })), + ) } pub fn fuel(rocket: Rocket) -> Rocket { - rocket.register(catchers![not_found, unprocessable_entity, internal_server_error]) -} \ No newline at end of file + rocket.register(catchers![ + not_found, + unprocessable_entity, + internal_server_error + ]) +} diff --git a/src/api/guards/db.rs b/src/api/guards/db.rs index 540b205..516b0bd 100644 --- a/src/api/guards/db.rs +++ b/src/api/guards/db.rs @@ -1,9 +1,9 @@ -use diesel::PgConnection; -use diesel::r2d2::{PooledConnection, ConnectionManager}; -use rocket::{Request, request, State, Outcome}; -use rocket::request::FromRequest; -use rocket::http::Status; use crate::utils::db::Pool; +use diesel::r2d2::{ConnectionManager, PooledConnection}; +use diesel::PgConnection; +use rocket::http::Status; +use rocket::request::FromRequest; +use rocket::{request, Outcome, Request, State}; use std::ops::Deref; pub struct DbConn(pub PooledConnection>); @@ -26,4 +26,4 @@ impl Deref for DbConn { fn deref(&self) -> &Self::Target { &self.0 } -} \ No newline at end of file +} diff --git a/src/api/guards/mod.rs b/src/api/guards/mod.rs index 8c5eabd..dec1023 100644 --- a/src/api/guards/mod.rs +++ b/src/api/guards/mod.rs @@ -1 +1 @@ -pub mod db; \ No newline at end of file +pub mod db; diff --git a/src/api/misc/mod.rs b/src/api/misc/mod.rs index e69de29..8b13789 100644 --- a/src/api/misc/mod.rs +++ b/src/api/misc/mod.rs @@ -0,0 +1 @@ + diff --git a/src/api/mod.rs b/src/api/mod.rs index b6aba30..a9fc3cc 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,5 +1,5 @@ -pub mod routes; pub mod catchers; +pub mod routes; mod guards; mod misc; diff --git a/src/api/routes/health.rs b/src/api/routes/health.rs index 7fe4f55..282cd9c 100644 --- a/src/api/routes/health.rs +++ b/src/api/routes/health.rs @@ -1,9 +1,10 @@ -use rocket::{Rocket}; +use rocket::Rocket; #[get("/")] -fn check() -> &'static str { "OK" } - +fn check() -> &'static str { + "OK" +} pub fn fuel(rocket: Rocket) -> Rocket { rocket.mount("/api/health", routes![check]) -} \ No newline at end of file +} diff --git a/src/api/routes/mod.rs b/src/api/routes/mod.rs index d98a57d..5708a83 100644 --- a/src/api/routes/mod.rs +++ b/src/api/routes/mod.rs @@ -10,4 +10,3 @@ pub fn fuel(rocket: Rocket) -> Rocket { rocket = paste::fuel(rocket); rocket } - diff --git a/src/api/routes/paste.rs b/src/api/routes/paste.rs index 774991e..107c973 100644 --- a/src/api/routes/paste.rs +++ b/src/api/routes/paste.rs @@ -1,14 +1,18 @@ use std::ops::DerefMut; -use rocket::{http::{Cookie, Cookies, Status}, response::status, Rocket}; use rocket::response::status::Custom; +use rocket::{ + http::{Cookie, Cookies, Status}, + response::status, + Rocket, +}; use rocket_contrib::json::Json; use serde_json::Value; -use crate::core::paste::{entity::Paste, service::create_paste, service::fetch_paste}; -use crate::core::users::{service::create_or_fetch_user}; -use crate::utils::phonetic_key; use crate::api::guards::db; +use crate::core::paste::{entity::Paste, service::create_paste, service::fetch_paste}; +use crate::core::users::service::create_or_fetch_user; +use crate::utils::phonetic_key; use diesel::result::Error; @@ -27,10 +31,15 @@ fn create(mut paste: Json, conn: db::DbConn, mut ck: Cookies) -> Custom user, - Err(e) => return status::Custom(Status::InternalServerError, Json(json!({ - "err": e.to_string(), - "msg": "Failed to create or fetch user" - }))) + Err(e) => { + return status::Custom( + Status::InternalServerError, + Json(json!({ + "err": e.to_string(), + "msg": "Failed to create or fetch user" + })), + ) + } }; let new_paste = paste.deref_mut(); @@ -41,18 +50,20 @@ fn create(mut paste: Json, conn: db::DbConn, mut ck: Cookies) -> Custom { - status::Custom(Status::Created, Json(json!({ - "msg": "Successfully created paste", - "paste_id": new_paste.id - }))) - } - Err(e) => { - status::Custom(Status::InternalServerError, Json(json!({ + Ok(_) => status::Custom( + Status::Created, + Json(json!({ + "msg": "Successfully created paste", + "paste_id": new_paste.id + })), + ), + Err(e) => status::Custom( + Status::InternalServerError, + Json(json!({ "err": e.to_string(), "msg": "Failed to create paste" - }))) - } + })), + ), } } @@ -62,14 +73,20 @@ fn fetch(id: String, conn: db::DbConn) -> Custom> { Ok(paste) => paste, Err(err) => { return match err.downcast_ref::() { - Some(Error::NotFound) => Custom(Status::NotFound, Json(json!({ - "err": err.to_string(), - "msg": "Unable to find a paste with that ID" - }))), - _ => Custom(Status::InternalServerError, Json(json!({ - "err": err.to_string(), - "msg": "Something went wrong, try again" - }))) + Some(Error::NotFound) => Custom( + Status::NotFound, + Json(json!({ + "err": err.to_string(), + "msg": "Unable to find a paste with that ID" + })), + ), + _ => Custom( + Status::InternalServerError, + Json(json!({ + "err": err.to_string(), + "msg": "Something went wrong, try again" + })), + ), } } }; @@ -79,4 +96,4 @@ fn fetch(id: String, conn: db::DbConn) -> Custom> { pub fn fuel(rocket: Rocket) -> Rocket { rocket.mount("/api/paste", routes![create, fetch]) -} \ No newline at end of file +} diff --git a/src/api/routes/responder.rs b/src/api/routes/responder.rs index e69de29..8b13789 100644 --- a/src/api/routes/responder.rs +++ b/src/api/routes/responder.rs @@ -0,0 +1 @@ + diff --git a/src/core/mod.rs b/src/core/mod.rs index 304e7af..866feb5 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,2 +1,2 @@ pub mod paste; -pub mod users; \ No newline at end of file +pub mod users; diff --git a/src/core/paste/entity.rs b/src/core/paste/entity.rs index 82d8c3a..e41e88a 100644 --- a/src/core/paste/entity.rs +++ b/src/core/paste/entity.rs @@ -7,4 +7,4 @@ pub struct Paste { pub belongs_to: Option, pub is_url: bool, pub content: String, -} \ No newline at end of file +} diff --git a/src/core/paste/mod.rs b/src/core/paste/mod.rs index 7807cca..b06ddb4 100644 --- a/src/core/paste/mod.rs +++ b/src/core/paste/mod.rs @@ -1,3 +1,3 @@ pub mod entity; pub mod postgres; -pub mod service; \ No newline at end of file +pub mod service; diff --git a/src/core/paste/postgres.rs b/src/core/paste/postgres.rs index 2daf29a..798ef8d 100644 --- a/src/core/paste/postgres.rs +++ b/src/core/paste/postgres.rs @@ -16,4 +16,4 @@ pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result { pub fn fetch_paste(id: String, conn: &PgConnection) -> Result { let paste = pastes::table.find(id).get_result::(conn)?; Ok(paste) -} \ No newline at end of file +} diff --git a/src/core/paste/service.rs b/src/core/paste/service.rs index 3a6563d..3f17aec 100644 --- a/src/core/paste/service.rs +++ b/src/core/paste/service.rs @@ -11,4 +11,4 @@ pub fn create_paste(paste: &mut Paste, conn: &PgConnection) -> Result { pub fn fetch_paste(id: String, conn: &PgConnection) -> Result { postgres::fetch_paste(id, conn) -} \ No newline at end of file +} diff --git a/src/core/users/entity.rs b/src/core/users/entity.rs index ff9ba1d..2b5c3f3 100644 --- a/src/core/users/entity.rs +++ b/src/core/users/entity.rs @@ -7,4 +7,4 @@ pub struct User { pub username: Option, pub password: Option, pub activated: Option, -} \ No newline at end of file +} diff --git a/src/core/users/mod.rs b/src/core/users/mod.rs index b2a1bcb..b06ddb4 100644 --- a/src/core/users/mod.rs +++ b/src/core/users/mod.rs @@ -1,3 +1,3 @@ -pub mod postgres; pub mod entity; -pub mod service; \ No newline at end of file +pub mod postgres; +pub mod service; diff --git a/src/core/users/postgres.rs b/src/core/users/postgres.rs index ac1e201..e63bab6 100644 --- a/src/core/users/postgres.rs +++ b/src/core/users/postgres.rs @@ -1,6 +1,6 @@ -use diesel::prelude::*; -use diesel::pg::PgConnection; use anyhow::Result; +use diesel::pg::PgConnection; +use diesel::prelude::*; use crate::core::users::entity::User; use crate::schema::users; @@ -16,4 +16,4 @@ pub fn create_user(user: &User, conn: &PgConnection) -> Result { pub fn find_user(id: String, conn: &PgConnection) -> Result { let user = users::table.find(id).get_result::(conn)?; Ok(user) -} \ No newline at end of file +} diff --git a/src/core/users/service.rs b/src/core/users/service.rs index c71703a..5fa136c 100644 --- a/src/core/users/service.rs +++ b/src/core/users/service.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use bcrypt::{DEFAULT_COST, hash}; +use bcrypt::{hash, DEFAULT_COST}; use diesel::pg::PgConnection; use diesel::result::Error; @@ -16,21 +16,19 @@ pub fn create_user(user: &mut User, conn: &PgConnection) -> Result { pub fn create_or_fetch_user(id: String, conn: &PgConnection) -> Result { let user = match postgres::find_user(id.clone(), conn) { Ok(user) => user, - Err(err) => { - match err.downcast_ref::() { - Some(Error::NotFound) => { - let new_user = User { - id: id.clone(), - username: None, - password: None, - activated: Some(false), - }; - postgres::create_user(&new_user, conn)?; - new_user - } - _ => return Err(err) + Err(err) => match err.downcast_ref::() { + Some(Error::NotFound) => { + let new_user = User { + id: id.clone(), + username: None, + password: None, + activated: Some(false), + }; + postgres::create_user(&new_user, conn)?; + new_user } - } + _ => return Err(err), + }, }; Ok(user) -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 220a5a8..f197a03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,12 +16,12 @@ extern crate slog; pub mod api; pub mod core; -pub mod utils; pub mod schema; +pub mod utils; -use slog_term; -use slog_async; use slog::{Drain, Logger}; +use slog_async; +use slog_term; embed_migrations!("migrations"); @@ -32,7 +32,6 @@ fn run_migrations(logger: &Logger) { } } - fn main() { dotenv::dotenv().ok(); @@ -48,7 +47,5 @@ fn main() { rocket = api::routes::fuel(rocket); rocket = api::catchers::fuel(rocket); - rocket.manage(utils::db::pool()) - .manage(logger) - .launch(); + rocket.manage(utils::db::pool()).manage(logger).launch(); } diff --git a/src/schema.rs b/src/schema.rs index 33c788f..74ca95f 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -18,7 +18,4 @@ table! { joinable!(pastes -> users (belongs_to)); -allow_tables_to_appear_in_same_query!( - pastes, - users, -); +allow_tables_to_appear_in_same_query!(pastes, users,); diff --git a/src/utils/db.rs b/src/utils/db.rs index 2ec6a54..360477f 100644 --- a/src/utils/db.rs +++ b/src/utils/db.rs @@ -1,6 +1,6 @@ use diesel::pg::PgConnection; -use diesel::{r2d2, Connection}; use diesel::r2d2::ConnectionManager; +use diesel::{r2d2, Connection}; use std::env; pub type Pool = r2d2::Pool>; @@ -16,4 +16,4 @@ fn database_url() -> String { pub fn pg_connection() -> PgConnection { PgConnection::establish(database_url().as_str()).unwrap() -} \ No newline at end of file +} diff --git a/src/utils/errors.rs b/src/utils/errors.rs index ae287e7..e546037 100644 --- a/src/utils/errors.rs +++ b/src/utils/errors.rs @@ -1,8 +1,13 @@ -use rocket::{http::{Status, ContentType}, response::Responder, Request, response, Response, Outcome, State}; +use rocket::{ + http::{ContentType, Status}, + response, + response::Responder, + Outcome, Request, Response, State, +}; -use std::io::Cursor; -use slog::Logger; use slog; +use slog::Logger; +use std::io::Cursor; #[derive(Debug, Serialize, Clone)] pub enum ErrorCode { @@ -32,14 +37,12 @@ impl Error { Error { code, msg: "".to_string(), - }.set_msg() + } + .set_msg() } pub fn custom(code: ErrorCode, msg: String) -> Error { - Error { - code, - msg, - } + Error { code, msg } } fn get_status_code(&self) -> Status { @@ -56,14 +59,16 @@ impl Error { ErrorCode::NotAuthorized => Status::Forbidden, ErrorCode::CorruptResource => Status::InternalServerError, ErrorCode::LogicalConflict => Status::BadRequest, - ErrorCode::Unknown => Status::InternalServerError + ErrorCode::Unknown => Status::InternalServerError, } } fn set_msg(mut self) -> Self { self.msg = match self.code.clone() { ErrorCode::InvalidCredentials => "invalid credentials were provided".to_string(), - ErrorCode::MultipleAuthToken => "multiple authorization tokens were provided".to_string(), + ErrorCode::MultipleAuthToken => { + "multiple authorization tokens were provided".to_string() + } ErrorCode::NoAuthToken => "no authorization token was found".to_string(), ErrorCode::AuthTokenCreationFailed => "authorization token creation failed".to_string(), ErrorCode::MalformedAuthToken => "authorization token was malformed".to_string(), @@ -71,10 +76,14 @@ impl Error { ErrorCode::ResourceAlreadyExists => "the given resource already exists".to_string(), ErrorCode::DatabaseError => "database error occured".to_string(), ErrorCode::InvalidData => "invalid data provided".to_string(), - ErrorCode::NotAuthorized => "you are not authorized to perform the requested operation".to_string(), + ErrorCode::NotAuthorized => { + "you are not authorized to perform the requested operation".to_string() + } ErrorCode::CorruptResource => "requested resource was corrupted".to_string(), - ErrorCode::LogicalConflict => "the request logically conflicts with the existing data".to_string(), - ErrorCode::Unknown => "unknown error occured".to_string() + ErrorCode::LogicalConflict => { + "the request logically conflicts with the existing data".to_string() + } + ErrorCode::Unknown => "unknown error occured".to_string(), }; self } @@ -94,4 +103,4 @@ impl<'r> Responder<'r> for Error { .sized_body(Cursor::new(serde_json::to_string(&self).unwrap())) .ok() } -} \ No newline at end of file +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 2a81a13..6e67a6c 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,3 +1,3 @@ -pub mod errors; pub mod db; -pub mod phonetic_key; \ No newline at end of file +pub mod errors; +pub mod phonetic_key; diff --git a/src/utils/phonetic_key.rs b/src/utils/phonetic_key.rs index aed53da..8f157d0 100644 --- a/src/utils/phonetic_key.rs +++ b/src/utils/phonetic_key.rs @@ -17,4 +17,4 @@ use uuid::Uuid; pub fn get_random_id() -> String { Uuid::new_v4().to_string() -} \ No newline at end of file +}