From a6bba31d0fb8a6a22ffaf4fc0c44f37b1cfa212e Mon Sep 17 00:00:00 2001
From: Timothy du Heaume <timothy.duheaume@gmail.com>
Date: Sat, 1 Feb 2020 22:31:17 +0900
Subject: [PATCH] convert config file format from toml to yaml

This also involves changing ownership details in the config struct due to differing ownership strictness in the yaml deserializer.
---
 Cargo.toml           |  2 +-
 src/config.rs        | 30 +++++++++++++++---------------
 src/config.test.toml | 20 --------------------
 src/config.test.yaml | 20 ++++++++++++++++++++
 src/config.toml      | 23 -----------------------
 src/config.ucc.toml  | 21 ---------------------
 src/config.ucc.yaml  | 21 +++++++++++++++++++++
 src/config.yaml      | 23 +++++++++++++++++++++++
 src/main.rs          |  2 +-
 src/voting.rs        | 22 +++++++++++-----------
 10 files changed, 92 insertions(+), 92 deletions(-)
 delete mode 100644 src/config.test.toml
 create mode 100644 src/config.test.yaml
 delete mode 100644 src/config.toml
 delete mode 100644 src/config.ucc.toml
 create mode 100644 src/config.ucc.yaml
 create mode 100644 src/config.yaml

diff --git a/Cargo.toml b/Cargo.toml
index 02f571c..3d18dd1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,6 +10,6 @@ rand = "^0.7.2"
 lazy_static = "^1.4.0"
 log = "^0.4.8"
 simplelog = "^0.7.4"
-toml = "^0.5.6"
+serde_yaml = "^0.8"
 serde = "^1.0.104"
 chrono = "^0.4.10"
diff --git a/src/config.rs b/src/config.rs
index b310d94..9761f70 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,32 +1,32 @@
 use serde::Deserialize;
-use serenity;
+use serenity::model::id;
 use std::fs;
-use toml;
+use serde_yaml;
 
 lazy_static! {
-    static ref CONFIG_FILE: String = fs::read_to_string("config.toml").unwrap();
-    pub static ref CONFIG: UccbotConfig = toml::from_str(&CONFIG_FILE).unwrap();
+    static ref CONFIG_FILE: String = fs::read_to_string("config.yaml").unwrap();
+    pub static ref CONFIG: UccbotConfig = serde_yaml::from_str(&CONFIG_FILE).unwrap();
 }
 
