//! File: settings.rs //! Description: Defines a stuct that holds all of the configuration settings passed to the program //! by line. //! Created On: Tue 04 Jun 2019 10:30:46 pm CEST //! Last Modified: Tue 01 Oct 2019 09:52:47 pm CEST //! Author: Jali 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("127.0.0.1").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() } }