Skip to content

Commit 635e74a

Browse files
authored
Merge pull request #16 from QuantGeekDev/feature/rustfmt
chore: format code with updated rustfmt config
2 parents ce9a014 + 85c81f1 commit 635e74a

File tree

14 files changed

+104
-129
lines changed

14 files changed

+104
-129
lines changed

rustfmt.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
use_small_heuristics = "Max"
2+
merge_derives = false

src/auth/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub mod user;
1+
pub mod user;

src/catchers/mod.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,15 @@ pub struct ErrorResponse {
1111

1212
#[catch(401)]
1313
pub fn unauthorized(req: &Request) -> Json<ErrorResponse> {
14-
Json(ErrorResponse {
15-
status: 401,
16-
message: format!("Unauthorized access to {}", req.uri()),
17-
})
14+
Json(ErrorResponse { status: 401, message: format!("Unauthorized access to {}", req.uri()) })
1815
}
1916

2017
#[catch(404)]
2118
pub fn not_found(_req: &Request) -> Json<ErrorResponse> {
22-
Json(ErrorResponse {
23-
status: 404,
24-
message: "Page not found".to_string(),
25-
})
19+
Json(ErrorResponse { status: 404, message: "Page not found".to_string() })
2620
}
2721

2822
#[catch(500)]
2923
pub fn internal_server_error(_req: &Request) -> Json<ErrorResponse> {
30-
Json(ErrorResponse {
31-
status: 500,
32-
message: "Internal Server Error".to_string(),
33-
})
24+
Json(ErrorResponse { status: 500, message: "Internal Server Error".to_string() })
3425
}

src/db.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use diesel::prelude::*;
33
use std::env;
44

55
pub fn establish_connection() -> PgConnection {
6-
let database_url = env::var("DATABASE_URL")
7-
.expect("DATABASE_URL must be set");
6+
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
87
PgConnection::establish(&database_url)
98
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
109
}

src/main.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
extern crate dotenv;
22
#[macro_use]
33
extern crate rocket;
4-
mod routes;
5-
mod models;
64
mod auth;
75
mod catchers;
86
mod db;
7+
mod models;
8+
mod routes;
99
mod schema;
1010

1111
use crate::routes::auth::{login, register};
12-
use crate::routes::relationship::{invite_project_manager, list_developers, list_snacks, respond_to_invite};
12+
use crate::routes::relationship::{
13+
invite_project_manager, list_developers, list_snacks, respond_to_invite,
14+
};
1315
use crate::routes::snack::{create_snack, delete_snack, update_snack};
1416
use dotenv::dotenv;
1517
use rocket::*;
@@ -25,12 +27,28 @@ fn index() -> &'static str {
2527
|___| |_______||_______||______| |__| |__| |______| |_______| |___| "
2628
}
2729

28-
2930
#[launch]
3031
fn rocket() -> _ {
3132
dotenv().ok();
3233

33-
rocket::build().mount("/", routes![index, invite_project_manager,list_developers, respond_to_invite, create_snack, list_snacks, update_snack, delete_snack, register, login]).register("/", catchers![catchers::unauthorized, catchers::not_found,
34-
catchers::internal_server_error])
34+
rocket::build()
35+
.mount(
36+
"/",
37+
routes![
38+
index,
39+
invite_project_manager,
40+
list_developers,
41+
respond_to_invite,
42+
create_snack,
43+
list_snacks,
44+
update_snack,
45+
delete_snack,
46+
register,
47+
login
48+
],
49+
)
50+
.register(
51+
"/",
52+
catchers![catchers::unauthorized, catchers::not_found, catchers::internal_server_error],
53+
)
3554
}
36-

src/models/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
pub(crate) mod relationship;
12
pub mod snack;
23
pub(crate) mod user;
3-
pub(crate) mod relationship;

src/models/relationship.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
// src/models/relationship.rs
21
use crate::models::user::User;
2+
use crate::schema::dev_pm_relationships;
33
use chrono::NaiveDateTime;
4+
use diesel::pg::Pg;
45
use diesel::prelude::*;
56
use serde::{Deserialize, Serialize};
6-
use diesel::pg::Pg;
7-
use crate::schema::dev_pm_relationships;
8-
use crate::schema::dev_pm_relationships::dsl;
97

