Skip to content

Commit

Permalink
Impl AsRef<str>
Browse files Browse the repository at this point in the history
  • Loading branch information
GPeaky committed Oct 14, 2024
1 parent eaaaa17 commit daaa493
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 23 deletions.
6 changes: 4 additions & 2 deletions crates/db/src/cache/championship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ impl ChampionshipCache {
self.user_championships.get(user_id)
}

pub fn get_by_name(&self, name: &str) -> Option<Arc<Championship>> {
self.name_to_id.get(name).and_then(|id| self.get(&id))
pub fn get_by_name(&self, name: impl AsRef<str>) -> Option<Arc<Championship>> {
self.name_to_id
.get(name.as_ref())
.and_then(|id| self.get(&id))
}

pub fn set_user_championships(&self, user_id: i32, championships: Vec<Arc<Championship>>) {
Expand Down
8 changes: 4 additions & 4 deletions crates/db/src/cache/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ impl DriverCache {
}
}

pub fn get(&self, steam_name: &str) -> Option<SharedDriver> {
self.inner.get(steam_name)
pub fn get(&self, steam_name: impl AsRef<str>) -> Option<SharedDriver> {
self.inner.get(steam_name.as_ref())
}

pub fn set(&self, entity: SharedDriver) {
self.inner.insert(entity.steam_name.clone(), entity)
}

pub fn delete(&self, steam_name: &str) {
self.inner.remove(steam_name);
pub fn delete(&self, steam_name: impl AsRef<str>) {
self.inner.remove(steam_name.as_ref());
}
}
4 changes: 2 additions & 2 deletions crates/db/src/cache/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl UserCache {
}

/// Retrieves a user by their email address.
pub fn get_by_email(&self, email: &str) -> Option<SharedUser> {
let id = self.email_to_id.get(email)?;
pub fn get_by_email(&self, email: impl AsRef<str>) -> Option<SharedUser> {
let id = self.email_to_id.get(email.as_ref())?;
self.get(&id)
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/intelli-core/src/repositories/championship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ impl ChampionshipRepository {
///
/// # Returns
/// An Option containing the Championship if found.
pub async fn find_by_name(&self, name: &str) -> AppResult<Option<Arc<Championship>>> {
pub async fn find_by_name(&self, name: impl AsRef<str>) -> AppResult<Option<Arc<Championship>>> {
let name = name.as_ref();
if let Some(championship) = self.db.cache.championship.get_by_name(name) {
return Ok(Some(championship));
};
Expand Down
4 changes: 2 additions & 2 deletions crates/intelli-core/src/repositories/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ impl DiscordRepository {
///
/// # Returns
/// Discord user information wrapped in AppResult.
pub async fn account_info(&self, code: &str) -> AppResult<DiscordUserInfo> {
pub async fn account_info(&self, code: impl AsRef<str>) -> AppResult<DiscordUserInfo> {
let discord_exchange = DiscordExchangeRequest {
client_id: self.client_id,
client_secret: self.client_secret,
grant_type: "authorization_code",
code,
code: code.as_ref(),
redirect_uri: self.redirect_uri,
};

Expand Down
3 changes: 2 additions & 1 deletion crates/intelli-core/src/repositories/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ impl DriverRepository {
DriverRepository { db }
}

pub async fn find(&self, steam_name: &str) -> AppResult<Option<Arc<Driver>>> {
pub async fn find(&self, steam_name: impl AsRef<str>) -> AppResult<Option<Arc<Driver>>> {
let steam_name = steam_name.as_ref();
if let Some(driver) = self.db.cache.driver.get(steam_name) {
return Ok(Some(driver));
}
Expand Down
5 changes: 3 additions & 2 deletions crates/intelli-core/src/repositories/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ impl UserRepository {
///
/// # Returns
/// An Option containing the user if found.
pub async fn find_by_email(&self, email: &str) -> AppResult<Option<SharedUser>> {
pub async fn find_by_email(&self, email: impl AsRef<str>) -> AppResult<Option<SharedUser>> {
let email = email.as_ref();
if let Some(user) = self.db.cache.user.get_by_email(email) {
return Ok(Some(user));
};
Expand Down Expand Up @@ -185,7 +186,7 @@ impl UserRepository {
/// # Returns
/// Boolean indicating if the user exists.
#[inline]
pub async fn user_exists(&self, email: &str) -> AppResult<bool> {
pub async fn user_exists(&self, email: impl AsRef<str>) -> AppResult<bool> {
Ok(self.find_by_email(email).await?.is_some())
}

Expand Down
9 changes: 7 additions & 2 deletions crates/intelli-core/src/services/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ impl EmailService {
///
/// # Returns
/// `AppResult<()>`: Ok if successfully queued, Err otherwise.
pub async fn send_mail<T>(&self, user: SharedUser, subject: &str, body: T) -> AppResult<()>
pub async fn send_mail<T>(
&self,
user: SharedUser,
subject: impl AsRef<str>,
body: T,
) -> AppResult<()>
where
T: TemplateSimple,
{
Expand All @@ -61,7 +66,7 @@ impl EmailService {
Some(user.username.to_string()),
Address::from_str(&user.email).unwrap(),
))
.subject(subject)
.subject(subject.as_ref())
.header(ContentType::TEXT_HTML)
.body(body.render_once()?)
.expect("Message builder error");
Expand Down
13 changes: 8 additions & 5 deletions crates/password-hash/src/password_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ mod tests {

let hashed = hasher.hash_password(password.clone()).await.unwrap();
assert!(hasher
.verify_password(hashed.clone(), password.clone())
.verify_password(hashed.clone().into(), password.clone())
.await
.unwrap());

assert!(!hasher
.verify_password(hashed, "wrong_password".to_string())
.verify_password(hashed.into(), "wrong_password".to_string())
.await
.unwrap());
}
Expand Down Expand Up @@ -176,7 +176,10 @@ mod tests {
async fn invalid_encoded_password() {
let hasher = Arc::new(PasswordHasher::new(1));
let result = hasher
.verify_password("invalid_encoded_string".to_string(), "password".to_string())
.verify_password(
"invalid_encoded_string".to_string().into(),
"password".to_string(),
)
.await;
assert!(matches!(
result,
Expand All @@ -195,11 +198,11 @@ mod tests {
let strong_hash = hasher.hash_password(strong_password.clone()).await.unwrap();

assert!(hasher
.verify_password(weak_hash.clone(), weak_password)
.verify_password(weak_hash.clone().into(), weak_password)
.await
.unwrap());
assert!(hasher
.verify_password(strong_hash.clone(), strong_password)
.verify_password(strong_hash.clone().into(), strong_password)
.await
.unwrap());

Expand Down
4 changes: 2 additions & 2 deletions crates/token_manager/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ impl Token {
}

#[inline]
pub fn from_base64(str: &str) -> AppResult<Token> {
pub fn from_base64(str: impl AsRef<str>) -> AppResult<Token> {
let mut token = [0u8; 16];

match URL_SAFE_NO_PAD.decode(str.as_bytes(), Out::from_slice(&mut token)) {
match URL_SAFE_NO_PAD.decode(str.as_ref().as_bytes(), Out::from_slice(&mut token)) {
Ok(_) => Ok(Self(token)),
_ => Err(TokenError::InvalidToken)?,
}
Expand Down

0 comments on commit daaa493

Please sign in to comment.