33 lines
988 B
Rust
33 lines
988 B
Rust
|
use std::{fs, io::Write};
|
||
|
|
||
|
use serde::{Deserialize, Serialize};
|
||
|
use std::error::Error;
|
||
|
|
||
|
#[derive(Deserialize, Serialize)]
|
||
|
pub struct Config {
|
||
|
pub discord_token: String
|
||
|
}
|
||
|
|
||
|
const CONFIG_FILE: &str = "./data/config.json";
|
||
|
|
||
|
pub fn load() -> Result<Config, Box<dyn Error>> {
|
||
|
// TODO: load config, create empty config if there is no config, stop if there is no complete config
|
||
|
let config_file = match fs::File::open(CONFIG_FILE) {
|
||
|
Ok(file) => file,
|
||
|
Err(_) => create_empty()
|
||
|
};
|
||
|
let config_file = serde_json::from_reader(config_file).unwrap();
|
||
|
Ok(config_file)
|
||
|
}
|
||
|
|
||
|
fn create_empty() -> fs::File{
|
||
|
let example_config = Config {
|
||
|
discord_token: "paste_your_config".to_string()
|
||
|
};
|
||
|
|
||
|
let mut config_file = fs::File::create(CONFIG_FILE).unwrap();
|
||
|
let file_content = serde_json::to_string(&example_config).unwrap();
|
||
|
config_file.write_all(&file_content.as_bytes()).unwrap();
|
||
|
|
||
|
panic!("There is no config. But now there is a template.")
|
||
|
}
|