You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.5 KiB
42 lines
1.5 KiB
use std::str::FromStr;
|
|
|
|
use clap::ArgMatches;
|
|
|
|
use crate::config::model::Config;
|
|
|
|
fn get_config_for_string(arg_value: Option<&str>, config_value: &Option<String>, default_value: &str) -> String {
|
|
debug!("arg: {:?}; config: {:?}; default: {}", arg_value, config_value, default_value);
|
|
String::from(match arg_value {
|
|
Some(v) => v,
|
|
None => match config_value {
|
|
Some(host) => host,
|
|
None => default_value,
|
|
}
|
|
})
|
|
}
|
|
|
|
fn get_config_for_number<T: FromStr + std::fmt::Debug>(arg_value: Option<&str>, config_value: Option<T>, default_value: T) -> T {
|
|
debug!("arg: {:?}; config: {:?}; default: {:?}", arg_value, config_value, default_value);
|
|
match arg_value {
|
|
Some(v) => v.parse::<T>().unwrap_or(default_value),
|
|
None => match config_value {
|
|
Some(v) => v,
|
|
None => default_value
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn get_host(args: &ArgMatches, config: &Config, default_host: &str) -> String {
|
|
let arg_value = args.value_of("host");
|
|
get_config_for_string(arg_value, &config.server.host, default_host).clone()
|
|
}
|
|
|
|
pub fn get_port(args: &ArgMatches, config: &Config, default_port: i16) -> i16 {
|
|
let arg_value = args.value_of("port");
|
|
get_config_for_number(arg_value, config.server.port, default_port)
|
|
}
|
|
|
|
pub fn get_workers(args: &ArgMatches, config: &Config, default_workers: usize) -> usize {
|
|
let arg_value = args.value_of("workers");
|
|
get_config_for_number(arg_value, config.server.workers, default_workers)
|
|
}
|