implementation in the Rust language
authorVojtěch Král <vojtech@kral.hk>
Sat, 01 Oct 2016 16:19:20 +0200
changeset 292d2eaba76d70
parent 28 bfef9f34e438
child 30 536f9116a52c
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>
.hgignore
rust/parameter-lister/Cargo.lock
rust/parameter-lister/Cargo.toml
rust/parameter-lister/src/main.rs
rust/parameter-lister/src/outputmodule.rs
rust/parameter-lister/src/terminalmodule.rs
rust/parameter-lister/src/xmlmodule.rs
rust/parameter-lister/start.sh
     1.1 --- a/.hgignore	Sun Sep 11 19:52:26 2016 +0200
     1.2 +++ b/.hgignore	Sat Oct 01 16:19:20 2016 +0200
     1.3 @@ -12,3 +12,5 @@
     1.4  ^java/[^/]+/dist/
     1.5  ^java/[^/]+/build/
     1.6  ^java/[^/]+/nbproject/private/
     1.7 +
     1.8 +^rust/parameter-lister/target/
     1.9 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/rust/parameter-lister/Cargo.lock	Sat Oct 01 16:19:20 2016 +0200
     2.3 @@ -0,0 +1,4 @@
     2.4 +[root]
     2.5 +name = "parameter-lister-rust"
     2.6 +version = "0.1.0"
     2.7 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/rust/parameter-lister/Cargo.toml	Sat Oct 01 16:19:20 2016 +0200
     3.3 @@ -0,0 +1,6 @@
     3.4 +[package]
     3.5 +name = "parameter-lister-rust"
     3.6 +version = "0.1.0"
     3.7 +authors = ["Vojtech Kral <vojtech@kral.hk>"]
     3.8 +
     3.9 +[dependencies]
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/rust/parameter-lister/src/main.rs	Sat Oct 01 16:19:20 2016 +0200
     4.3 @@ -0,0 +1,45 @@
     4.4 +/**
     4.5 + * parameter-lister (Rust implementation)
     4.6 + * Copyright © 2016 Vojtěch Král <vojtech@kral.hk>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, either version 3 of the License, or
    4.11 + * (at your option) any later version.
    4.12 + *
    4.13 + * This program is distributed in the hope that it will be useful,
    4.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    4.16 + * GNU General Public License for more details.
    4.17 + *
    4.18 + * You should have received a copy of the GNU General Public License
    4.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    4.20 + */
    4.21 +use std::collections::HashMap;
    4.22 +use std::env;
    4.23 +use std::io;
    4.24 +
    4.25 +mod outputmodule;
    4.26 +mod terminalmodule;
    4.27 +mod xmlmodule;
    4.28 +
    4.29 +use outputmodule::OutputModule;
    4.30 +use terminalmodule::TerminalModule;
    4.31 +use xmlmodule::XMLModule;
    4.32 +
    4.33 +static ENV_OUTPUT_MODULE_NAME: &'static str = "PARAMETER_LISTER_OUTPUT";
    4.34 +
    4.35 +
    4.36 +fn main() {
    4.37 +	let mut modules: HashMap<_, Box<OutputModule>> = HashMap::new();
    4.38 +	modules.insert(terminalmodule::MOD_NAME, Box::new(TerminalModule::new()));
    4.39 +	modules.insert(xmlmodule::MOD_NAME, Box::new(XMLModule::new()));
    4.40 +
    4.41 +	let module_name = env::var(ENV_OUTPUT_MODULE_NAME).unwrap_or(String::from("terminal"));
    4.42 +	let module = modules.get(&*module_name).expect(&*format!("No such module: {}", module_name));
    4.43 +
    4.44 +	match module.process(&mut io::stdout(), env::args().collect()) {
    4.45 +		Ok(()) => {},
    4.46 +		Err(msg) => panic!("Error while processing output with module {}: {}", module_name, msg),
    4.47 +	}
    4.48 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/rust/parameter-lister/src/outputmodule.rs	Sat Oct 01 16:19:20 2016 +0200
     5.3 @@ -0,0 +1,22 @@
     5.4 +/**
     5.5 + * parameter-lister (Rust implementation)
     5.6 + * Copyright © 2016 Vojtěch Král <vojtech@kral.hk>
     5.7 + *
     5.8 + * This program is free software: you can redistribute it and/or modify
     5.9 + * it under the terms of the GNU General Public License as published by
    5.10 + * the Free Software Foundation, either version 3 of the License, or
    5.11 + * (at your option) any later version.
    5.12 + *
    5.13 + * This program is distributed in the hope that it will be useful,
    5.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    5.16 + * GNU General Public License for more details.
    5.17 + *
    5.18 + * You should have received a copy of the GNU General Public License
    5.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    5.20 + */
    5.21 +pub use std::io::Write;
    5.22 +
    5.23 +pub trait OutputModule {
    5.24 +	fn process(&self, output: &mut Write, parameters: Vec<String>) -> Result<(), String>;
    5.25 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/rust/parameter-lister/src/terminalmodule.rs	Sat Oct 01 16:19:20 2016 +0200
     6.3 @@ -0,0 +1,38 @@
     6.4 +/**
     6.5 + * parameter-lister (Rust implementation)
     6.6 + * Copyright © 2016 Vojtěch Král <vojtech@kral.hk>
     6.7 + *
     6.8 + * This program is free software: you can redistribute it and/or modify
     6.9 + * it under the terms of the GNU General Public License as published by
    6.10 + * the Free Software Foundation, either version 3 of the License, or
    6.11 + * (at your option) any later version.
    6.12 + *
    6.13 + * This program is distributed in the hope that it will be useful,
    6.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    6.16 + * GNU General Public License for more details.
    6.17 + *
    6.18 + * You should have received a copy of the GNU General Public License
    6.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    6.20 + */
    6.21 +use outputmodule::*;
    6.22 +
    6.23 +pub static MOD_NAME: &'static str = "terminal";
    6.24 +
    6.25 +
    6.26 +pub struct TerminalModule;
    6.27 +
    6.28 +impl TerminalModule {
    6.29 +	pub fn new() -> TerminalModule { TerminalModule }
    6.30 +}
    6.31 +
    6.32 +impl OutputModule for TerminalModule {
    6.33 +	fn process(&self, output: &mut Write, parameters: Vec<String>) -> Result<(), String> {
    6.34 +		for p in parameters {
    6.35 +			if let Err(err) = write!(output, "{}\n", p) {
    6.36 +				return Err(format!("{}", err));
    6.37 +			}
    6.38 +		}
    6.39 +		Ok(())
    6.40 +	}
    6.41 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/rust/parameter-lister/src/xmlmodule.rs	Sat Oct 01 16:19:20 2016 +0200
     7.3 @@ -0,0 +1,58 @@
     7.4 +/**
     7.5 + * parameter-lister (Rust implementation)
     7.6 + * Copyright © 2016 Vojtěch Král <vojtech@kral.hk>
     7.7 + *
     7.8 + * This program is free software: you can redistribute it and/or modify
     7.9 + * it under the terms of the GNU General Public License as published by
    7.10 + * the Free Software Foundation, either version 3 of the License, or
    7.11 + * (at your option) any later version.
    7.12 + *
    7.13 + * This program is distributed in the hope that it will be useful,
    7.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    7.16 + * GNU General Public License for more details.
    7.17 + *
    7.18 + * You should have received a copy of the GNU General Public License
    7.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    7.20 + */
    7.21 +use std::io;
    7.22 +
    7.23 +use outputmodule::*;
    7.24 +
    7.25 +pub static MOD_NAME: &'static str = "xml";
    7.26 +
    7.27 +
    7.28 +trait XMLEscape {
    7.29 +	fn xml_escape(self) -> Self;
    7.30 +}
    7.31 +
    7.32 +impl XMLEscape for String {
    7.33 +	fn xml_escape(self) -> String {
    7.34 +		self.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
    7.35 +	}
    7.36 +}
    7.37 +
    7.38 +
    7.39 +pub struct XMLModule;
    7.40 +
    7.41 +impl XMLModule {
    7.42 +	pub fn new() -> XMLModule { XMLModule }
    7.43 +
    7.44 +	fn process_io(&self, output: &mut Write, parameters: Vec<String>) -> io::Result<()> {
    7.45 +		try!(write!(output, "<parameters>\n"));
    7.46 +		for p in parameters {
    7.47 +			try!(write!(output, "\t<parameter>{}</parameter>\n", p.xml_escape()));
    7.48 +		}
    7.49 +		try!(write!(output, "</parameters>\n"));
    7.50 +		Ok(())
    7.51 +	}
    7.52 +}
    7.53 +
    7.54 +impl OutputModule for XMLModule {
    7.55 +	fn process(&self, output: &mut Write, parameters: Vec<String>) -> Result<(), String> {
    7.56 +		match self.process_io(output, parameters) {
    7.57 +			Ok(()) => Ok(()),
    7.58 +			Err(err) => Err(format!("{}", err)),
    7.59 +		}
    7.60 +	}
    7.61 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/rust/parameter-lister/start.sh	Sat Oct 01 16:19:20 2016 +0200
     8.3 @@ -0,0 +1,15 @@
     8.4 +#!/bin/sh
     8.5 +
     8.6 +# syntax highlight:
     8.7 +# ./start.sh | pygmentize -l xml
     8.8 +
     8.9 +p1="aaa";
    8.10 +p2="first line
    8.11 +second line
    8.12 +third line";
    8.13 +p3="a & b >> OMG <<";
    8.14 +
    8.15 +export PARAMETER_LISTER_OUTPUT="xml";
    8.16 +
    8.17 +target/debug/parameter-lister-rust "$p1" "$p2" "$p3";
    8.18 +#cargo run -q "$p1" "$p2" "$p3";