From e5dc929135ff5bd0325f624d582ff5040c5b017f Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 9 Jun 2019 00:10:33 +0200 Subject: [PATCH] Moved parameter reader into own file --- src/configuration.rs | 77 ++++++++++++++++++++++++++++++++--- src/configuration/settings.rs | 15 ------- src/main.rs | 55 ++++--------------------- 3 files changed, 80 insertions(+), 67 deletions(-) delete mode 100644 src/configuration/settings.rs diff --git a/src/configuration.rs b/src/configuration.rs index 1342e14..7accafb 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -1,7 +1,72 @@ -//! File: configuration.rs -//! Description: Definition file for the configuration namespace -//! Author:Jali -//! Created On: Tue 04 Jun 2019 10:06:53 pm CEST -//! Last Modified: Tue 04 Jun 2019 10:06:53 pm CEST +//! File: settings.rs +//! Description: Defines a stuct that holds all of the configuration settings passed to the program +//! by line. +//! Author: Jali +//! Created On: Tue 04 Jun 2019 10:30:46 pm CEST +//! Last Modified: TIMESTAMP -pub mod configuration; +use clap::ArgMatches; +use std::str::FromStr; + +/** + * Stores the configuration needed for the server. + */ +pub struct Settings { + pub foreground: bool, + pub listen: String, + pub plugins: Vec, + pub port: i32, +} + +impl Settings { + /** + * # Description + * + * Creates a new instance of a Settings object either from + * the command line, or the default configuration. + * + * # Returns + * + * A new ```Settings``` object + * + * # Exanples + * + * ```rust + * let settings = Settings::create(); + * ``` + */ + pub fn create() -> Settings { + let matches = Settings::parse_command_line(); + + Settings { + foreground: matches.is_present("FOREGROUND"), + listen: matches.value_of("LISTEN").unwrap_or("default").to_owned(), + plugins: matches.value_of("PLUGIN").unwrap_or("filesystem").split(",").map(|s| s.to_string()).collect(), + port: FromStr::from_str(matches.value_of("PORT").unwrap_or("70")).unwrap() + } + } + + + /** + * # Description + * + * Parses the command line and returns all matches that fit the known + * options + * + * # returns + * A collection of matches. + */ + fn parse_command_line<'a>() -> ArgMatches<'a> { + // Read the configuration from the command line + clap_app!(myapp => + (version: "0.1") + (author: "jali@orca-central.de") + (about: "A simple modular gopher server, that pushes converted web-content to a client") + (@arg FOREGROUND: -f --foreground "Keeps the service in the foreground") + (@arg LISTEN: -l --listen +takes_value "Specifies the interface to listn on. Is either a hostname or an address-pattern.") + (@arg PLUGIN: -u --plugins +takes_value +required "A comma separated list of plugins to load.") + (@arg PORT: -p --port +takes_value "Specifies the port the server listens on. Default is 70") + ) + .get_matches() + } +} diff --git a/src/configuration/settings.rs b/src/configuration/settings.rs deleted file mode 100644 index 8475caf..0000000 --- a/src/configuration/settings.rs +++ /dev/null @@ -1,15 +0,0 @@ -//! File: settings.rs -//! Description: Defines a stuct that holds all of the configuration settings passed to the program -//! by command line. -//! Author: Jali -//! Created On: Tue 04 Jun 2019 10:30:46 pm CEST -//! Last Modified: TIMESTAMP - -/** - * Defines a data stucture to to store the configuration in. - */ -pub struct settings { - Foreground: bool, - Plugins: Vec, - Port: i32, -} diff --git a/src/main.rs b/src/main.rs index d7dbba0..322d39a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,57 +1,20 @@ +#[macro_use] extern crate clap; +mod configuration; -use clap::{App, Arg, ArgMatches}; +use crate::configuration::Settings; /** * Initiates an instance of the gopherbridge server and * starts listening for connections on the given port. */ fn main() { - let matches = parse_command_line(); - let pluginlist = matches.value_of("PLUGIN"); - let port = matches.value_of("PORT"); + let settings = Settings::create(); - match port { - None => println!("Default port 70 used"), - Some(p) => println!("Using port {}", p), - } - - match pluginlist { - None => println!("No plugins given"), - Some(p) => println!("Loading the following plugins: {}", p), + println!("Program listens on {}", settings.listen); + println!("Loading plugins {:?}", settings.plugins); + println!("Listening on port {}", settings.port); + if settings.foreground { + println!("Server runs in the foreground"); } } - -/** - * Defines the command line parameters passed to the server and creates the - * server lines - */ -fn parse_command_line<'a>() -> ArgMatches<'a> { - return App::new("gopherbridge") - .version("0.1") - .author("jali@orca-central.de") - .about("a simple modular gopher server, that pushes converted web-content to a client") - .arg( - Arg::with_name("FORGROUND") - .short("f") - .long("foreground") - .help("Keeps the service in the foreground") - .takes_value(false), - ) - .arg( - Arg::with_name("PLUGIN") - .short("l") - .long("plugins") - .value_name("PLUGINLIST") - .takes_value(true) - .index(1), - ) - .arg( - Arg::with_name("PORT") - .short("p") - .long("port") - .help("Specifies the port the server listens on. Default is 70") - .takes_value(true), - ) - .get_matches(); -}