From 23081ad5d54e437978e575403ae1dcfb3d0cffb4 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Thu, 8 Feb 2018 22:36:42 -0500 Subject: [PATCH 1/2] Adds a `message_type` field to `MessageFromIRC`. The `message_type` allows switching to `notice` type messages. It defaults to `privmsg` and any other (string) values will have the same effect than using `privmsg`. No code should need to be updated after this change. --- ircbot/src/bin/gateway.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ircbot/src/bin/gateway.rs b/ircbot/src/bin/gateway.rs index b1341aa..8e10135 100644 --- a/ircbot/src/bin/gateway.rs +++ b/ircbot/src/bin/gateway.rs @@ -30,7 +30,9 @@ use std::env; #[derive(Serialize, Deserialize, Debug)] struct MessageToIRC { target: String, - body: String + body: String, + #[serde(default = "default_irc_message_type")] + message_type: String } #[derive(Serialize, Deserialize, Debug)] @@ -40,6 +42,10 @@ struct MessageFromIRC { body: String } +fn default_irc_message_type() -> String { + "privmsg".to_string() +} + fn main() { if let Err(_) = env::var("RUST_LOG") { env::set_var("RUST_LOG", "info"); @@ -81,7 +87,12 @@ fn main() { move |_channel: &mut Channel, _deliver: Deliver, _headers: BasicProperties, body: Vec| { let msg: Result = serde_json::from_slice(&body); if let Ok(msg) = msg { - server.send_privmsg(&msg.target, &msg.body).unwrap(); + if &msg.message_type == "notice" { + server.send_notice(&msg.target, &msg.body).unwrap(); + } + else { + server.send_privmsg(&msg.target, &msg.body).unwrap(); + } } }, "queue-publish", "", false, true, false, false, Table::new()); From bb406624d87265831456d5855742b283071cd0d0 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Fri, 9 Feb 2018 12:58:03 -0500 Subject: [PATCH 2/2] Changes message_type to an Enum. --- ircbot/src/bin/gateway.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/ircbot/src/bin/gateway.rs b/ircbot/src/bin/gateway.rs index 8e10135..18fef10 100644 --- a/ircbot/src/bin/gateway.rs +++ b/ircbot/src/bin/gateway.rs @@ -32,7 +32,7 @@ struct MessageToIRC { target: String, body: String, #[serde(default = "default_irc_message_type")] - message_type: String + message_type: IRCMessageType } #[derive(Serialize, Deserialize, Debug)] @@ -42,8 +42,15 @@ struct MessageFromIRC { body: String } -fn default_irc_message_type() -> String { - "privmsg".to_string() +#[derive(Serialize, Deserialize, Debug)] +#[serde(rename_all = "lowercase")] +enum IRCMessageType { + Privmsg, + Notice +} + +fn default_irc_message_type() -> IRCMessageType { + IRCMessageType::Privmsg } fn main() { @@ -87,11 +94,13 @@ fn main() { move |_channel: &mut Channel, _deliver: Deliver, _headers: BasicProperties, body: Vec| { let msg: Result = serde_json::from_slice(&body); if let Ok(msg) = msg { - if &msg.message_type == "notice" { - server.send_notice(&msg.target, &msg.body).unwrap(); - } - else { - server.send_privmsg(&msg.target, &msg.body).unwrap(); + match msg.message_type { + IRCMessageType::Notice => { + server.send_notice(&msg.target, &msg.body).unwrap(); + } + IRCMessageType::Privmsg => { + server.send_privmsg(&msg.target, &msg.body).unwrap(); + } } } },