manifold/src/config.rs

126 lines
4.5 KiB
Rust

use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::path::PathBuf;
use serenity::model::prelude::*;
use crate::error::ManifoldResult;
#[derive(Debug)]
pub struct ManifoldConfig {
config_path: PathBuf,
config_environment: String,
config: HashMap<String, String>
}
pub trait Config {
fn load_config(&mut self) -> ManifoldResult<usize>;
fn save_config(&self) -> ManifoldResult<usize>;
fn create_default_config(&self, config_file: &String, config_environment: &String) -> ManifoldResult<usize>;
fn get_environment(&self) -> &String;
fn get_path(&self) -> &PathBuf;
fn get_environment_mut(&mut self) -> &mut String;
fn get_path_mut(&mut self) -> &mut PathBuf;
fn get_value(&self, key: &String) -> Option<&String>;
fn set_value(&mut self, key: &String, value: &String) -> Option<String>;
fn unset_value(&mut self, key: &String) -> Option<String>;
fn get_role(&self, role_name: &String) -> RoleId;
fn get_channel(&self, channel_name: &String) -> ChannelId;
fn get_message(&self, message_id: &String) -> MessageId;
}
impl ManifoldConfig {
pub fn new() -> Self {
ManifoldConfig {
config_path: Default::default(),
config_environment: "Production".to_string(),
config: HashMap::<String, String>::new(),
}
}
}
impl Config for ManifoldConfig {
fn load_config(&mut self) -> ManifoldResult<usize> {
let file = File::open(&self.config_path).expect("Could not open config file. Please check and retry.");
let reader = BufReader::new(file);
self.config = serde_json::from_reader(reader)
.map_err(|e| {error!("Error reading config as JSON: {:?}", e.to_string()); e} )?;
let config_entry_count = self.config.len();
debug!("Loaded config: {:?}", &self.config);
debug!("Loaded {} entries", config_entry_count);
Ok(config_entry_count)
}
fn save_config(&self) -> ManifoldResult<usize> {
let file = File::create(&self.config_path).expect("Could not open config file for saving.");
let writer = BufWriter::new(file);
serde_json::to_writer(writer, &self.config)?;
let config_entry_count = self.config.len();
Ok(config_entry_count)
}
fn create_default_config(&self, config_file: &String, config_environment: &String) -> ManifoldResult<usize> {
let mut config = ManifoldConfig::new();
config.config_environment = config_environment.to_owned();
config.config_path = PathBuf::from(config_file);
config.config.insert(format!("{}LogChannel", config_environment), "".to_string());
config.config.insert(format!("{}BotOwner", config_environment), "".to_string());
config.config.insert(format!("{}AdminRole", config_environment), "".to_string());
config.config.insert(format!("{}DJRole", config_environment), "".to_string());
config.config.insert(format!("{}SpamChannel", config_environment), "true".to_string());
config.config.insert(format!("{}ResponsesFilePath", config_environment), "config/responses.txt".to_string());
config.save_config()
}
fn get_environment(&self) -> &String {
&self.config_environment
}
fn get_path(&self) -> &PathBuf {
&self.config_path
}
fn get_environment_mut(&mut self) -> &mut String {
&mut self.config_environment
}
fn get_path_mut(&mut self) -> &mut PathBuf {
&mut self.config_path
}
fn get_value(&self, key: &String) -> Option<&String> {
self.config.get(&format!("{}{}", &self.config_environment, key))
}
fn set_value(&mut self, key: &String, value: &String) -> Option<String> {
self.config.insert(format!("{}{}", &self.config_environment, key), value.clone())
}
fn unset_value(&mut self, key: &String) -> Option<String> {
self.config.remove(&*format!("{}{}", &self.config_environment, key))
}
fn get_role(&self, role_name: &String) -> RoleId {
let key = format!("{}Role", role_name);
RoleId(self.get_value(&key).unwrap().parse::<u64>().unwrap())
}
fn get_channel(&self, channel_name: &String) -> ChannelId {
let key = format!("{}Channel", channel_name);
ChannelId(self.get_value(&key).unwrap().parse::<u64>().unwrap())
}
fn get_message(&self, message_id: &String) -> MessageId {
let key = format!("{}Message", message_id);
MessageId(self.get_value(&key).unwrap().parse::<u64>().unwrap())
}
}