rust/parameter-lister/src/main.rs
author Vojtěch Král <vojtech@kral.hk>
Sat, 01 Oct 2016 16:19:20 +0200
changeset 29 2d2eaba76d70
permissions -rw-r--r--
implementation in the Rust language
discussion and source: https://www.abclinuxu.cz/blog/xkucf03/2016/9/parametry-prikazu-roury-java-a-jine-jazyky#12
Committed by: František Kučera <franta-hg@frantovo.cz>
     1 /**
     2  * parameter-lister (Rust implementation)
     3  * Copyright © 2016 Vojtěch Král <vojtech@kral.hk>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 use std::collections::HashMap;
    19 use std::env;
    20 use std::io;
    21 
    22 mod outputmodule;
    23 mod terminalmodule;
    24 mod xmlmodule;
    25 
    26 use outputmodule::OutputModule;
    27 use terminalmodule::TerminalModule;
    28 use xmlmodule::XMLModule;
    29 
    30 static ENV_OUTPUT_MODULE_NAME: &'static str = "PARAMETER_LISTER_OUTPUT";
    31 
    32 
    33 fn main() {
    34 	let mut modules: HashMap<_, Box<OutputModule>> = HashMap::new();
    35 	modules.insert(terminalmodule::MOD_NAME, Box::new(TerminalModule::new()));
    36 	modules.insert(xmlmodule::MOD_NAME, Box::new(XMLModule::new()));
    37 
    38 	let module_name = env::var(ENV_OUTPUT_MODULE_NAME).unwrap_or(String::from("terminal"));
    39 	let module = modules.get(&*module_name).expect(&*format!("No such module: {}", module_name));
    40 
    41 	match module.process(&mut io::stdout(), env::args().collect()) {
    42 		Ok(()) => {},
    43 		Err(msg) => panic!("Error while processing output with module {}: {}", module_name, msg),
    44 	}
    45 }