Rustendo/src/util/config.rs

40 lines
1.2 KiB
Rust
Raw Normal View History

2024-02-17 01:28:24 +00:00
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,
2024-03-03 22:30:31 +00:00
pub user_id: u64,
2024-02-17 01:28:24 +00:00
}
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,
2024-03-03 22:30:31 +00:00
Err(_) => create_empty(),
2024-02-17 01:28:24 +00:00
};
let config_file = serde_json::from_reader(config_file).unwrap();
Ok(config_file)
}
2024-03-03 22:30:31 +00:00
fn create_empty() -> fs::File {
2024-02-17 01:28:24 +00:00
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(),
2024-03-03 22:30:31 +00:00
user_id: 1,
2024-02-17 01:28:24 +00:00
};
let mut config_file = fs::File::create(CONFIG_FILE).unwrap();
let file_content = serde_json::to_string(&example_config).unwrap();
2024-03-03 22:30:31 +00:00
config_file.write_all(file_content.as_bytes()).unwrap();
2024-02-17 01:28:24 +00:00
panic!("There is no config. But now there is a template.")
2024-03-03 22:30:31 +00:00
}