-#[derive(Deserialize)]
+#[derive(Debug, Deserialize)]
 pub struct UccbotConfig {
     pub server_id: u64,
-    pub main_channel: serenity::model::id::ChannelId,
-    pub welcome_channel: serenity::model::id::ChannelId,
-    pub announcement_channel: serenity::model::id::ChannelId,
+    pub main_channel: id::ChannelId,
+    pub welcome_channel: id::ChannelId,
+    pub announcement_channel: id::ChannelId,
     pub bot_id: u64,
     pub vote_pool_size: i8,
     pub vote_role: u64,
     pub tiebreaker_role: u64,
     pub unregistered_member_role: u64,
     pub registered_member_role: u64,
-    pub command_prefix: &'static str,
-    pub for_vote: &'static str,
-    pub against_vote: &'static str,
-    pub abstain_vote: &'static str,
-    pub approve_react: &'static str,
-    pub disapprove_react: &'static str,
-    pub unsure_react: &'static str,
+    pub command_prefix: String,
+    pub for_vote: String,
+    pub against_vote: String,
+    pub abstain_vote: String,
+    pub approve_react: String,
+    pub disapprove_react: String,
+    pub unsure_react: String,
 }
 
 impl UccbotConfig {
diff --git a/src/config.test.toml b/src/config.test.toml
deleted file mode 100644
index 5ca508a..0000000
--- a/src/config.test.toml
+++ /dev/null
@@ -1,20 +0,0 @@
-server_id = 606351521117896704 # general
-main_channel = 606351521117896706 # the-corner
-welcome_channel = 606351613816209418 # general
-announcement_channel = 606351521117896706 # the-corner
-
-bot_id = 607078903969742848
-
-vote_pool_size = 2
-vote_role = 607478818038480937 # Vote Role
-tiebreaker_role = 607509283483025409 # tie-breaker
-unregistered_member_role = 608282247350714408 # unregistered
-registered_member_role = 608282133118582815 # registered
-command_prefix = "!"
-
-for_vote = "👍"
-against_vote = "👎"
-abstain_vote = "🙊"
-approve_react = "⬆"
-disapprove_react = "⬇"
-unsure_react = "❔"
diff --git a/src/config.test.yaml b/src/config.test.yaml
new file mode 100644
index 0000000..f0d8295
--- /dev/null
+++ b/src/config.test.yaml
@@ -0,0 +1,20 @@
+server_id: 606351521117896704 # general
+main_channel: 606351521117896706 # the-corner
+welcome_channel: 606351613816209418 # general
+announcement_channel: 606351521117896706 # the-corner
+
+bot_id: 607078903969742848
+
+vote_pool_size: 2
+vote_role: 607478818038480937 # Vote Role
+tiebreaker_role: 607509283483025409 # tie-breaker
+unregistered_member_role: 608282247350714408 # unregistered
+registered_member_role: 608282133118582815 # registered
+command_prefix: "!"
+
+for_vote: "👍"
+against_vote: "👎"
+abstain_vote: "🙊"
+approve_react: "⬆"
+disapprove_react: "⬇"
+unsure_react: "❔"
diff --git a/src/config.toml b/src/config.toml
deleted file mode 100644
index bb0b38e..0000000
--- a/src/config.toml
+++ /dev/null
@@ -1,23 +0,0 @@
-server_id = 606351521117896704
-#general
-main_channel = 606351521117896706
-#the-corner
-welcome_channel = 606351613816209418
-#general
-announcement_channel = 606351521117896706
-
-bot_id = 607078903969742848
-
-vote_pool_size = 2
-vote_role = 607478818038480937
-tiebreaker_role = 607509283483025409
-unregistered_member_role = 608282247350714408
-registered_member_role = 608282133118582815
-command_prefix = "!"
-
-for_vote = "👍"
-against_vote = "👎"
-abstain_vote = "🙊"
-approve_react = "⬆"
-disapprove_react = "⬇"
-unsure_react = "❔"
diff --git a/src/config.ucc.toml b/src/config.ucc.toml
deleted file mode 100644
index 52253cd..0000000
--- a/src/config.ucc.toml
+++ /dev/null
@@ -1,21 +0,0 @@
-server_id = 264401248676085760 # ucc
-main_channel = 264401248676085760 # ucc
-welcome_channel = 606750983699300372 # welcome
-announcement_channel = 264411219627212801 # committee
-
-bot_id = 635407267881156618
-
-vote_pool_size = 7 # 4 exec + 3 ocm
-vote_role = 269817189966544896 # @committee
-tiebreaker_role = 635370432568098817 # @Presiding Presidenterino
-unregistered_member_role = 0 # does not exist
-registered_member_role = 0 # does not exist
-
-command_prefix = "!"
-
-for_vote = "👍"
-against_vote = "👎"
-abstain_vote = "🙊"
-approve_react = "⬆"
-disapprove_react = "⬇"
-unsure_react = "❔"
diff --git a/src/config.ucc.yaml b/src/config.ucc.yaml
new file mode 100644
index 0000000..a299b93
--- /dev/null
+++ b/src/config.ucc.yaml
@@ -0,0 +1,21 @@
+server_id: 264401248676085760 # ucc
+main_channel: 264401248676085760 # ucc
+welcome_channel: 606750983699300372 # welcome
+announcement_channel: 264411219627212801 # committee
+
+bot_id: 635407267881156618
+
+vote_pool_size: 7 # 4 exec + 3 ocm
+vote_role: 269817189966544896 # @committee
+tiebreaker_role: 635370432568098817 # @Presiding Presidenterino
+unregistered_member_role: 0 # does not exist
+registered_member_role: 0 # does not exist
+
+command_prefix: "!"
+
+for_vote: "👍"
+against_vote: "👎"
+abstain_vote: "🙊"
+approve_react: "⬆"
+disapprove_react: "⬇"
+unsure_react: "❔"
diff --git a/src/config.yaml b/src/config.yaml
new file mode 100644
index 0000000..9cbe58b
--- /dev/null
+++ b/src/config.yaml
@@ -0,0 +1,23 @@
+server_id: 606351521117896704
+#general
+main_channel: 606351521117896706
+#the-corner
+welcome_channel: 606351613816209418
+#general
+announcement_channel: 606351521117896706
+
+bot_id: 607078903969742848
+
+vote_pool_size: 2
+vote_role: 607478818038480937
+tiebreaker_role: 607509283483025409
+unregistered_member_role: 608282247350714408
+registered_member_role: 608282133118582815
+command_prefix: "!"
+
+for_vote: "👍"
+against_vote: "👎"
+abstain_vote: "🙊"
+approve_react: "⬆"
+disapprove_react: "⬇"
+unsure_react: "❔"
diff --git a/src/main.rs b/src/main.rs
index e0ad703..de524f8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -38,7 +38,7 @@ impl EventHandler for Handler {
     // Event handlers are dispatched through a threadpool, and so multiple
     // events can be dispatched simultaneously.
     fn message(&self, ctx: Context, msg: Message) {
-        if !(msg.content.starts_with(CONFIG.command_prefix)) {
+        if !(msg.content.starts_with(&CONFIG.command_prefix)) {
             return;
         }
         let message_content: Vec<_> = msg.content[1..].splitn(2, ' ').collect();
diff --git a/src/voting.rs b/src/voting.rs
index d7dd907..9e4f4d5 100644
--- a/src/voting.rs
+++ b/src/voting.rs
@@ -163,7 +163,7 @@ fn create_poll(ctx: &Context, msg: &Message, topic: &str) {
 
 #[derive(Debug, Clone)]
 struct MotionInfo {
-    votes: HashMap<&'static str, Vec<serenity::model::user::User>>,
+    votes: HashMap<String, Vec<serenity::model::user::User>>,
 }
 
 lazy_static! {
@@ -179,18 +179,18 @@ fn get_cached_motion(ctx: &Context, msg: &Message) -> MotionInfo {
             votes: {
                 let mut m = HashMap::new();
                 m.insert(
-                    CONFIG.for_vote,
-                    msg.reaction_users(ctx, CONFIG.for_vote, None, None)
+                    CONFIG.for_vote.to_string(),
+                    msg.reaction_users(ctx, CONFIG.for_vote.to_string(), None, None)
                         .unwrap(),
                 );
                 m.insert(
-                    CONFIG.against_vote,
-                    msg.reaction_users(ctx, CONFIG.against_vote, None, None)
+                    CONFIG.against_vote.to_string(),
+                    msg.reaction_users(ctx, CONFIG.against_vote.to_string(), None, None)
                         .unwrap(),
                 );
                 m.insert(
-                    CONFIG.abstain_vote,
-                    msg.reaction_users(ctx, CONFIG.abstain_vote, None, None)
+                    CONFIG.abstain_vote.to_string(),
+                    msg.reaction_users(ctx, CONFIG.abstain_vote.to_string(), None, None)
                         .unwrap(),
                 );
                 m
@@ -347,11 +347,11 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
                 match user.has_role(&ctx, CONFIG.server_id, CONFIG.vote_role) {
                     Ok(true) => {
                         // remove vote if already voted
-                        for react in [CONFIG.for_vote, CONFIG.against_vote, CONFIG.abstain_vote]
+                        for react in [CONFIG.for_vote.to_string(), CONFIG.against_vote.to_string(), CONFIG.abstain_vote.to_string()]
                             .iter()
                             .filter(|r| r != &&add_reaction.emoji.as_data().as_str())
                         {
-                            for a_user in message.reaction_users(&ctx, *react, None, None).unwrap()
+                            for a_user in message.reaction_users(&ctx, react.as_str(), None, None).unwrap()
                             {
                                 if a_user.id.0 == user.id.0 {
                                     if let Err(why) = add_reaction.delete(&ctx) {
@@ -382,8 +382,8 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
                         update_motion(&ctx, &mut message, &user, "add", add_reaction);
                     }
                     Ok(false) => {
-                        if ![CONFIG.approve_react, CONFIG.disapprove_react]
-                            .contains(&add_reaction.emoji.as_data().as_str())
+                        if ![CONFIG.approve_react.to_string(), CONFIG.disapprove_react.to_string()]
+                            .contains(&add_reaction.emoji.as_data())
                         {
                             if let Err(why) = add_reaction.delete(&ctx) {
                                 error!("Error deleting react: {:?}", why);
-- 
GitLab