108
#[derive(Queryable, Selectable, Serialize, Identifiable, Associations, Debug)]
119
#[diesel(belongs_to(User, foreign_key = developer_id))]
@@ -37,37 +35,28 @@ pub struct RespondToInviteRequest {
3735
pub status: String,
3836
}
3937

40-
// Query builders helper methods
4138
impl DevPmRelationship {
4239
pub fn for_developer(user_id: i32) -> dev_pm_relationships::BoxedQuery<'static, Pg> {
4340
use crate::schema::dev_pm_relationships::dsl::*;
4441

45-
dev_pm_relationships
46-
.filter(developer_id.eq(user_id))
47-
.into_boxed()
42+
dev_pm_relationships.filter(developer_id.eq(user_id)).into_boxed()
4843
}
4944

5045
pub fn for_project_manager(user_id: i32) -> dev_pm_relationships::BoxedQuery<'static, Pg> {
5146
use crate::schema::dev_pm_relationships::dsl::*;
5247

53-
dev_pm_relationships
54-
.filter(project_manager_id.eq(user_id))
55-
.into_boxed()
48+
dev_pm_relationships.filter(project_manager_id.eq(user_id)).into_boxed()
5649
}
5750

5851
pub fn pending() -> dev_pm_relationships::BoxedQuery<'static, Pg> {
5952
use crate::schema::dev_pm_relationships::dsl::*;
6053

61-
dev_pm_relationships
62-
.filter(status.eq("pending"))
63-
.into_boxed()
54+
dev_pm_relationships.filter(status.eq("pending")).into_boxed()
6455
}
6556

6657
pub fn accepted() -> dev_pm_relationships::BoxedQuery<'static, Pg> {
6758
use crate::schema::dev_pm_relationships::dsl::*;
6859

69-
dev_pm_relationships
70-
.filter(status.eq("accepted"))
71-
.into_boxed()
60+
dev_pm_relationships.filter(status.eq("accepted")).into_boxed()
7261
}
73-
}
62+
}

src/models/snack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ impl CreateSnackRequest {
4949
user_id,
5050
}
5151
}
52-
}
52+
}

src/models/user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ pub struct NewUser {
1818
pub username: String,
1919
pub password_hash: String,
2020
pub role: String,
21-
}
21+
}

src/routes/auth.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ pub struct TokenResponse {
3030
#[post("/register", data = "<info>")]
3131
pub fn register(info: Json<RegisterInfo>) -> Result<Json<User>, Status> {
3232
let conn = &mut db::establish_connection();
33-
let hashed_password = hash(&info.password, DEFAULT_COST)
34-
.map_err(|_| Status::InternalServerError)?;
33+
let hashed_password =
34+
hash(&info.password, DEFAULT_COST).map_err(|_| Status::InternalServerError)?;
3535

3636
let user_role = match info.role.as_deref() {
3737
Some("developer") => "developer",
3838
Some("project_manager") => "project_manager",
39-
_ => "developer"
40-
}.to_string();
39+
_ => "developer",
40+
}
41+
.to_string();
4142

4243
let new_user = NewUser {
4344
username: info.username.clone(),
@@ -60,19 +61,12 @@ pub fn login(info: Json<LoginInfo>) -> Result<Json<TokenResponse>, Status> {
6061
.first::<User>(conn)
6162
.map_err(|_| Status::Unauthorized)?;
6263
if verify(&info.password, &user.password_hash).map_err(|_| Status::InternalServerError)? {
63-
let claims = Claims {
64-
sub: user.id,
65-
role: user.role.clone(),
66-
exp: 10000000000,
67-
};
68-
let token = encode(
69-
&Header::default(),
70-
&claims,
71-
&EncodingKey::from_secret("SECRET".as_ref()),
72-
)
73-
.map_err(|_| Status::InternalServerError)?;
64+
let claims = Claims { sub: user.id, role: user.role.clone(), exp: 10000000000 };
65+
let token =
66+
encode(&Header::default(), &claims, &EncodingKey::from_secret("SECRET".as_ref()))
67+
.map_err(|_| Status::InternalServerError)?;
7468
Ok(Json(TokenResponse { token }))
7569
} else {
7670
Err(Status::Unauthorized)
7771
}
78-
}
72+
}

0 commit comments

Comments
 (0)