From f101645af16e42ffb0574b46a5dc321c3594ad7d Mon Sep 17 00:00:00 2001 From: Jaby Date: Sat, 17 Sep 2022 14:31:20 +0200 Subject: [PATCH] Change name of variable --- src/Tools/Tests/Test.mk | 2 +- src/Tools/cpp_out/src/lib.rs | 20 ++++++++++---------- src/Tools/cpp_out/src/main.rs | 11 +++++++---- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Tools/Tests/Test.mk b/src/Tools/Tests/Test.mk index 31cb2004..59eafe2e 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 "Test_Planschbecken.cpp" + @echo "Planschbecken" | ./../cpp_out/target/release/cpp_out --name Dino -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 f4109f32..dd09b9af 100644 --- a/src/Tools/cpp_out/src/lib.rs +++ b/src/Tools/cpp_out/src/lib.rs @@ -1,4 +1,4 @@ -use tool_helper::{Input, Output, Result}; +use tool_helper::{Input, Output}; use std::io::Read; pub use tool_helper::Error; @@ -32,31 +32,31 @@ const WINDOWS_LINEFEED: &'static str = "\r\n"; const C_VARIABLES: FileVariables = FileVariables{include: "", var_type: "char"}; const CPP_VARIABLES: FileVariables = FileVariables{include: "#include ", var_type: "uint8_t"}; -fn write_source_file(variables: &FileVariables, input: Input, output: &mut Output, line_feed: &str) -> Result<()> { - Error::try_or_new(write!(output, "{}{}{}", variables.include, line_feed, line_feed), None)?; - Error::try_or_new(write!(output, "const {} data[] = {{{}", variables.var_type, line_feed), None)?; +fn write_source_file(input: Input, output: &mut Output, variables: &FileVariables, data_name: String, line_feed: &str) -> Result<(), std::io::Error> { + write!(output, "{}{}{}", variables.include, line_feed, line_feed)?; + write!(output, "const {} {}[] = {{{}", variables.var_type, data_name, line_feed)?; let mut byte_line_count = 1; let mut bytes = input.bytes(); if let Some(byte) = bytes.next() { - Error::try_or_new(write!(output, "\t{:#04X}", Error::try_or_new(byte, None)?), None)?; + write!(output, "\t{:#04X}", byte?)?; for byte in bytes { - Error::try_or_new(write!(output, ", {:#04X}", Error::try_or_new(byte, None)?), None)?; + write!(output, ", {:#04X}", byte?)?; byte_line_count += 1; if byte_line_count >= 16 { - Error::try_or_new(write!(output, "{}\t", line_feed), None)?; + write!(output, "{}\t", line_feed)?; byte_line_count = 0; } } } - Error::try_or_new(write!(output, "{}}};", line_feed), None)?; + write!(output, "{}}};", line_feed)?; Ok(()) } -pub fn convert(cfg: Configuration, input: Input, mut output: Output) -> Result<()> { +pub fn convert(cfg: Configuration, input: Input, mut output: Output) -> Result<(), Error> { let line_feed = { match cfg.line_feed { LineFeed::Windows => WINDOWS_LINEFEED, @@ -71,5 +71,5 @@ pub fn convert(cfg: Configuration, input: Input, mut output: Output) -> Result<( } }; - write_source_file(source_variables, input, &mut output, line_feed) + Error::try_or_new(write_source_file(input, &mut output, source_variables, cfg.data_name, line_feed), None) } \ No newline at end of file diff --git a/src/Tools/cpp_out/src/main.rs b/src/Tools/cpp_out/src/main.rs index ad09cf63..5340f6a1 100644 --- a/src/Tools/cpp_out/src/main.rs +++ b/src/Tools/cpp_out/src/main.rs @@ -8,11 +8,14 @@ struct CommandLine { #[clap(short='f', long="file")] input_file: Option, + #[clap(short='n', long="name")] + data_name: String, + #[clap(short='o')] output_file: PathBuf, } -fn configurate(cmd: &CommandLine) -> tool_helper::Result { +fn configurate(cmd: &mut CommandLine) -> tool_helper::Result { let file_name = tool_helper::get_file_name_from_path_buf(&cmd.output_file, "output")?; let extension = tool_helper::os_str_to_string(Error::ok_or_new(cmd.output_file.extension(), ||"File extension required for output".to_owned(), None)?, "extension")?.to_ascii_lowercase(); let file_type = Error::ok_or_new({ @@ -25,13 +28,13 @@ fn configurate(cmd: &CommandLine) -> tool_helper::Result { } }, ||format!("{} unkown file extension", extension), None)?; - Ok(Configuration{file_name, data_name: "Planschbecken".to_owned(), line_feed: cpp_out::LineFeed::Windows, file_type}) + Ok(Configuration{file_name, data_name: std::mem::take(&mut cmd.data_name), line_feed: cpp_out::LineFeed::Windows, file_type}) } fn run_main() -> tool_helper::Result<()> { match CommandLine::try_parse() { - Ok(cmd) => { - let cfg = configurate(&cmd)?; + Ok(mut cmd) => { + let cfg = configurate(&mut cmd)?; let input = tool_helper::open_input(cmd.input_file)?; let output = tool_helper::open_output(Some(cmd.output_file))?;