#![warn(missing_docs)]
pub mod client;
mod read;
pub mod server;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use anyhow::Context;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::Deserialize;
use self::read::parse_options;
use client::ClientOptions;
use server::ServerOptions;
#[derive(Deserialize, Debug, Clone)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Options {
#[serde(default)]
pub client: Option<ClientOptions>,
#[serde(default)]
pub server: Option<ServerOptions>,
}
pub fn read_options(path: &Path) -> anyhow::Result<Option<Options>> {
if !path.exists() {
return Ok(None);
}
let file = File::open(path).context("Failed to open options file")?;
let mut file = BufReader::new(file);
Ok(Some(
parse_options(&mut file).context("Failed to read options")?,
))
}
macro_rules! match_key {
($out:ident, $option:expr, $key:expr) => {
if let Some(value) = $option {
$out.insert($key.into(), value.to_string());
}
};
($out:ident, $option:expr, $key:expr, $version:expr) => {
if $version {
match_key!($out, $option, $key)
}
};
}
macro_rules! match_key_int {
($out:ident, $option:expr, $key:expr) => {
if let Some(value) = $option {
$out.insert($key.into(), value.to_int().to_string());
}
};
($out:ident, $option:expr, $key:expr, $version:expr) => {
if $version {
match_key_int!($out, $option, $key)
}
};
}
pub(crate) use match_key;
pub(crate) use match_key_int;