use std::sync::Arc;
use crate::{
bot::{channel, monitor::Monitor, monitor::watch},
config::Config,
stream::discord::DiscordSink,
};
use serenity::all::Http;
pub struct Wiring {
pub sink: DiscordSink<Http>,
pub board: Option<Arc<channel::Live>>,
pub wired: Option<Arc<watch::Wired<channel::Live>>>,
}
impl Wiring {
#[must_use]
pub fn new(config: &Config, monitor: &Arc<Monitor>) -> Self {
let board = config
.monitor_channel
.map(|channel| Arc::new(channel::Live::new(&config.token, channel)));
let wired = board.as_ref().map(|board| {
Arc::new(watch::Wired {
monitor: Arc::clone(monitor),
board: Arc::clone(board),
})
});
Self {
sink: DiscordSink::new(&config.token),
board,
wired,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_monitor_channel_is_written_through_one_board() {
let config = Config::from_toml("token = \"t\"\nguild_id = 1\nmonitor_channel = 7\n")
.expect("parsed");
let wiring = Wiring::new(&config, &Arc::new(Monitor::new()));
let board = wiring.board.as_ref().expect("a channel was named");
let wired = wiring.wired.as_ref().expect("a channel was named");
assert!(Arc::ptr_eq(board, &wired.board));
assert_eq!(
board.channel(),
serenity::all::ChannelId::new(7),
"the board is built for the channel dogs.toml names"
);
}
#[test]
fn a_config_naming_no_channel_builds_no_board_and_no_wiring_for_one() {
let config = Config::from_toml("token = \"t\"\nguild_id = 1\n").expect("parsed");
let wiring = Wiring::new(&config, &Arc::new(Monitor::new()));
assert!(wiring.board.is_none());
assert!(wiring.wired.is_none());
}
}