quectocraft/src/config.rs

28 lines
716 B
Rust
Raw Normal View History

2022-12-17 05:10:01 +00:00
use std::{net::IpAddr, fs::OpenOptions};
use serde::Deserialize;
2022-12-17 20:39:56 +00:00
#[derive(Deserialize, PartialEq, Eq)]
2022-12-17 20:45:43 +00:00
#[serde(rename_all="lowercase")]
2022-12-17 20:39:56 +00:00
pub enum LoginMode {
Offline,
Velocity,
// TODO online, bungeecord
}
2022-12-17 05:10:01 +00:00
#[derive(Deserialize)]
pub struct Config {
pub addr: IpAddr,
pub port: u16,
2022-12-17 20:39:56 +00:00
pub login: LoginMode,
pub velocity_secret: Option<String>,
2022-12-17 05:10:01 +00:00
}
pub fn load_config() -> Result<Config, Box<dyn std::error::Error>> {
2022-12-17 20:39:56 +00:00
let config: Config = serde_json::from_reader(OpenOptions::new().read(true).open("./config.json")?)?;
if config.login == LoginMode::Velocity && config.velocity_secret.is_none() {
Err("Velocity is enabled but no secret is configured")?
}
2022-12-17 05:10:01 +00:00
Ok(config)
}