manifold/src/config.rs

67 lines
1.9 KiB
Rust
Raw Normal View History

2021-11-12 12:02:22 +00:00
use std::path::PathBuf;
2022-07-17 23:25:59 +00:00
use config::Config;
2023-08-23 17:37:42 +00:00
use poise::serenity_prelude::model::prelude::{ChannelId, MessageId, RoleId};
2022-07-17 23:25:59 +00:00
use crate::ManifoldResult;
2021-11-12 12:02:22 +00:00
2023-08-23 17:37:42 +00:00
#[derive(Debug)]
2021-11-12 12:02:22 +00:00
pub struct ManifoldConfig {
2022-07-17 23:25:59 +00:00
pub environment: String,
pub path: PathBuf,
pub config: Config,
2021-11-12 12:02:22 +00:00
}
impl ManifoldConfig {
2022-07-17 23:25:59 +00:00
pub fn load_config(config_file: &String, bot_environment: &String) -> ManifoldResult<Self> {
let settings = Config::builder()
.set_override("BotEnvironment", bot_environment.clone())?
.add_source(config::File::with_name(config_file))
.add_source(config::Environment::with_prefix("MANIFOLD"))
.build()?;
Ok(Self {
environment: bot_environment.clone(),
path: PathBuf::from(config_file),
config: settings
})
2021-11-12 12:02:22 +00:00
}
2022-07-17 23:25:59 +00:00
pub fn get_environment(&self) -> &String {
&self.environment
2021-11-12 12:02:22 +00:00
}
2022-07-17 23:25:59 +00:00
pub fn get_path(&self) -> &PathBuf {
&self.path
2021-11-12 12:02:22 +00:00
}
2022-07-17 23:25:59 +00:00
pub fn get_environment_mut(&mut self) -> &mut String {
&mut self.environment
2021-11-12 12:02:22 +00:00
}
2022-07-17 23:25:59 +00:00
pub fn get_path_mut(&mut self) -> &mut PathBuf {
&mut self.path
2021-11-12 12:02:22 +00:00
}
2022-07-17 23:25:59 +00:00
pub fn get_value(&self, key: &String) -> ManifoldResult<String> {
Ok(self.config.get_string(&format!("{}{}", &self.environment, key))?)
2021-11-12 12:02:22 +00:00
}
2022-07-17 23:25:59 +00:00
pub fn get_role(&self, role_name: &String) -> ManifoldResult<RoleId> {
2021-11-12 12:02:22 +00:00
let key = format!("{}Role", role_name);
2022-07-17 23:25:59 +00:00
Ok(RoleId(self.get_value(&key)?.parse::<u64>()?))
2021-11-12 12:02:22 +00:00
}
2022-07-17 23:25:59 +00:00
pub fn get_channel(&self, channel_name: &String) -> ManifoldResult<ChannelId> {
2021-11-12 12:02:22 +00:00
let key = format!("{}Channel", channel_name);
2022-07-17 23:25:59 +00:00
Ok(ChannelId(self.get_value(&key)?.parse::<u64>()?))
2021-11-12 12:02:22 +00:00
}
2022-07-17 23:25:59 +00:00
pub fn get_message(&self, message_id: &String) -> ManifoldResult<MessageId> {
2021-11-12 12:02:22 +00:00
let key = format!("{}Message", message_id);
2022-07-17 23:25:59 +00:00
Ok(MessageId(self.get_value(&key)?.parse::<u64>()?))
2021-11-12 12:02:22 +00:00
}
}