From 279af37cdeb9ea9317ee854725a27fd7cadea5b2 Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Fri, 1 Dec 2023 01:04:03 +0000 Subject: [PATCH] chore: cargo fmt and fix clippy warnings --- src/controller.rs | 6 +++- src/discord.rs | 6 +++- src/discord/relay.rs | 11 ++++-- src/statistics/wrapped.rs | 75 +++++++++++++++++++++------------------ src/web.rs | 3 +- 5 files changed, 61 insertions(+), 40 deletions(-) diff --git a/src/controller.rs b/src/controller.rs index a07c256..4e6df70 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -299,7 +299,11 @@ impl Handler for Controller { #[async_trait] impl Handler for Controller { - async fn handle(&mut self, message: OutgoingCommand, _ctx: &mut Context) -> ::Result { + async fn handle( + &mut self, + message: OutgoingCommand, + _ctx: &mut Context, + ) -> ::Result { println!( "[{}] <@{}> /{}", message.channel, message.sender, message.command diff --git a/src/discord.rs b/src/discord.rs index e285e0d..aa1ed0e 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -251,7 +251,11 @@ impl DiscordHandler { self.relay.connect(channel, ctx, message).await } ["relay", "disconnect"] if admin => self.relay.disconnect(ctx, message).await, - ["relay", "command", channel, command @ ..] if admin => self.relay.send_relay_command(ctx, message, channel, command).await, + ["relay", "command", channel, command @ ..] if admin => { + self.relay + .send_relay_command(ctx, message, channel, command) + .await + } ["ping", "add", ping, role] if admin => self.pings.add(ctx, message, ping, role).await, ["ping", "remove", ping] if admin => self.pings.remove(ctx, message, ping).await, ["ping", "allow", ping, role] if admin => { diff --git a/src/discord/relay.rs b/src/discord/relay.rs index fce956b..9267353 100644 --- a/src/discord/relay.rs +++ b/src/discord/relay.rs @@ -250,7 +250,13 @@ impl Handler { } } - pub async fn send_relay_command(&self, ctx: &SerenityContext, message: &SerenityMessage, channel: &str, command: &[&str]) -> CommandResult { + pub async fn send_relay_command( + &self, + ctx: &SerenityContext, + message: &SerenityMessage, + channel: &str, + command: &[&str], + ) -> CommandResult { let sender = self.sender_name(ctx, message).await; let roles = if let Ok(member) = message.member(&ctx).await { member.roles.iter().map(ToString::to_string).collect() @@ -258,7 +264,8 @@ impl Handler { Vec::new() }; - let success = self.controller + let success = self + .controller .send(OutgoingCommand { channel: channel.to_string(), command: command.join(" "), diff --git a/src/statistics/wrapped.rs b/src/statistics/wrapped.rs index a64630a..7db1b22 100644 --- a/src/statistics/wrapped.rs +++ b/src/statistics/wrapped.rs @@ -6,12 +6,8 @@ pub struct NucleoidWrapped { } impl NucleoidWrapped { - pub fn new( - clickhouse_pool: clickhouse_rs::Pool, - ) -> Self { - Self { - clickhouse_pool, - } + pub fn new(clickhouse_pool: clickhouse_rs::Pool) -> Self { + Self { clickhouse_pool } } async fn played_count(&self, player: &Uuid) -> Result { @@ -28,14 +24,17 @@ impl NucleoidWrapped { // safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql player_id = player )).fetch_all().await?; - if let Some(row) = results.rows().nth(0) { + if let Some(row) = results.rows().next() { Ok(row.get("total")?) } else { Ok(0) } } - async fn top_games(&self, player: &Uuid) -> Result, clickhouse_rs::errors::Error> { + async fn top_games( + &self, + player: &Uuid, + ) -> Result, clickhouse_rs::errors::Error> { let mut ch_handle = self.clickhouse_pool.get_handle().await?; let results = ch_handle.query(format!( r#" @@ -57,10 +56,7 @@ impl NucleoidWrapped { for row in results.rows() { let namespace: String = row.get("namespace")?; let total = row.get("total")?; - top_games.push(PerGameStat { - namespace, - total, - }); + top_games.push(PerGameStat { namespace, total }); } Ok(top_games) @@ -80,14 +76,17 @@ impl NucleoidWrapped { // safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql player_id = player )).fetch_all().await?; - if let Some(row) = results.rows().nth(0) { + if let Some(row) = results.rows().next() { Ok(row.get("total")?) } else { Ok(0) } } - async fn days_played_games(&self, player: &Uuid) -> Result, clickhouse_rs::errors::Error> { + async fn days_played_games( + &self, + player: &Uuid, + ) -> Result, clickhouse_rs::errors::Error> { let mut ch_handle = self.clickhouse_pool.get_handle().await?; let results = ch_handle.query(format!( r#" @@ -109,10 +108,7 @@ impl NucleoidWrapped { for row in results.rows() { let namespace: String = row.get("namespace")?; let total = row.get("total")?; - top_games.push(PerGameStat { - namespace, - total, - }); + top_games.push(PerGameStat { namespace, total }); } Ok(top_games) @@ -120,8 +116,9 @@ impl NucleoidWrapped { async fn most_players(&self, player: &Uuid) -> Result { let mut ch_handle = self.clickhouse_pool.get_handle().await?; - let results = ch_handle.query(format!( - r#" + let results = ch_handle + .query(format!( + r#" SELECT MAX(total) as total FROM @@ -139,20 +136,26 @@ impl NucleoidWrapped { INNER JOIN player_statistics ON player_statistics.game_id = games.game_id GROUP BY game_id) "#, - // safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql - player_id = player - )).fetch_all().await?; - if let Some(row) = results.rows().nth(0) { + // safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql + player_id = player + )) + .fetch_all() + .await?; + if let Some(row) = results.rows().next() { Ok(row.get("total")?) } else { Ok(0) } } - async fn most_players_games(&self, player: &Uuid) -> Result, clickhouse_rs::errors::Error> { + async fn most_players_games( + &self, + player: &Uuid, + ) -> Result, clickhouse_rs::errors::Error> { let mut ch_handle = self.clickhouse_pool.get_handle().await?; - let results = ch_handle.query(format!( - r#" + let results = ch_handle + .query(format!( + r#" SELECT MAX(total) as total, namespace @@ -175,25 +178,27 @@ impl NucleoidWrapped { GROUP BY namespace ORDER BY total DESC "#, - // safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql - player_id = player - )).fetch_all().await?; + // safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql + player_id = player + )) + .fetch_all() + .await?; let mut top_games = Vec::with_capacity(results.row_count()); for row in results.rows() { let namespace: String = row.get("namespace")?; let total = row.get("total")?; - top_games.push(PerGameStat { - namespace, - total, - }); + top_games.push(PerGameStat { namespace, total }); } Ok(top_games) } - pub async fn build_wrapped(&self, player: &Uuid) -> Result { + pub async fn build_wrapped( + &self, + player: &Uuid, + ) -> Result { let played_count = self.played_count(player).await?; let top_games = self.top_games(player).await?; let days_played = self.days_played(player).await?; diff --git a/src/web.rs b/src/web.rs index 211dcfe..c3b2532 100644 --- a/src/web.rs +++ b/src/web.rs @@ -268,7 +268,8 @@ async fn get_player_username(mojang_client: Address, id: Uuid) async fn nucleoid_wrapped(controller: Address, player_id: Uuid) -> ApiResult { let statistics = get_statistics_controller(controller).await?; - let res = statistics.send(WrappedData(player_id)) + let res = statistics + .send(WrappedData(player_id)) .await .expect("controller disconnected"); handle_result(res)