summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: d32c5253d3f2fc166f1794d5539e78d8fe7a06a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use std::fs::File;
use std::io::Read;

use serde_derive::Deserialize;

#[derive(Deserialize)]
pub struct Config {
    pub bot_token: String,
    pub log_file: String,
    pub db_file: String,
}

impl Config {
    // Read config file. Panic if any error occurs.
    pub fn from_file(file_path: &str) -> Config {
        let mut file = File::open(file_path)
            .unwrap_or_else(|_| panic!("cannot open file {}", file_path));
        let mut config = String::new();
        file.read_to_string(&mut config)
            .unwrap_or_else(|_| panic!("cannot read config file {}", file_path));
        return serde_json::from_str(config.as_str())
            .expect("cannot decode config file in JSON");
    }
}