-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_loader.rs
More file actions
109 lines (95 loc) · 3.48 KB
/
env_loader.rs
File metadata and controls
109 lines (95 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::config::model::{Config, DebugConfig, EmojiConfig};
use serenity::all::ChannelId;
use std::collections::HashMap;
use std::env;
pub fn load_config() -> Config {
let teatro_channel_id: ChannelId = load_channel_id_config("DISCORD_TEATRO_CHANNEL_ID");
let artes_channel_id: ChannelId = load_channel_id_config("DISCORD_ARTES_CHANNEL_ID");
let voting_emojis: [EmojiConfig; 5] = load_voting_emojis_config("VOTING_EMOJIS");
let gather_new_events: bool = load_bool_config("GATHER_NEW_EVENTS", true);
let venue_ticket_shop_url: HashMap<String, String> =
load_venue_ticket_shop_config("VENUE_TICKET_SHOP_URLS");
let debug_config = DebugConfig {
clear_channel: load_bool_config("DEBUG_CLEAR_CHANNEL", false),
exit_after_clearing: load_bool_config("DEBUG_EXIT_AFTER_CLEARING", false),
skip_sending: load_bool_config("DEBUG_SKIP_SENDING", false),
skip_feature_reactions: load_bool_config("DEBUG_SKIP_FEATURE_REACTIONS", false),
skip_artes: load_bool_config("DEBUG_SKIP_ARTES", false),
event_limit: load_i32_config("DEBUG_EVENT_LIMIT"),
};
Config {
debug_config,
teatro_channel_id,
artes_channel_id,
voting_emojis,
gather_new_events,
venue_ticket_shop_url,
}
}
fn load_channel_id_config(name: &str) -> ChannelId {
env::var(name)
.unwrap_or_else(|_| panic!("{} must be set.", name))
.parse()
.unwrap_or_else(|_| panic!("{} is not a valid Discord channel ID", name))
}
pub fn load_venue_ticket_shop_config(name: &str) -> HashMap<String, String> {
let config = env::var(name).unwrap_or_else(|_| panic!("{} must be set.", name));
let venue_ticket_shop: Vec<&str> = config
.split(";")
.collect();
venue_ticket_shop
.iter()
.map(|v| {
let venue_to_ticket_shop = v
.split_once(":")
.expect("Emojis must be comma-separated in the Name:ID format");
(
venue_to_ticket_shop.0.into(),
venue_to_ticket_shop.1.into(),
)
})
.collect()
}
pub fn load_voting_emojis_config(name: &str) -> [EmojiConfig; 5] {
let config = env::var(name).unwrap_or_else(|_| panic!("{} must be set.", name));
let emojis: [&str; 5] = config
.split(";")
.collect::<Vec<&str>>()
.try_into()
.expect("Expected just 5 semi-colon separated emojis");
emojis.map(|c| {
let split = c
.split_once(":")
.expect("Emojis must be comma-separated in the Name:ID format");
EmojiConfig {
id: split.1.to_string().parse().unwrap_or_else(|_| {
panic!(
"{} is not a valid Discord channel ID. Must be an integer but got: {}",
name, split.1
)
}),
name: split.0.to_string(),
}
})
}
fn load_bool_config(name: &str, default: bool) -> bool {
env::var(name)
.unwrap_or_else(|_| default.to_string())
.parse()
.unwrap_or_else(|_| {
panic!(
"Invalid config '{}'. Expected either 'true' or 'false'",
name
)
})
}
fn load_i32_config(name: &str) -> Option<i32> {
match env::var(name) {
Ok(value) => {
Some(value.parse().unwrap_or_else(|_| {
panic!("Invalid config '{}'. Expected an integer number.", name)
}))
}
Err(_) => None,
}
}