From eaaaa171835f3ebc8a45859757bc295bee442509 Mon Sep 17 00:00:00 2001 From: Gerard Zinn Date: Mon, 14 Oct 2024 00:32:49 +0200 Subject: [PATCH] Reducing String use when its not necessary --- crates/db/src/cache/championship.rs | 2 +- crates/db/src/cache/driver.rs | 2 +- crates/db/src/cache/user.rs | 2 +- crates/entities/src/championship.rs | 2 +- crates/entities/src/driver.rs | 2 +- crates/entities/src/user.rs | 8 ++--- .../src/repositories/championship.rs | 2 +- crates/intelli-core/src/repositories/user.rs | 2 +- crates/intelli-core/src/services/email.rs | 2 +- crates/password-hash/src/password_hash.rs | 2 +- crates/telemetry/src/firewall.rs | 30 +++++++++---------- crates/telemetry/src/service.rs | 4 +-- 12 files changed, 30 insertions(+), 30 deletions(-) diff --git a/crates/db/src/cache/championship.rs b/crates/db/src/cache/championship.rs index 47f855b..92c8993 100644 --- a/crates/db/src/cache/championship.rs +++ b/crates/db/src/cache/championship.rs @@ -9,7 +9,7 @@ use super::{EntityCache, CACHE_CAPACITY}; pub struct ChampionshipCache { inner: Cache>, races: Cache>>, - name_to_id: Cache, + name_to_id: Cache, i32>, user_championships: Cache>>, } diff --git a/crates/db/src/cache/driver.rs b/crates/db/src/cache/driver.rs index 0a20fbd..6189f70 100644 --- a/crates/db/src/cache/driver.rs +++ b/crates/db/src/cache/driver.rs @@ -5,7 +5,7 @@ use entities::SharedDriver; use super::CACHE_CAPACITY; pub struct DriverCache { - inner: Cache, + inner: Cache, SharedDriver>, } impl DriverCache { diff --git a/crates/db/src/cache/user.rs b/crates/db/src/cache/user.rs index 991ba56..6fe99de 100644 --- a/crates/db/src/cache/user.rs +++ b/crates/db/src/cache/user.rs @@ -8,7 +8,7 @@ use super::{EntityCache, CACHE_CAPACITY}; pub struct UserCache { inner: Cache, - email_to_id: Cache, + email_to_id: Cache, i32>, } impl UserCache { diff --git a/crates/entities/src/championship.rs b/crates/entities/src/championship.rs index 74786de..d0d0441 100644 --- a/crates/entities/src/championship.rs +++ b/crates/entities/src/championship.rs @@ -44,7 +44,7 @@ pub struct ChampionshipRelation { pub struct Championship { pub id: i32, pub port: i32, - pub name: String, + pub name: Box, #[serde(skip_serializing)] pub owner_id: i32, pub category: Category, diff --git a/crates/entities/src/driver.rs b/crates/entities/src/driver.rs index 15c00be..3791087 100644 --- a/crates/entities/src/driver.rs +++ b/crates/entities/src/driver.rs @@ -8,7 +8,7 @@ pub type SharedDriver = Arc; /// Represents a driver in the championship #[allow(unused)] pub struct Driver { - pub steam_name: String, + pub steam_name: Box, pub nationality: i16, pub user_id: Option, pub created_at: DateTime, diff --git a/crates/entities/src/user.rs b/crates/entities/src/user.rs index d8a6244..b4f0bd8 100644 --- a/crates/entities/src/user.rs +++ b/crates/entities/src/user.rs @@ -37,11 +37,11 @@ pub enum Role { #[derive(Debug, Serialize)] pub struct User { pub id: i32, - pub email: String, - pub username: String, + pub email: Box, + pub username: Box, #[serde(skip_serializing)] - pub password: Option, - pub avatar: String, + pub password: Option>, + pub avatar: Box, #[serde(skip_serializing)] pub provider: Provider, pub role: Role, diff --git a/crates/intelli-core/src/repositories/championship.rs b/crates/intelli-core/src/repositories/championship.rs index 3c6829b..5628d1e 100644 --- a/crates/intelli-core/src/repositories/championship.rs +++ b/crates/intelli-core/src/repositories/championship.rs @@ -197,7 +197,7 @@ impl ChampionshipRepository { } } - pub async fn drivers_linked(&self, id: i32) -> AppResult> { + pub async fn drivers_linked(&self, id: i32) -> AppResult>> { let stream = { let conn = self.db.pg.get().await?; diff --git a/crates/intelli-core/src/repositories/user.rs b/crates/intelli-core/src/repositories/user.rs index 3d69319..5b64806 100644 --- a/crates/intelli-core/src/repositories/user.rs +++ b/crates/intelli-core/src/repositories/user.rs @@ -269,7 +269,7 @@ impl UserRepository { /// # Returns /// Boolean indicating if the password is valid. #[inline] - pub async fn validate_password(&self, pwd: String, hash: String) -> AppResult { + pub async fn validate_password(&self, pwd: String, hash: Box) -> AppResult { self.password_hasher.verify_password(hash, pwd).await } } diff --git a/crates/intelli-core/src/services/email.rs b/crates/intelli-core/src/services/email.rs index 1e2d09b..9a7c261 100644 --- a/crates/intelli-core/src/services/email.rs +++ b/crates/intelli-core/src/services/email.rs @@ -58,7 +58,7 @@ impl EmailService { Address::from_str(&dotenvy::var("EMAIL_FROM").unwrap()).unwrap(), )) .to(Mailbox::new( - Some(user.username.to_owned()), + Some(user.username.to_string()), Address::from_str(&user.email).unwrap(), )) .subject(subject) diff --git a/crates/password-hash/src/password_hash.rs b/crates/password-hash/src/password_hash.rs index 30af91b..8269c14 100644 --- a/crates/password-hash/src/password_hash.rs +++ b/crates/password-hash/src/password_hash.rs @@ -56,7 +56,7 @@ impl PasswordHasher { .unwrap_or_else(|_| Err(CommonError::InternalServerError)?) } - pub async fn verify_password(&self, encoded: String, password: String) -> AppResult { + pub async fn verify_password(&self, encoded: Box, password: String) -> AppResult { let _permit = self.semaphore.acquire().await.unwrap(); tokio::task::spawn_blocking(move || { diff --git a/crates/telemetry/src/firewall.rs b/crates/telemetry/src/firewall.rs index a85e3b7..ad7f29c 100644 --- a/crates/telemetry/src/firewall.rs +++ b/crates/telemetry/src/firewall.rs @@ -1,7 +1,7 @@ use ahash::AHashMap; use error::{AppResult, FirewallError}; use regex::Regex; -use std::{str, sync::Arc}; +use std::{net::IpAddr, str, sync::Arc}; use tokio::process::Command; use tokio::sync::RwLock; use tracing::{error, warn}; @@ -9,8 +9,8 @@ use tracing::{error, warn}; /// Represents a single firewall rule. struct FirewallRule { port: u16, - handle: String, - ip_address: Option, + handle: Box, + ip_address: Option, } impl FirewallRule { @@ -19,7 +19,7 @@ impl FirewallRule { /// # Arguments /// - `port`: The port number for the rule. /// - `handle`: A unique identifier for the rule. - pub fn new(port: u16, handle: String) -> Self { + pub fn new(port: u16, handle: Box) -> Self { FirewallRule { port, handle, @@ -71,7 +71,7 @@ impl FirewallService { &port.to_string(), "accept", ]) - .await?; + .await?; let ruleset = Self::ruleset().await?; let handle = @@ -91,7 +91,7 @@ impl FirewallService { /// /// # Returns /// Result indicating success or failure. - pub async fn restrict_to_ip(&self, id: i32, ip_address: String) -> AppResult<()> { + pub async fn restrict_to_ip(&self, id: i32, ip_address: IpAddr) -> AppResult<()> { if cfg!(not(target_os = "linux")) { warn!("Firewall not supported on this platform"); return Ok(()); @@ -109,7 +109,7 @@ impl FirewallService { "handle", &rule.handle, ]) - .await?; + .await?; Self::nft_command(&[ "add", @@ -119,13 +119,13 @@ impl FirewallService { "allow", "ip", "saddr", - &ip_address, + &ip_address.to_string(), "udp", "dport", &rule.port.to_string(), "accept", ]) - .await?; + .await?; let ruleset = Self::ruleset().await?; let new_handle = Self::extract_handle_from_ruleset( @@ -168,7 +168,7 @@ impl FirewallService { "handle", &rule.handle, ]) - .await?; + .await?; rules.remove(&id); Ok(()) @@ -214,9 +214,9 @@ impl FirewallService { /// Retrieves the current firewall ruleset. /// /// # Returns - /// String representation of the current ruleset or an error. + /// Box representation of the current ruleset or an error. #[inline] - async fn ruleset() -> AppResult { + async fn ruleset() -> AppResult> { let output = Command::new("nft") .args(["-a", "list", "ruleset"]) .output() @@ -227,7 +227,7 @@ impl FirewallService { Err(FirewallError::ExecutionError)? } - let ruleset = String::from_utf8(output.stdout).unwrap_or_default(); + let ruleset = Box::from(str::from_utf8(&output.stdout).unwrap_or_default()); Ok(ruleset) } @@ -263,13 +263,13 @@ impl FirewallService { /// # Returns /// The extracted handle as a string or an error. #[inline] - fn extract_handle_from_ruleset(ruleset: &str, search_pattern: &str) -> AppResult { + fn extract_handle_from_ruleset(ruleset: &str, search_pattern: &str) -> AppResult> { let pattern = format!(r"{}\s+#\s+handle\s+(\d+)", regex::escape(search_pattern)); let re = Regex::new(&pattern).map_err(|_| FirewallError::ParseError)?; if let Some(caps) = re.captures(ruleset) { if let Some(handle) = caps.get(1) { - return Ok(handle.as_str().to_string()); + return Ok(Box::from(handle.as_str())); } } diff --git a/crates/telemetry/src/service.rs b/crates/telemetry/src/service.rs index 644ed4f..e93e0d4 100644 --- a/crates/telemetry/src/service.rs +++ b/crates/telemetry/src/service.rs @@ -216,7 +216,7 @@ impl F1Service { if self .f1_state .firewall - .restrict_to_ip(self.championship_id, address.ip().to_string()) + .restrict_to_ip(self.championship_id, address.ip()) .await .is_err() { @@ -474,7 +474,7 @@ impl F1Service { } if drivers - .binary_search_by(|probe| probe.as_str().cmp(steam_name)) + .binary_search_by(|probe| probe.as_ref().cmp(steam_name)) .is_err() { self.f1_state