use serde::Deserialize;

#[derive(Deserialize)]
struct ConfigFile {
    pub name: String,
    pub nats_cert: String,
    pub nats_key: String,
    pub nats_url: String,
}

#[derive(Clone)]
pub struct LypheDBConfig {
    pub name: String,
    pub nats_cert: String,
    pub nats_key: String,
    pub nats_url: String,
}

pub fn load_config(path: &str) -> LypheDBConfig {
    let s = std::fs::read_to_string(path);
    if let Err(e) = s {
        panic!("failed to read config file: {}", e);
    }
    let s = s.unwrap();
    let cnf = toml::from_str::<ConfigFile>(&s);
    if let Err(e) = cnf {
        panic!("failed to parse config file: {}", e);
    }
    let cnf = cnf.unwrap();

    // quick checks and conversions
    let mut config = LypheDBConfig {
        name: cnf.name,
        nats_cert: cnf.nats_cert,
        nats_key: cnf.nats_key,
        nats_url: cnf.nats_url,
    };

    config
}