Prepare tool_helper
This commit is contained in:
parent
b13b4e85c7
commit
2fdd4bf829
|
@ -2,6 +2,6 @@ export PATH := $(HOME)/.cargo/bin/:$(PATH)
|
||||||
|
|
||||||
test_cpp_out: always
|
test_cpp_out: always
|
||||||
@cargo build --manifest-path ../cpp_out/Cargo.toml --release
|
@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: ;
|
always: ;
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = {version = "*", features = ["derive"]}
|
||||||
|
|
|
@ -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<PathBuf>,
|
||||||
|
|
||||||
|
#[clap(short='o')]
|
||||||
|
output_file: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
mod tool_helper {
|
||||||
|
use std::{boxed::Box, io::Write, path::PathBuf};
|
||||||
|
|
||||||
|
pub type Output = Box<dyn Write>;
|
||||||
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
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<T>(result: std::result::Result<T, std::io::Error>, exit_code: Option<i32>) -> Result<T> {
|
||||||
|
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<T>(result: std::result::Result<T, std::io::Error>) -> Result<T> {
|
||||||
|
Self::from_io_result_with_code(result, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open_output(input_file: Option<PathBuf>) -> Result<Output> {
|
||||||
|
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() {
|
fn main() {
|
||||||
println!("Planschbecken!");
|
match run_main() {
|
||||||
|
Ok(_) => println!("All good"),
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("{}", error.text);
|
||||||
|
std::process::exit(error.exit_code);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue