From f95872126d50be44ebd9b15a1bfca68a847bfa27 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Wed, 5 Dec 2018 18:47:40 -0600 Subject: [PATCH] Moved argument parsing into module * Port was redefined as i16 as that is the valid range for ports * argument parsing is cleaned up and moved into args module * correctly prefer arguments over config if present --- src/args.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/config/model.rs | 2 +- src/main.rs | 31 ++++++------------------- 3 files changed, 64 insertions(+), 25 deletions(-) create mode 100644 src/args.rs diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..73ee634 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,56 @@ +use std::str::FromStr; + +use clap::ArgMatches; + +use config::model::Config; + +fn get_config_for_string(arg_value: Option<&str>, config_value: Option, default_value: &str) -> String { + String::from(match arg_value { + Some(v) => v, + None => match c.server.host { + Some(host) => host, + None => default_host, + } + }) +} + +fn get_config_for_number(arg_value: Option<&str>, config_value: Option, default_value: T) -> T { + match arg_value { + Some(v) => v.parse::().unwrap_or(default_value), + None => match config_value { + Some(v) => v, + None => default_value + } + } +} + +fn get_number(arg_value: Option<&str>, default_value: T) -> T { + match arg_value { + Some(value) => port.parse::().unwrap_or(default_port), + None => default_port + } +} + +pub fn get_host(args: &ArgMatches, config: &Option, default_host: &str) -> String { + 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() +} + +pub fn get_port(args: &ArgMatches, config: &Option, default_port: i16) -> i16 { + 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) + } +} + +pub fn get_workers(args: &ArgMatches, config: &Option, default_workers: usize) -> usize { + 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(args_value, default_workers) + } +} \ No newline at end of file diff --git a/src/config/model.rs b/src/config/model.rs index ec87fef..a6ba7ab 100644 --- a/src/config/model.rs +++ b/src/config/model.rs @@ -1,7 +1,7 @@ #[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct ServerConfig { pub host: Option, - pub port: Option, + pub port: Option, pub workers: Option, } diff --git a/src/main.rs b/src/main.rs index da7f66c..c15b687 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,12 +15,14 @@ extern crate serde_yaml; use clap::{App, Arg}; -mod server; +mod args; mod config; +mod server; + const VERSION: &'static str = env!("CARGO_PKG_VERSION"); const DEFAULT_HOST: &'static str = "localhost"; -const DEFAULT_PORT: i32 = 8080; +const DEFAULT_PORT: i16 = 8080; lazy_static! { static ref DEFAULT_PORT_STR: String = String::from(DEFAULT_PORT.to_string()); @@ -83,28 +85,9 @@ fn main() { } }; - let host = String::from(match &config { - Some(c) => match &c.server.host { - Some(host) => host, - None => DEFAULT_HOST, - } - None => args.value_of("host").unwrap_or(DEFAULT_HOST) - }.clone()); - let port = match &config { - Some(c) => c.server.port.unwrap_or(DEFAULT_PORT), - None => args - .value_of("port") - .unwrap() - .parse::() - .unwrap_or(DEFAULT_PORT) - }; - let workers = match &config { - Some(c) => c.server.workers.unwrap_or(*DEFAULT_WORKERS), - None => match args.value_of("workers") { - Some(count) => count.parse::().unwrap_or(*DEFAULT_WORKERS), - None => num_cpus::get(), - } - }; + let host = args::get_host(&args, &config, DEFAULT_HOST); + let port = args::get_port(&args, &config, DEFAULT_PORT); + let workers = args::get_workers(&args, &config, *DEFAULT_WORKERS); info!( "Starting server on {}:{} with workers={} and config {}",