use std::ops::{Deref, DerefMut};
use serde::{Serialize, Deserialize};
use structopt::{StructOpt};
#[derive(Debug,PartialEq,Serialize,Deserialize)]
pub struct Config {
pub log: LoggingOptions,
pub services: ExtendedOption<ServicesConfig>,
pub tokio_threads: i16,
pub ui: ExtendedOption<UiOptions>,
}
#[derive(Debug,PartialEq,Clone,Copy,Serialize,Deserialize,StructOpt)]
pub enum UiOptions {
Console(Jobs),
}
#[derive(Debug,PartialEq,Clone,Serialize,Deserialize)]
pub struct ServicesConfig {
pub web: ExtendedOption<WebConfig>,
pub socket_server: ExtendedOption<SocketServerConfig>,
pub telegram: ExtendedOption<TelegramConfig>,
}
#[derive(Debug,PartialEq,Clone,Serialize,Deserialize)]
pub struct TelegramConfig {
pub token: String,
pub bot: TelegramBotOptions,
pub notification_chat_ids: Vec<i64>,
}
#[derive(Debug,PartialEq,Clone,Serialize,Deserialize)]
pub enum TelegramBotOptions {
Dice,
Stateless,
Stateful,
}
#[derive(Debug,PartialEq,Clone,Serialize,Deserialize)]
pub enum RocketProfiles {
Debug,
Production,
}
#[derive(Debug,PartialEq,Clone,Serialize,Deserialize)]
pub enum RocketConfigOptions {
StandardRocketTomlFile,
Provided {
http_port: u16,
workers: u16,
}
}
#[derive(Debug,PartialEq,Clone,Serialize,Deserialize)]
pub struct WebConfig {
pub profile: RocketProfiles,
pub rocket_config: RocketConfigOptions,
pub sanity_check_routes: bool,
pub stats_routes: bool,
pub logs_following_routes: bool,
pub ogre_events_following_routes: bool,
pub ogre_events_queue_routes: bool,
pub web_app: bool,
pub routes_prefix: String,
}
#[derive(Debug,PartialEq,Clone,Serialize,Deserialize)]
pub struct SocketServerConfig {
pub interface: String,
pub port: u16,
pub workers: u16,
}
#[derive(Debug,PartialEq,Serialize,Deserialize)]
pub enum LoggingOptions {
Quiet,
ToConsole,
ToFile {
file_path: String,
rotation_size: usize,
rotations_kept: usize,
compress_rotated: bool,
},
}
#[derive(Debug,PartialEq,Clone,Copy,Serialize,Deserialize,StructOpt)]
pub enum Jobs {
Daemon,
CheckConfig,
}
#[derive(Debug,PartialEq,Clone,Serialize,Deserialize)]
pub enum ExtendedOption<T> {
Unset,
Disabled,
Enabled(T),
}
impl<T> ExtendedOption<T> {
pub fn is_enabled(&self) -> bool {
if let ExtendedOption::Enabled(_) = self {
true
} else {
false
}
}
}
impl<T> Deref for ExtendedOption<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
match self {
ExtendedOption::Enabled(raw) => raw,
ExtendedOption::Unset => panic!("BUG! attempted to `deref` the (non-existing) raw value -- from an 'Unset' variant of 'ExtendedOption'"),
ExtendedOption::Disabled => panic!("BUG! attempted to `deref` the (non-existing) raw value -- from an 'Disabled' variant of 'ExtendedOption'"),
}
}
}
impl<T> DerefMut for ExtendedOption<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
ExtendedOption::Enabled(raw) => raw,
ExtendedOption::Unset => panic!("BUG! attempted to `deref_mut` the (non-existing) raw value -- from an 'Unset' variant of 'ExtendedOption'"),
ExtendedOption::Disabled => panic!("BUG! attempted to `deref_mut` the (non-existing) raw value -- from an 'Disabled' variant of 'ExtendedOption'"),
}
}
}
impl Default for Config {
fn default() -> Self {
Self {
log: LoggingOptions::ToConsole,
services: ExtendedOption::Enabled(
ServicesConfig {
telegram: ExtendedOption::Enabled(TelegramConfig {
token: String::from("<<Open TelegramApp, search for BotFather, send /newbot>>"),
bot: TelegramBotOptions::Stateless,
notification_chat_ids: vec![
9999999999, 9999999999, ],
}),
web: ExtendedOption::Enabled(WebConfig {
profile: RocketProfiles::Debug,
rocket_config: RocketConfigOptions::Provided {
http_port: 8000,
workers: 1,
},
sanity_check_routes: false,
stats_routes: false,
logs_following_routes: false,
ogre_events_following_routes: false,
ogre_events_queue_routes: false,
web_app: true,
routes_prefix: "".to_string()
}),
socket_server: ExtendedOption::Enabled(SocketServerConfig {
interface: "0.0.0.0".to_string(),
port: 9758,
workers: 1,
}),
}
),
tokio_threads: 0,
ui: ExtendedOption::Enabled(UiOptions::Console(Jobs::Daemon)),
}
}
}
pub const REPLACEMENTS: &[(&str, &str)] = &[
("\n//![^\n]*", ""), ("\nuse serde::[^\n]*", ""), ("\n#[^\n]*", ""), ("(?s)\n///// EVERYTHING BELOW THIS LINE WILL NOT BE INCLUDED IN THE APPLICATION'S CONFIG FILE /////.*", ""), ("\n\n+", "\n\n"), ];