use std::path::Path;
use crate::config::global_config::GeneralConfig;
use crate::config::pool_config::PoolConfig;
use config::{Config, ConfigError, File, FileFormat};
use serde::{Deserialize, Serialize};
pub const DEFAULT_POOL: &str = "default";
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TyraConfig {
pub general: GeneralConfig,
pub thread_pool: PoolConfig,
}
impl TyraConfig {
pub fn new() -> Result<Self, ConfigError> {
let default: &str = std::include_str!("default.toml");
let mut config = Config::builder();
config = config.add_source(File::from_str(default, FileFormat::Toml));
let path = Path::new("config/tyra.toml");
if path.exists() {
config = config.add_source(File::from(Path::new("config/tyra.toml")));
}
let conf = config.build().expect("Could not fetch Config");
let mut parsed: TyraConfig = conf.try_deserialize().expect("Could not parse Config");
if parsed.general.name == "$HOSTNAME" {
parsed.general.name = String::from(hostname::get().unwrap().to_str().unwrap());
}
Ok(parsed)
}
}