From 1ff95de3bf317dc4c2937d9a7aaebfded6e4a755 Mon Sep 17 00:00:00 2001 From: seth Date: Sat, 30 Mar 2024 03:42:53 -0400 Subject: [PATCH] make sure to borrow in getters --- src/commands/general/say.rs | 1 - src/commands/general/stars.rs | 2 +- src/commands/moderation/set_welcome.rs | 2 +- src/config/bot.rs | 6 +++--- src/config/discord.rs | 14 +++++++------- src/config/mod.rs | 12 ++++++------ src/handlers/event/analyze_logs/issues.rs | 2 +- src/main.rs | 2 +- src/storage/mod.rs | 6 +++--- 9 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/commands/general/say.rs b/src/commands/general/say.rs index 1703fe3..9aa8098 100644 --- a/src/commands/general/say.rs +++ b/src/commands/general/say.rs @@ -23,7 +23,6 @@ pub async fn say( if let Some(channel_id) = ctx .data() .config - .clone() .discord_config() .channels() .log_channel_id() diff --git a/src/commands/general/stars.rs b/src/commands/general/stars.rs index 4858d87..b669dbe 100644 --- a/src/commands/general/stars.rs +++ b/src/commands/general/stars.rs @@ -13,7 +13,7 @@ pub async fn stars(ctx: Context<'_>) -> Result<()> { ctx.defer().await?; let count = if let Some(storage) = &ctx.data().storage { - if let Ok(count) = storage.get_launcher_stargazer_count().await { + if let Ok(count) = storage.launcher_stargazer_count().await { count } else { let count = api::github::get_prism_stargazers_count().await?; diff --git a/src/commands/moderation/set_welcome.rs b/src/commands/moderation/set_welcome.rs index a9438d2..901aecc 100644 --- a/src/commands/moderation/set_welcome.rs +++ b/src/commands/moderation/set_welcome.rs @@ -115,7 +115,7 @@ pub async fn set_welcome( ) -> Result<()> { trace!("Running set_welcome command!"); - let configured_channels = ctx.data().config.clone().discord_config().channels(); + let configured_channels = ctx.data().config.discord_config().channels(); let Some(channel_id) = configured_channels.welcome_channel_id() else { ctx.say("You don't have a welcome channel ID set, so I can't do anything :(") .await?; diff --git a/src/config/bot.rs b/src/config/bot.rs index 7eee8eb..ebad8fe 100644 --- a/src/config/bot.rs +++ b/src/config/bot.rs @@ -10,7 +10,7 @@ impl Config { Self { redis_url } } - pub fn new_from_env() -> Self { + pub fn from_env() -> Self { let redis_url = std::env::var("BOT_REDIS_URL").ok(); if let Some(url) = &redis_url { @@ -22,7 +22,7 @@ impl Config { Self::new(redis_url) } - pub fn redis_url(self) -> Option { - self.redis_url + pub fn redis_url(&self) -> Option<&str> { + self.redis_url.as_deref() } } diff --git a/src/config/discord.rs b/src/config/discord.rs index 837c860..8ef8082 100644 --- a/src/config/discord.rs +++ b/src/config/discord.rs @@ -46,12 +46,12 @@ impl RefractionChannels { .and_then(|env_var| ChannelId::from_str(&env_var).ok()) } - pub fn log_channel_id(self) -> Option { - self.log_channel_id + pub fn log_channel_id(&self) -> Option<&ChannelId> { + self.log_channel_id.as_ref() } - pub fn welcome_channel_id(self) -> Option { - self.welcome_channel_id + pub fn welcome_channel_id(&self) -> Option<&ChannelId> { + self.welcome_channel_id.as_ref() } } @@ -60,13 +60,13 @@ impl Config { Self { channels } } - pub fn new_from_env() -> Self { + pub fn from_env() -> Self { let channels = RefractionChannels::new_from_env(); Self::new(channels) } - pub fn channels(self) -> RefractionChannels { - self.channels + pub fn channels(&self) -> &RefractionChannels { + &self.channels } } diff --git a/src/config/mod.rs b/src/config/mod.rs index 416472b..568a7e2 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -16,17 +16,17 @@ impl Config { } pub fn new_from_env() -> Self { - let bot = bot::Config::new_from_env(); - let discord = discord::Config::new_from_env(); + let bot = bot::Config::from_env(); + let discord = discord::Config::from_env(); Self::new(bot, discord) } - pub fn bot_config(self) -> bot::Config { - self.bot + pub fn bot_config(&self) -> &bot::Config { + &self.bot } - pub fn discord_config(self) -> discord::Config { - self.discord + pub fn discord_config(&self) -> &discord::Config { + &self.discord } } diff --git a/src/handlers/event/analyze_logs/issues.rs b/src/handlers/event/analyze_logs/issues.rs index 72ee00a..c2254bc 100644 --- a/src/handlers/event/analyze_logs/issues.rs +++ b/src/handlers/event/analyze_logs/issues.rs @@ -195,7 +195,7 @@ async fn outdated_launcher(log: &str, data: &Data) -> Result { let version_from_log = captures[0].replace("Prism Launcher version: ", ""); let latest_version = if let Some(storage) = &data.storage { - if let Ok(version) = storage.get_launcher_version().await { + if let Ok(version) = storage.launcher_version().await { version } else { api::github::get_latest_prism_version().await? diff --git a/src/main.rs b/src/main.rs index 118b109..3e0e101 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,7 @@ async fn setup( ) -> Result { let config = Config::new_from_env(); - let storage = if let Some(url) = &config.clone().bot_config().redis_url() { + let storage = if let Some(url) = config.bot_config().redis_url() { Some(Storage::from_url(url)?) } else { None diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 6166276..e482b4f 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -25,7 +25,7 @@ impl Storage { Ok(Self::new(client)) } - pub fn has_connection(mut self) -> bool { + pub fn has_connection(&mut self) -> bool { self.client.check_connection() } @@ -60,7 +60,7 @@ impl Storage { Ok(()) } - pub async fn get_launcher_version(&self) -> Result { + pub async fn launcher_version(&self) -> Result { debug!("Fetching launcher version"); let mut con = self.client.get_multiplexed_async_connection().await?; @@ -79,7 +79,7 @@ impl Storage { Ok(()) } - pub async fn get_launcher_stargazer_count(&self) -> Result { + pub async fn launcher_stargazer_count(&self) -> Result { debug!("Fetching launcher stargazer count"); let mut con = self.client.get_multiplexed_async_connection().await?;