use std::time::Duration;
use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use crate::config::file::srv::{KEEP_ALIVE_DEFAULT, LISTEN_ADDRESSES_DEFAULT, SrvConfig};
#[allow(
clippy::doc_markdown,
reason = "for command line arguments, formatting `TileJSON` is awkward"
)]
#[derive(clap::Args, Debug, PartialEq, Default)]
#[command(about, version)]
pub struct SrvArgs {
#[arg(help = format!("Connection keep alive timeout. [DEFAULT: {KEEP_ALIVE_DEFAULT}]"), short, long)]
pub keep_alive: Option<u64>,
#[arg(help = format!("The socket address to bind. [DEFAULT: {LISTEN_ADDRESSES_DEFAULT}]"), short, long)]
pub listen_addresses: Option<String>,
#[arg(long)]
pub route_prefix: Option<String>,
#[arg(long, hide = true)]
pub base_path: Option<String>,
#[arg(short = 'W', long)]
pub workers: Option<usize>,
#[arg(long)]
pub preferred_encoding: Option<PreferredEncoding>,
#[arg(short = 'u', long = "webui")]
#[cfg(all(feature = "webui", not(docsrs)))]
pub web_ui: Option<WebUiMode>,
#[arg(long)]
#[cfg(feature = "_tiles")]
pub tilejson_url_version_param: Option<String>,
#[arg(short = 'C', long)]
pub cache_size: Option<u64>,
#[arg(long, value_parser = parse_duration)]
pub cache_expiry: Option<Duration>,
#[arg(long, value_parser = parse_duration)]
pub cache_idle_timeout: Option<Duration>,
}
fn parse_duration(s: &str) -> Result<Duration, String> {
use humantime_serde::re::humantime;
humantime::parse_duration(s).map_err(|e| e.to_string())
}
#[cfg(all(feature = "webui", not(docsrs)))]
#[derive(PartialEq, Eq, Debug, Clone, Copy, Default, Serialize, Deserialize, ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum WebUiMode {
#[default]
#[serde(alias = "false")]
Disable,
#[serde(alias = "enable-for-all")]
#[clap(alias("enable-for-all"))]
EnableForAll,
}
#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize, ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum PreferredEncoding {
#[serde(alias = "br")]
#[clap(alias("br"))]
Brotli,
Gzip,
}
impl SrvArgs {
pub(crate) fn merge_into_config(self, srv_config: &mut SrvConfig) {
if self.keep_alive.is_some() {
srv_config.keep_alive = self.keep_alive;
}
if self.listen_addresses.is_some() {
srv_config.listen_addresses = self.listen_addresses;
}
if self.route_prefix.is_some() {
srv_config.route_prefix = self.route_prefix;
}
if self.base_path.is_some() {
srv_config.base_path = self.base_path;
}
if self.workers.is_some() {
srv_config.worker_processes = self.workers;
}
if self.preferred_encoding.is_some() {
srv_config.preferred_encoding = self.preferred_encoding;
}
#[cfg(all(feature = "webui", not(docsrs)))]
if self.web_ui.is_some() {
srv_config.web_ui = self.web_ui;
}
#[cfg(feature = "_tiles")]
if self.tilejson_url_version_param.is_some() {
srv_config.tilejson_url_version_param = self.tilejson_url_version_param;
}
}
}