From 2fdd4bf829ce3a50ad2b224fa30e9d66d3088b7f Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 13 Sep 2022 20:49:02 +0200 Subject: [PATCH] Prepare tool_helper --- src/Tools/Tests/Test.mk | 2 +- src/Tools/cpp_out/Cargo.toml | 1 + src/Tools/cpp_out/src/main.rs | 72 ++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/Tools/Tests/Test.mk b/src/Tools/Tests/Test.mk index ef1e1c84..a5be6ac9 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 - @./../cpp_out/target/release/cpp_out + @echo "Planschbecken" | ./../cpp_out/target/release/cpp_out -o "Planschbecken.cpp" always: ; \ No newline at end of file diff --git a/src/Tools/cpp_out/Cargo.toml b/src/Tools/cpp_out/Cargo.toml index edcc822a..8512ce3e 100644 --- a/src/Tools/cpp_out/Cargo.toml +++ b/src/Tools/cpp_out/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = {version = "*", features = ["derive"]} diff --git a/src/Tools/cpp_out/src/main.rs b/src/Tools/cpp_out/src/main.rs index 2941c2d1..0d5d09d9 100644 --- a/src/Tools/cpp_out/src/main.rs +++ b/src/Tools/cpp_out/src/main.rs @@ -1,3 +1,73 @@ +use clap::{Parser}; +use std::path::PathBuf; + +#[derive(Parser)] +#[clap(about = "Output a file content or stdin to a c++ header/source file", long_about = None)] +struct CommandLine { + #[clap(short='f', long="file")] + input_file: Option, + + #[clap(short='o')] + output_file: PathBuf, +} + +mod tool_helper { + use std::{boxed::Box, io::Write, path::PathBuf}; + + pub type Output = Box; + pub type Result = std::result::Result; + + pub struct Error { + pub exit_code: i32, + pub text: String, + } + + impl Error { + pub fn new(exit_code: i32, text: String) -> Error { + Error{exit_code, text} + } + + pub fn from_io_result_with_code(result: std::result::Result, exit_code: Option) -> Result { + match result { + Ok(value) => Ok(value), + Err(error) => Err(Error::new({ + match exit_code { + Some(exit_code) => exit_code, + None => -1, + } + }, error.to_string())), + } + } + + pub fn from_io_result(result: std::result::Result) -> Result { + Self::from_io_result_with_code(result, None) + } + } + + pub fn open_output(input_file: Option) -> Result { + match input_file { + Some(input_path) => Ok(Box::new(Error::from_io_result(std::fs::File::create(input_path))?)), + None => Ok(Box::new(std::io::stdout())), + } + } +} + +fn run_main() -> tool_helper::Result<()> { + match CommandLine::try_parse() { + Ok(cmd) => { + let _output = tool_helper::open_output(cmd.input_file)?; + return Ok(()); + }, + Err(error) => Err(tool_helper::Error::new(-1, error.to_string())) + } +} + fn main() { - println!("Planschbecken!"); + match run_main() { + Ok(_) => println!("All good"), + Err(error) => { + eprintln!("{}", error.text); + std::process::exit(error.exit_code); + } + } } \ No newline at end of file