Browse Source

Configuration file is not optional

develop
Drew Short 6 years ago
parent
commit
a4e2c2b46b
  1. 28
      src/args/parse.rs
  2. 6
      src/main.rs

28
src/args/parse.rs

@ -24,33 +24,17 @@ fn get_config_for_number<T: FromStr>(arg_value: Option<&str>, config_value: Opti
} }
} }
fn get_number<T: FromStr>(arg_value: Option<&str>, default_value: T) -> T {
match arg_value {
Some(value) => value.parse::<T>().unwrap_or(default_value),
None => default_value
}
}
pub fn get_host(args: &ArgMatches, config: &Option<Config>, default_host: &str) -> String {
pub fn get_host(args: &ArgMatches, config: &Config, default_host: &str) -> String {
let arg_value = args.value_of("host"); let arg_value = args.value_of("host");
match &config {
Some(c) => get_config_for_string(arg_value, &c.server.host, default_host),
None => String::from(arg_value.unwrap_or(default_host))
}.clone()
get_config_for_string(arg_value, &config.server.host, default_host).clone()
} }
pub fn get_port(args: &ArgMatches, config: &Option<Config>, default_port: i16) -> i16 {
pub fn get_port(args: &ArgMatches, config: &Config, default_port: i16) -> i16 {
let arg_value = args.value_of("port"); let arg_value = args.value_of("port");
match &config {
Some(c) => get_config_for_number(arg_value, c.server.port, default_port),
None => get_number(arg_value, default_port)
}
get_config_for_number(arg_value, config.server.port, default_port)
} }
pub fn get_workers(args: &ArgMatches, config: &Option<Config>, default_workers: usize) -> usize {
pub fn get_workers(args: &ArgMatches, config: &Config, default_workers: usize) -> usize {
let arg_value = args.value_of("workers"); let arg_value = args.value_of("workers");
match &config {
Some(c) => get_config_for_number(arg_value, c.server.workers, default_workers),
None => get_number(arg_value, default_workers)
}
get_config_for_number(arg_value, config.server.workers, default_workers)
} }

6
src/main.rs

@ -13,6 +13,8 @@ extern crate serde;
extern crate serde_derive; extern crate serde_derive;
extern crate serde_yaml; extern crate serde_yaml;
use std::process;
mod args; mod args;
mod config; mod config;
mod server; mod server;
@ -41,11 +43,11 @@ fn main() {
let config = match config::load::read(config_path) { let config = match config::load::read(config_path) {
Ok(c) => { Ok(c) => {
info!("Loaded configuration from \"{}\"", config_path); info!("Loaded configuration from \"{}\"", config_path);
Option::Some(c)
c
} }
Err(e) => { Err(e) => {
error!("{}", e); error!("{}", e);
Option::None
process::exit(-1)
} }
}; };

Loading…
Cancel
Save