# HG changeset patch # User Vojtěch Král # Date 1475331560 -7200 # Node ID 2d2eaba76d70f74fb9d0c30b936011f687f927cf # Parent bfef9f34e438642fd780fea2c47ac13384479523 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 diff -r bfef9f34e438 -r 2d2eaba76d70 .hgignore --- a/.hgignore Sun Sep 11 19:52:26 2016 +0200 +++ b/.hgignore Sat Oct 01 16:19:20 2016 +0200 @@ -12,3 +12,5 @@ ^java/[^/]+/dist/ ^java/[^/]+/build/ ^java/[^/]+/nbproject/private/ + +^rust/parameter-lister/target/ \ No newline at end of file diff -r bfef9f34e438 -r 2d2eaba76d70 rust/parameter-lister/Cargo.lock --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/parameter-lister/Cargo.lock Sat Oct 01 16:19:20 2016 +0200 @@ -0,0 +1,4 @@ +[root] +name = "parameter-lister-rust" +version = "0.1.0" + diff -r bfef9f34e438 -r 2d2eaba76d70 rust/parameter-lister/Cargo.toml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/parameter-lister/Cargo.toml Sat Oct 01 16:19:20 2016 +0200 @@ -0,0 +1,6 @@ +[package] +name = "parameter-lister-rust" +version = "0.1.0" +authors = ["Vojtech Kral "] + +[dependencies] diff -r bfef9f34e438 -r 2d2eaba76d70 rust/parameter-lister/src/main.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/parameter-lister/src/main.rs Sat Oct 01 16:19:20 2016 +0200 @@ -0,0 +1,45 @@ +/** + * parameter-lister (Rust implementation) + * Copyright © 2016 Vojtěch Král + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +use std::collections::HashMap; +use std::env; +use std::io; + +mod outputmodule; +mod terminalmodule; +mod xmlmodule; + +use outputmodule::OutputModule; +use terminalmodule::TerminalModule; +use xmlmodule::XMLModule; + +static ENV_OUTPUT_MODULE_NAME: &'static str = "PARAMETER_LISTER_OUTPUT"; + + +fn main() { + let mut modules: HashMap<_, Box> = HashMap::new(); + modules.insert(terminalmodule::MOD_NAME, Box::new(TerminalModule::new())); + modules.insert(xmlmodule::MOD_NAME, Box::new(XMLModule::new())); + + let module_name = env::var(ENV_OUTPUT_MODULE_NAME).unwrap_or(String::from("terminal")); + let module = modules.get(&*module_name).expect(&*format!("No such module: {}", module_name)); + + match module.process(&mut io::stdout(), env::args().collect()) { + Ok(()) => {}, + Err(msg) => panic!("Error while processing output with module {}: {}", module_name, msg), + } +} diff -r bfef9f34e438 -r 2d2eaba76d70 rust/parameter-lister/src/outputmodule.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/parameter-lister/src/outputmodule.rs Sat Oct 01 16:19:20 2016 +0200 @@ -0,0 +1,22 @@ +/** + * parameter-lister (Rust implementation) + * Copyright © 2016 Vojtěch Král + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +pub use std::io::Write; + +pub trait OutputModule { + fn process(&self, output: &mut Write, parameters: Vec) -> Result<(), String>; +} diff -r bfef9f34e438 -r 2d2eaba76d70 rust/parameter-lister/src/terminalmodule.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/parameter-lister/src/terminalmodule.rs Sat Oct 01 16:19:20 2016 +0200 @@ -0,0 +1,38 @@ +/** + * parameter-lister (Rust implementation) + * Copyright © 2016 Vojtěch Král + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +use outputmodule::*; + +pub static MOD_NAME: &'static str = "terminal"; + + +pub struct TerminalModule; + +impl TerminalModule { + pub fn new() -> TerminalModule { TerminalModule } +} + +impl OutputModule for TerminalModule { + fn process(&self, output: &mut Write, parameters: Vec) -> Result<(), String> { + for p in parameters { + if let Err(err) = write!(output, "{}\n", p) { + return Err(format!("{}", err)); + } + } + Ok(()) + } +} diff -r bfef9f34e438 -r 2d2eaba76d70 rust/parameter-lister/src/xmlmodule.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/parameter-lister/src/xmlmodule.rs Sat Oct 01 16:19:20 2016 +0200 @@ -0,0 +1,58 @@ +/** + * parameter-lister (Rust implementation) + * Copyright © 2016 Vojtěch Král + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +use std::io; + +use outputmodule::*; + +pub static MOD_NAME: &'static str = "xml"; + + +trait XMLEscape { + fn xml_escape(self) -> Self; +} + +impl XMLEscape for String { + fn xml_escape(self) -> String { + self.replace("&", "&").replace("<", "<").replace(">", ">") + } +} + + +pub struct XMLModule; + +impl XMLModule { + pub fn new() -> XMLModule { XMLModule } + + fn process_io(&self, output: &mut Write, parameters: Vec) -> io::Result<()> { + try!(write!(output, "\n")); + for p in parameters { + try!(write!(output, "\t{}\n", p.xml_escape())); + } + try!(write!(output, "\n")); + Ok(()) + } +} + +impl OutputModule for XMLModule { + fn process(&self, output: &mut Write, parameters: Vec) -> Result<(), String> { + match self.process_io(output, parameters) { + Ok(()) => Ok(()), + Err(err) => Err(format!("{}", err)), + } + } +} diff -r bfef9f34e438 -r 2d2eaba76d70 rust/parameter-lister/start.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/parameter-lister/start.sh Sat Oct 01 16:19:20 2016 +0200 @@ -0,0 +1,15 @@ +#!/bin/sh + +# syntax highlight: +# ./start.sh | pygmentize -l xml + +p1="aaa"; +p2="first line +second line +third line"; +p3="a & b >> OMG <<"; + +export PARAMETER_LISTER_OUTPUT="xml"; + +target/debug/parameter-lister-rust "$p1" "$p2" "$p3"; +#cargo run -q "$p1" "$p2" "$p3";