Skip to content

Commit 2af0966

Browse files
committed
chore: formatted the codebase
1 parent a722f6e commit 2af0966

11 files changed

+36
-20
lines changed

src/authentication/middleware.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use crate::{
22
session_state::TypedSession,
33
utils::{e500, see_other},
44
};
5-
use actix_web::body::MessageBody;
6-
use actix_web::dev::{ServiceRequest, ServiceResponse};
7-
use actix_web::error::InternalError;
8-
use actix_web::{FromRequest, HttpMessage};
5+
use actix_web::{
6+
body::MessageBody,
7+
dev::{ServiceRequest, ServiceResponse},
8+
error::InternalError,
9+
{FromRequest, HttpMessage},
10+
};
911
use actix_web_lab::middleware::Next;
1012
use std::ops::Deref;
1113
use uuid::Uuid;
@@ -27,6 +29,7 @@ impl Deref for UserId {
2729
}
2830
}
2931

32+
//
3033
pub async fn reject_anonymous_users(
3134
mut req: ServiceRequest,
3235
next: Next<impl MessageBody>,

src/authentication/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod middleware;
22
mod password;
3-
pub use middleware::reject_anonymous_users;
4-
pub use middleware::UserId;
3+
4+
pub use middleware::{reject_anonymous_users, UserId};
55
pub use password::{change_password, validate_credentials, AuthError, Credentials};

src/authentication/password.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use anyhow::Context;
2-
use argon2::password_hash::SaltString;
3-
use argon2::{Algorithm, Argon2, Params, PasswordHash, PasswordHasher, PasswordVerifier, Version};
2+
use argon2::{
3+
password_hash::SaltString, Algorithm, Argon2, Params, PasswordHash, PasswordHasher,
4+
PasswordVerifier, Version,
5+
};
46
use secrecy::{ExposeSecret, Secret};
57
use sqlx::PgPool;
68

@@ -24,6 +26,7 @@ async fn get_stored_credentials(
2426
username: &str,
2527
pool: &PgPool,
2628
) -> Result<Option<(uuid::Uuid, Secret<String>)>, anyhow::Error> {
29+
// Retrieve the user's id and password hash from the database.
2730
let row = sqlx::query!(
2831
r#"
2932
SELECT user_id, password_hash
@@ -52,6 +55,7 @@ pub async fn validate_credentials(
5255
.to_string(),
5356
);
5457

58+
// conditionally assign stored_user_id and stored_password_hash
5559
if let Some((stored_user_id, stored_password_hash)) =
5660
get_stored_credentials(&credentials.username, pool).await?
5761
{

src/domain/new_subscriber.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::domain::subscriber_email::SubscriberEmail;
2-
use crate::domain::subscriber_name::SubscriberName;
1+
use crate::domain::{subscriber_email::SubscriberEmail, subscriber_name::SubscriberName};
2+
33
pub struct NewSubscriber {
44
pub email: SubscriberEmail,
55
pub name: SubscriberName,

src/domain/subscriber_email.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ impl SubscriberEmail {
1212
}
1313
}
1414

15+
// Gain access to the inner String by implementing AsRef<str>.
1516
impl AsRef<str> for SubscriberEmail {
1617
fn as_ref(&self) -> &str {
1718
&self.0

src/domain/subscriber_name.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ impl SubscriberName {
1313
let is_empty_or_whitespace = name.trim().is_empty();
1414
let is_too_long = name.len() > 256;
1515
let forbidden_characters = ['/', '(', ')', '"', '<', '>', '\\', '{', '}'];
16+
// Check if the name contains any of the forbidden characters.
1617
let contains_forbidden_characters = name
1718
.chars()
1819
.any(|char| forbidden_characters.contains(&char));
20+
// If any of the above checks returns true, return an error.
1921
if is_empty_or_whitespace || is_too_long || contains_forbidden_characters {
2022
Err(format!(
2123
"`{}` is not a valid subscriber name. Subscriber name cannot be empty, more than 256 characters long, or contain the following characters: {:?}",

src/idempotency/key.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl TryFrom<String> for IdempotencyKey {
1111
if s.len() >= max_length {
1212
anyhow::bail!(
1313
"The idempotency key must be shorter
14-
than {max_length} characters"
14+
than {max_length} characters"
1515
);
1616
}
1717
Ok(Self(s))

src/idempotency/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
mod key;
22
mod persistence;
33
pub use key::IdempotencyKey;
4-
pub use persistence::get_saved_response;
5-
pub use persistence::save_response;
6-
pub use persistence::{try_processing, NextAction};
4+
pub use persistence::{get_saved_response, save_response, try_processing, NextAction};

src/idempotency/persistence.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use super::IdempotencyKey;
2-
use actix_web::body::to_bytes;
3-
use actix_web::http::StatusCode;
4-
use actix_web::HttpResponse;
5-
use sqlx::postgres::PgHasArrayType;
6-
use sqlx::PgPool;
7-
use sqlx::{Postgres, Transaction};
2+
use actix_web::{body::to_bytes, http::StatusCode, HttpResponse};
3+
use sqlx::{postgres::PgHasArrayType, PgPool, Postgres, Transaction};
84
use uuid::Uuid;
95

106
#[derive(Debug, sqlx::Type)]

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Purpose: Main library file for the application.
12
pub mod authentication;
23
pub mod configuration;
34
pub mod domain;

src/session_state.rs

+11
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,34 @@ use actix_web::dev::Payload;
33
use actix_web::{FromRequest, HttpRequest};
44
use std::future::{ready, Ready};
55
use uuid::Uuid;
6+
7+
// Purpose: A wrapper around `Session` that provides type-safe access to the
8+
// session data.
69
pub struct TypedSession(Session);
710

11+
// Associated functions for `TypedSession`.
812
impl TypedSession {
13+
// The key used to store the user ID in the session.
914
const USER_ID_KEY: &'static str = "user_id";
1015

16+
// Renews the session key, assigning existing session state to new key.
1117
pub fn renew(&self) {
1218
self.0.renew();
1319
}
1420

21+
// Inserts the user ID into the session.
22+
// Returns an error if it fails to serialize value to JSON.
1523
pub fn insert_user_id(&self, user_id: Uuid) -> Result<(), SessionInsertError> {
1624
self.0.insert(Self::USER_ID_KEY, user_id)
1725
}
1826

27+
// Gets the user ID from the session.
28+
// Returns an error if it fails to deserialize value from JSON.
1929
pub fn get_user_id(&self) -> Result<Option<Uuid>, SessionGetError> {
2030
self.0.get(Self::USER_ID_KEY)
2131
}
2232

33+
// Removes the user ID from the session.
2334
pub fn log_out(self) {
2435
self.0.purge()
2536
}

0 commit comments

Comments
 (0)