Rustendo/src/util/config.rs

39 lines
1.2 KiB
Rust

use std::{fs, io::Write};
use serde::{Deserialize, Serialize};
use std::error::Error;
#[derive(Deserialize, Serialize)]
pub struct Config {
pub discord_token: String,
pub lavalink_address: String,
pub lavalink_password: String,
pub user_id: u64,
}
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_token".to_string(),
lavalink_address: "paste_your_lavalink_address".to_string(),
lavalink_password: "paste_your_lavalink_password".to_string(),
user_id: 1,
};
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.")
}