Skip to content

Commit

Permalink
Simplify generics for redis connection argument
Browse files Browse the repository at this point in the history
"ConnectionLike + AsyncCommands" is redundant, since ConnectionLike is
already a supertrait of AsyncCommands.
  • Loading branch information
Bastian Oppermann committed Aug 11, 2024
1 parent 83cd248 commit c23e7e0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/charts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod single_line_chart;
use std::collections::HashMap;

use chart::ChartType;
use redis::{aio::ConnectionLike, AsyncCommands};
use redis::AsyncCommands;
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand All @@ -31,7 +31,7 @@ static CHART_FIELDS: (&str, &str, &str, &str, &str, &str, &str) = (
/// Find all charts with the given IDs.
///
/// Using this function is more efficient than calling `find_by_id` multiple times.
pub async fn find_by_ids<C: ConnectionLike + AsyncCommands>(
pub async fn find_by_ids<C: AsyncCommands>(
con: &mut C,
ids: Vec<u64>,
) -> Result<HashMap<u64, Option<Chart>>, redis::RedisError> {
Expand Down Expand Up @@ -78,7 +78,7 @@ pub async fn find_by_ids<C: ConnectionLike + AsyncCommands>(
Ok(result)
}

pub async fn find_by_id<C: ConnectionLike + AsyncCommands>(
pub async fn find_by_id<C: AsyncCommands>(
con: &mut C,
id: u64,
) -> Result<Option<Chart>, redis::RedisError> {
Expand Down
6 changes: 3 additions & 3 deletions src/ratelimits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use redis::{aio::ConnectionLike, AsyncCommands};
use redis::AsyncCommands;

pub async fn is_ratelimited<C: ConnectionLike + AsyncCommands>(
pub async fn is_ratelimited<C: AsyncCommands>(
con: &mut C,
software_url: &str,
max_requests_per_ip: u16,
Expand Down Expand Up @@ -34,7 +34,7 @@ pub async fn is_ratelimited<C: ConnectionLike + AsyncCommands>(
return Ok(false);
}

async fn _is_ratelimited<C: ConnectionLike + AsyncCommands>(
async fn _is_ratelimited<C: AsyncCommands>(
con: &mut C,
identifier: &str,
software_url: &str,
Expand Down
14 changes: 6 additions & 8 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet};
extern crate redis;
use redis::{aio::ConnectionLike, AsyncCommands};
use redis::AsyncCommands;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -13,9 +13,7 @@ pub struct Service {
pub charts: Vec<u64>,
}

pub async fn find_all<C: ConnectionLike + AsyncCommands>(
con: &mut C,
) -> Result<Vec<Service>, redis::RedisError> {
pub async fn find_all<C: AsyncCommands>(con: &mut C) -> Result<Vec<Service>, redis::RedisError> {
let service_ids = find_all_service_ids(con).await?;
let mut services: Vec<_> = Vec::new();
for id in service_ids {
Expand All @@ -29,7 +27,7 @@ pub async fn find_all<C: ConnectionLike + AsyncCommands>(
Ok(services)
}

pub async fn find_by_software_url_and_name<C: ConnectionLike + AsyncCommands>(
pub async fn find_by_software_url_and_name<C: AsyncCommands>(
con: &mut C,
software_url: &str,
name: &str,
Expand All @@ -42,7 +40,7 @@ pub async fn find_by_software_url_and_name<C: ConnectionLike + AsyncCommands>(
find_by_id(con, id.unwrap()).await
}

pub async fn find_by_id<C: ConnectionLike + AsyncCommands>(
pub async fn find_by_id<C: AsyncCommands>(
con: &mut C,
id: u32,
) -> Result<Option<Service>, redis::RedisError> {
Expand All @@ -61,13 +59,13 @@ pub async fn find_by_id<C: ConnectionLike + AsyncCommands>(
}))
}

async fn find_all_service_ids<C: ConnectionLike + AsyncCommands>(
async fn find_all_service_ids<C: AsyncCommands>(
con: &mut C,
) -> Result<HashSet<u32>, redis::RedisError> {
con.smembers("plugins.ids").await
}

async fn _find_service_id_by_software_url_and_name<C: ConnectionLike + AsyncCommands>(
async fn _find_service_id_by_software_url_and_name<C: AsyncCommands>(
con: &mut C,
software_url: &str,
name: &str,
Expand Down
14 changes: 6 additions & 8 deletions src/software.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet};
extern crate redis;
use redis::{aio::ConnectionLike, AsyncCommands};
use redis::AsyncCommands;
use serde::{Deserialize, Serialize};

use crate::charts::chart::DefaultChartTemplate;
Expand All @@ -18,9 +18,7 @@ pub struct Software {
pub hide_in_plugin_list: bool,
}

pub async fn find_all<C: ConnectionLike + AsyncCommands>(
con: &mut C,
) -> Result<Vec<Software>, redis::RedisError> {
pub async fn find_all<C: AsyncCommands>(con: &mut C) -> Result<Vec<Software>, redis::RedisError> {
// TODO: Cache result since it hardly ever changes
let software_ids = find_all_software_ids(con).await?;
let mut software = Vec::new();
Expand All @@ -35,7 +33,7 @@ pub async fn find_all<C: ConnectionLike + AsyncCommands>(
Ok(software)
}

pub async fn find_by_url<C: ConnectionLike + AsyncCommands>(
pub async fn find_by_url<C: AsyncCommands>(
con: &mut C,
url: &str,
) -> Result<Option<Software>, redis::RedisError> {
Expand All @@ -47,7 +45,7 @@ pub async fn find_by_url<C: ConnectionLike + AsyncCommands>(
find_by_id(con, id.unwrap()).await
}

pub async fn find_by_id<C: ConnectionLike + AsyncCommands>(
pub async fn find_by_id<C: AsyncCommands>(
con: &mut C,
id: u16,
) -> Result<Option<Software>, redis::RedisError> {
Expand All @@ -73,13 +71,13 @@ pub async fn find_by_id<C: ConnectionLike + AsyncCommands>(
}))
}

async fn find_all_software_ids<C: ConnectionLike + AsyncCommands>(
async fn find_all_software_ids<C: AsyncCommands>(
con: &mut C,
) -> Result<HashSet<u16>, redis::RedisError> {
con.smembers("software.ids").await
}

async fn _find_software_id_by_url<C: ConnectionLike + AsyncCommands>(
async fn _find_software_id_by_url<C: AsyncCommands>(
con: &mut C,
url: &str,
) -> Result<Option<u16>, redis::RedisError> {
Expand Down

0 comments on commit c23e7e0

Please sign in to comment.