macro_rules! impl_event {
($name:ident; $first:expr, $second:ident) => {
impl Event for $name {
fn get_event_data(self) -> (String, String) {
($first.into(), self.$second.to_string())
}
}
}
}
macro_rules! impl_true_event {
($name:ident; $first:expr) => {
impl Event for $name {
fn get_event_data(self) -> (String, String) { ($first.into(), "true".to_string()) }
}
}
}
pub trait Event {
fn get_event_data(self) -> (String, String);
}
#[derive(Debug)]
pub struct Messages {
pub amount: usize,
}
#[derive(Debug)]
pub struct GuildLeave {}
#[derive(Debug)]
pub struct Error {
pub message: String,
}
#[derive(Debug)]
pub struct Disconnect {}
#[derive(Debug)]
pub struct VoiceChannelJoin {}
#[derive(Debug)]
pub struct GuildDetails {
pub guild: String,
pub user_count: usize,
pub channel_count: usize,
}
#[derive(Debug)]
pub struct Mention {}
#[derive(Debug)]
pub struct CommandUsed {
pub command: String,
}
impl_event!(Error; "error", message);
impl_event!(Messages; "messages", amount);
impl_event!(CommandUsed; "commands_used", command);
impl_true_event!(Mention; "mentions");
impl_true_event!(GuildLeave; "guildLeave");
impl_true_event!(Disconnect; "disconnect");
impl_true_event!(VoiceChannelJoin; "voiceChannelJoin");
#[cfg(test)]
mod test {
use super::*;
#[test]
fn error() {
assert_eq!(
Error {
message: "e".into(),
}.get_event_data(),
("error".into(), "e".into())
);
}
#[test]
fn messages() {
assert_eq!(
Messages { amount: 10 }.get_event_data(),
("messages".into(), "10".into())
);
}
#[test]
fn command_used() {
assert_eq!(
CommandUsed {
command: "c".into(),
}.get_event_data(),
("commands_used".into(), "c".into())
);
}
#[test]
fn mention() {
assert_eq!(
Mention {}.get_event_data(),
("mentions".into(), "true".into())
);
}
#[test]
fn guild_leave() {
assert_eq!(
GuildLeave {}.get_event_data(),
("guildLeave".into(), "true".into())
);
}
#[test]
fn disconnect() {
assert_eq!(
Disconnect {}.get_event_data(),
("disconnect".into(), "true".into())
);
}
#[test]
fn voice_channel_join() {
assert_eq!(
VoiceChannelJoin {}.get_event_data(),
("voiceChannelJoin".into(), "true".into())
);
}
}