Skip to content

Commit

Permalink
feat: use serde for de/serializing GuildId (#206)
Browse files Browse the repository at this point in the history
* feat: use serde for de/serializing GuildId

* fix: update tests to new types

* chore: rustfmt changes

* Add test for TournamentWatcherChannelListProvider impl for ChombotConfig
  • Loading branch information
seqre authored Nov 2, 2023
1 parent c119696 commit 1f8c1f5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
72 changes: 51 additions & 21 deletions chombot/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,9 @@ use std::path::PathBuf;
use async_trait::async_trait;
use chombot_common::tournaments_watcher::notifier::TournamentWatcherChannelListProvider;
use log::info;
use poise::serenity_prelude::ChannelId;
use poise::serenity_prelude::{ChannelId, GuildId};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct GuildId(String);

impl GuildId {
#[must_use]
pub fn new(value: u64) -> Self {
Self(value.to_string())
}
}

#[derive(Clone, Default, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Config {
/// Guild-specific configs
Expand All @@ -32,18 +21,19 @@ pub struct Config {
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Default)]
pub struct GuildConfig {
/// Tournaments watcher channel ID
pub tournaments_watcher_channel_id: Option<u64>,
pub tournaments_watcher_channel_id: Option<ChannelId>,
}

#[async_trait]
impl TournamentWatcherChannelListProvider for ChombotConfig {
type TournamentWatcherChannelList = Vec<ChannelId>;

async fn tournament_watcher_channels(&self) -> Self::TournamentWatcherChannelList {
let channel_ids =
self.config.guilds.iter().filter_map(|(_, config)| {
config.tournaments_watcher_channel_id.map(ChannelId::from)
});
let channel_ids = self
.config
.guilds
.iter()
.filter_map(|(_, config)| config.tournaments_watcher_channel_id);

channel_ids.collect()
}
Expand Down Expand Up @@ -129,6 +119,8 @@ impl<'a> DerefMut for ConfigUpdateGuard<'a> {
mod tests {
use std::collections::HashMap;

use chombot_common::tournaments_watcher::notifier::TournamentWatcherChannelListProvider;
use poise::serenity_prelude::ChannelId;
use tempfile::NamedTempFile;

use crate::config::{ChombotConfig, Config, GuildConfig, GuildId};
Expand All @@ -141,15 +133,15 @@ mod tests {
let config = Config {
guilds: HashMap::from([
(
GuildId::new(69),
GuildId(69),
GuildConfig {
tournaments_watcher_channel_id: Some(2137),
tournaments_watcher_channel_id: Some(ChannelId(2137)),
},
),
(
GuildId::new(420),
GuildId(420),
GuildConfig {
tournaments_watcher_channel_id: Some(69),
tournaments_watcher_channel_id: Some(ChannelId(69)),
},
),
]),
Expand All @@ -167,4 +159,42 @@ mod tests {

path.close().unwrap();
}

#[test]
fn test_tournament_watcher_channel_list_provider_for_chombo_config() -> std::io::Result<()> {
let file = NamedTempFile::new().unwrap();
let path = file.into_temp_path();

let config = Config {
guilds: HashMap::from([
(
GuildId(69),
GuildConfig {
tournaments_watcher_channel_id: Some(ChannelId(2137)),
},
),
(
GuildId(420),
GuildConfig {
tournaments_watcher_channel_id: Some(ChannelId(69)),
},
),
]),
};

let channel_ids: Vec<ChannelId> = vec![ChannelId(69), ChannelId(2137)];

{
let chombot_config = ChombotConfig::new(path.to_path_buf(), config.clone());
let mut ids = tokio::runtime::Builder::new_current_thread()
.build()?
.block_on(async { chombot_config.tournament_watcher_channels().await });
ids.sort();
assert_eq!(ids, channel_ids);
}

path.close().unwrap();

Ok(())
}
}
6 changes: 3 additions & 3 deletions chombot/src/tournament_watcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::anyhow;
use poise::serenity_prelude::ChannelId;
use poise::serenity_prelude::{ChannelId, GuildId};

use crate::{config, PoiseContext};

Expand All @@ -16,9 +16,9 @@ pub async fn tournament_watcher(
config
.config_mut()
.guilds
.entry(config::GuildId::new(guild.0))
.entry(guild)
.or_default()
.tournaments_watcher_channel_id = channel.map(|x| x.0);
.tournaments_watcher_channel_id = channel;
}

let reply_content = channel.as_ref().map_or_else(
Expand Down

0 comments on commit 1f8c1f5

Please sign in to comment.