From 36ca95e2e382a6fe6ed2fb2c9528c5c3148b7f8b Mon Sep 17 00:00:00 2001 From: supercmmetry Date: Thu, 29 Jul 2021 11:39:42 +0530 Subject: [PATCH] chore: fix warnings and cargo fmt Signed-off-by: supercmmetry --- src/api/routes/paste.rs | 61 ++++++++++++++++++++++---------------- src/api/routes/user.rs | 6 +--- src/core/paste/entity.rs | 2 +- src/core/paste/postgres.rs | 4 ++- src/core/users/entity.rs | 2 +- src/core/users/service.rs | 2 +- src/main.rs | 4 +-- src/utils/db.rs | 5 +++- src/utils/domain.rs | 7 +++-- src/utils/errors.rs | 2 +- src/utils/mod.rs | 2 +- 11 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/api/routes/paste.rs b/src/api/routes/paste.rs index eb8b61a..bbe8aef 100644 --- a/src/api/routes/paste.rs +++ b/src/api/routes/paste.rs @@ -1,18 +1,28 @@ use std::ops::DerefMut; use diesel::result::Error; -use rocket::{http::{Cookies, Status}, response::status, Rocket}; use rocket::response::status::Custom; +use rocket::{ + http::{Cookies, Status}, + response::status, + Rocket, +}; use rocket_contrib::json::Json; use serde_json::Value; -use crate::{api::catchers::{forbidden, internal_server_error, not_found, unprocessable_entity}, utils::domain::get_domain}; use crate::api::guards::db::DbConn; -use crate::core::paste::{entity::Paste, service::{create_paste, fetch_paste, update_paste}}; +use crate::core::paste::{ + entity::Paste, + service::{create_paste, fetch_paste, update_paste}, +}; +use crate::core::users::entity::User; use crate::core::users::service::{create_or_fetch_user, fetch_user}; use crate::utils::phonetic_key; use crate::utils::users::get_session_id; -use crate::core::users::entity::User; +use crate::{ + api::catchers::{forbidden, internal_server_error, not_found, unprocessable_entity}, + utils::domain::get_domain, +}; #[post("/", data = "")] fn create(mut paste: Json, conn: DbConn, mut ck: Cookies) -> Custom> { @@ -53,12 +63,10 @@ fn fetch(id: String, conn: DbConn, mut ck: Cookies) -> Custom> { let r_user = fetch_user(user_id, &conn); let user = match r_user { Ok(user) => user, - Err(e) => { - match e.downcast_ref::() { - Some(Error::NotFound) => User::new(), - _ => return internal_server_error(), - } - } + Err(e) => match e.downcast_ref::() { + Some(Error::NotFound) => User::new(), + _ => return internal_server_error(), + }, }; let paste = match fetch_paste(id, &conn) { @@ -73,17 +81,20 @@ fn fetch(id: String, conn: DbConn, mut ck: Cookies) -> Custom> { let belongs_to = paste.belongs_to.as_ref().unwrap(); - return if user.id == *belongs_to { - Custom(Status::Ok, Json(json!({ - "id": paste.id, - "belongs_to": *belongs_to, - "is_url": paste.is_url.unwrap(), - "content": paste.content, - "is_owner": true - }))) + if user.id == *belongs_to { + Custom( + Status::Ok, + Json(json!({ + "id": paste.id, + "belongs_to": *belongs_to, + "is_url": paste.is_url.unwrap(), + "content": paste.content, + "is_owner": true + })), + ) } else { Custom(Status::Ok, Json(json!(paste))) - }; + } } #[patch("/", data = "")] @@ -107,7 +118,7 @@ fn update(mut paste: Json, conn: DbConn, mut ck: Cookies) -> Custom paste.belongs_to, - Err(_) => return internal_server_error() + Err(_) => return internal_server_error(), }; if new_paste.belongs_to.is_some() { @@ -116,9 +127,9 @@ fn update(mut paste: Json, conn: DbConn, mut ck: Cookies) -> Custom status::Custom( Status::Created, Json(json!({ - "msg": "Successfully created paste", - "paste_id": new_paste.id - })), + "msg": "Successfully created paste", + "paste_id": new_paste.id + })), ), Err(_) => internal_server_error(), } @@ -149,6 +160,6 @@ fn anonymous(input: String, conn: DbConn) -> String { pub fn fuel(rocket: Rocket) -> Rocket { rocket - .mount("/api/paste", routes![create, fetch, update]) - .mount("/", routes![anonymous]) + .mount("/api/paste", routes![create, fetch, update]) + .mount("/", routes![anonymous]) } diff --git a/src/api/routes/user.rs b/src/api/routes/user.rs index b30cae5..60b02ef 100644 --- a/src/api/routes/user.rs +++ b/src/api/routes/user.rs @@ -1,11 +1,7 @@ use anyhow::Result; -use rocket::{ - http::Status, - response::status, - Rocket, -}; use rocket::http::Cookies; use rocket::response::status::Custom; +use rocket::{http::Status, response::status, Rocket}; use rocket_contrib::json::Json; use serde_json::Value; diff --git a/src/core/paste/entity.rs b/src/core/paste/entity.rs index 28f4d75..abf5101 100644 --- a/src/core/paste/entity.rs +++ b/src/core/paste/entity.rs @@ -1,7 +1,7 @@ use crate::schema::pastes; -#[table_name = "pastes"] #[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)] +#[table_name = "pastes"] pub struct Paste { pub id: Option, pub belongs_to: Option, diff --git a/src/core/paste/postgres.rs b/src/core/paste/postgres.rs index 34df526..f9da1d6 100644 --- a/src/core/paste/postgres.rs +++ b/src/core/paste/postgres.rs @@ -16,7 +16,9 @@ pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result { pub fn update_paste(paste: &Paste, conn: &PgConnection) -> Result { use crate::schema::pastes::dsl::*; - let updated_user = diesel::update(pastes.filter(id.eq(paste.id.as_ref().unwrap()))).set(paste).get_result(conn)?; + let updated_user = diesel::update(pastes.filter(id.eq(paste.id.as_ref().unwrap()))) + .set(paste) + .get_result(conn)?; Ok(updated_user) } diff --git a/src/core/users/entity.rs b/src/core/users/entity.rs index 484ae38..cc8c3bc 100644 --- a/src/core/users/entity.rs +++ b/src/core/users/entity.rs @@ -1,7 +1,7 @@ use crate::schema::users; +#[derive(Default, AsChangeset, Serialize, Deserialize, Queryable, Insertable)] #[table_name = "users"] -#[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)] pub struct User { pub id: String, pub username: Option, diff --git a/src/core/users/service.rs b/src/core/users/service.rs index 2f593b2..469d9c2 100644 --- a/src/core/users/service.rs +++ b/src/core/users/service.rs @@ -19,7 +19,7 @@ pub fn create_or_fetch_user(id: String, conn: &PgConnection) -> Result { Err(err) => match err.downcast_ref::() { Some(Error::NotFound) => { let new_user = User { - id: id.clone(), + id, username: None, password: None, activated: Some(false), diff --git a/src/main.rs b/src/main.rs index 326151a..2985e15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,8 +15,6 @@ extern crate serde_json; extern crate slog; use slog::{Drain, Logger}; -use slog_async; -use slog_term; pub mod api; pub mod core; @@ -40,7 +38,7 @@ fn main() { let drain = slog_async::Async::new(drain).build().fuse(); let logger = slog::Logger::root(drain, o!()); - // run_migrations(&logger); + run_migrations(&logger); let mut rocket = rocket::ignite(); diff --git a/src/utils/db.rs b/src/utils/db.rs index dc20059..19e698a 100644 --- a/src/utils/db.rs +++ b/src/utils/db.rs @@ -7,7 +7,10 @@ pub type Pool = r2d2::Pool>; pub fn pool() -> Pool { let manager = ConnectionManager::::new(database_url()); - let pool_size = env::var("KATBIN_POOL_SIZE").unwrap().parse::().expect("pool size"); + let pool_size = env::var("KATBIN_POOL_SIZE") + .unwrap() + .parse::() + .expect("pool size"); Pool::builder().max_size(pool_size).build(manager).unwrap() } diff --git a/src/utils/domain.rs b/src/utils/domain.rs index 7686d6e..385f568 100644 --- a/src/utils/domain.rs +++ b/src/utils/domain.rs @@ -1,5 +1,8 @@ use std::env; pub fn get_domain() -> String { - env::var("KATBIN_DOMAIN_NAME").unwrap().parse::().expect("domain name") -} \ No newline at end of file + env::var("KATBIN_DOMAIN_NAME") + .unwrap() + .parse::() + .expect("domain name") +} diff --git a/src/utils/errors.rs b/src/utils/errors.rs index e546037..be33794 100644 --- a/src/utils/errors.rs +++ b/src/utils/errors.rs @@ -64,7 +64,7 @@ impl Error { } fn set_msg(mut self) -> Self { - self.msg = match self.code.clone() { + self.msg = match self.code { ErrorCode::InvalidCredentials => "invalid credentials were provided".to_string(), ErrorCode::MultipleAuthToken => { "multiple authorization tokens were provided".to_string() diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 80e02cf..adfa174 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,5 +1,5 @@ pub mod db; +pub mod domain; pub mod errors; pub mod phonetic_key; pub mod users; -pub mod domain;