From 694d0a29acb4bf71928a4b06b86f4505746ee9f9 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 13 Sep 2022 21:35:52 +0200 Subject: [PATCH] Produce first output --- src/Tools/.gitignore | 4 +++- src/Tools/Tests/Planschbecken.cpp | 0 src/Tools/Tests/Test.mk | 2 +- src/Tools/cpp_out/src/lib.rs | 11 +++++++++-- src/Tools/tool_helper/src/lib.rs | 24 +++++++++++++----------- 5 files changed, 26 insertions(+), 15 deletions(-) delete mode 100644 src/Tools/Tests/Planschbecken.cpp diff --git a/src/Tools/.gitignore b/src/Tools/.gitignore index 01cb7728..bb4248c4 100644 --- a/src/Tools/.gitignore +++ b/src/Tools/.gitignore @@ -1,4 +1,6 @@ **/target Input/** Output/** -*.lock \ No newline at end of file +*.lock + +**/Tests/Test_*.cpp \ No newline at end of file diff --git a/src/Tools/Tests/Planschbecken.cpp b/src/Tools/Tests/Planschbecken.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/src/Tools/Tests/Test.mk b/src/Tools/Tests/Test.mk index a5be6ac9..31cb2004 100644 --- a/src/Tools/Tests/Test.mk +++ b/src/Tools/Tests/Test.mk @@ -2,6 +2,6 @@ export PATH := $(HOME)/.cargo/bin/:$(PATH) test_cpp_out: always @cargo build --manifest-path ../cpp_out/Cargo.toml --release - @echo "Planschbecken" | ./../cpp_out/target/release/cpp_out -o "Planschbecken.cpp" + @echo "Planschbecken" | ./../cpp_out/target/release/cpp_out -o "Test_Planschbecken.cpp" always: ; \ No newline at end of file diff --git a/src/Tools/cpp_out/src/lib.rs b/src/Tools/cpp_out/src/lib.rs index 9c0c550a..0ced3acd 100644 --- a/src/Tools/cpp_out/src/lib.rs +++ b/src/Tools/cpp_out/src/lib.rs @@ -1,6 +1,13 @@ use tool_helper::{Input, Output, Result}; pub use tool_helper::Error; -pub fn convert(_input: Input, _output: Output) -> Result<()> { - Err(Error::new(-1, "Not implemented yet".to_owned())) +fn write_cpp(_input: Input, output: &mut Output, line_feed: &str) -> Result<()> { + Error::try_with(write!(output, "const char data[] = {{{}", line_feed))?; + Error::try_with(write!(output, "}};"))?; + + Ok(()) +} + +pub fn convert(_input: Input, mut output: Output) -> Result<()> { + write_cpp(_input, &mut output, "\r\n") } \ No newline at end of file diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 4169063f..52f7ffa3 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -14,33 +14,35 @@ impl Error { Error{exit_code, text} } - pub fn from_io_result_with_code(result: std::result::Result, exit_code: Option) -> Result { + pub fn try_with_code(result: std::result::Result, exit_code: Option) -> Result where S: std::fmt::Display { match result { Ok(value) => Ok(value), - Err(error) => Err(Error::new({ - match exit_code { - Some(exit_code) => exit_code, - None => -1, - } - }, error.to_string())), + Err(error) => Err(Error::new(Self::get_exit_code(exit_code), error.to_string())), } } - pub fn from_io_result(result: std::result::Result) -> Result { - Self::from_io_result_with_code(result, None) + pub fn try_with(result: std::result::Result) -> Result where S: std::fmt::Display { + Self::try_with_code(result, None) + } + + fn get_exit_code(exit_code: Option) -> i32 { + match exit_code { + Some(exit_code) => exit_code, + None => -1, + } } } pub fn open_output(output_file: Option) -> Result { match output_file { - Some(output_path) => Ok(Box::new(Error::from_io_result(std::fs::File::create(output_path))?)), + Some(output_path) => Ok(Box::new(Error::try_with(std::fs::File::create(output_path))?)), None => Ok(Box::new(std::io::stdout())), } } pub fn open_input(input_file: Option) -> Result { match input_file { - Some(input_path) => Ok(Box::new(Error::from_io_result(std::fs::File::open(input_path))?)), + Some(input_path) => Ok(Box::new(Error::try_with(std::fs::File::open(input_path))?)), None => Ok(Box::new(std::io::stdin())), } } \ No newline at end of file