From 390bb1773a83105a7f7bab3bebdab0010943f418 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 13 Sep 2022 21:06:41 +0200 Subject: [PATCH] Create tool_helper library --- src/Tools/Tools.code-workspace | 2 +- src/Tools/build_all.bat | 2 ++ src/Tools/cpp_out/Cargo.toml | 3 ++- src/Tools/cpp_out/src/main.rs | 41 -------------------------------- src/Tools/tool_helper/Cargo.toml | 8 +++++++ src/Tools/tool_helper/src/lib.rs | 38 +++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 43 deletions(-) create mode 100644 src/Tools/tool_helper/Cargo.toml create mode 100644 src/Tools/tool_helper/src/lib.rs diff --git a/src/Tools/Tools.code-workspace b/src/Tools/Tools.code-workspace index 40a493f8..af473291 100644 --- a/src/Tools/Tools.code-workspace +++ b/src/Tools/Tools.code-workspace @@ -47,7 +47,7 @@ { "id": "project", "type": "pickString", - "options": ["cdtypes", "cpp_out", "psxcdgen", "psxcdread"], + "options": ["cdtypes", "cpp_out", "psxcdgen", "psxcdread", "tool_helper"], "description": "project to build" }, { diff --git a/src/Tools/build_all.bat b/src/Tools/build_all.bat index 44acb4b4..9e85ba8a 100644 --- a/src/Tools/build_all.bat +++ b/src/Tools/build_all.bat @@ -4,6 +4,7 @@ rem build_all [clean] set bin_projects=psxcdgen psxcdread set bin_linux_projects=cpp_out set clean_projects=cdtypes +set clean_projects_linux=tool_helper set projects=%bin_projects% set linux_projects=%bin_linux_projects% @@ -14,6 +15,7 @@ IF NOT "%~1" == "" ( IF "%~1" == "clean" ( set build_type=clean set projects=%projects% %clean_projects% + set linux_projects=%linux_projects% %clean_projects_linux% ) ELSE ( set build_type=%~1 ) diff --git a/src/Tools/cpp_out/Cargo.toml b/src/Tools/cpp_out/Cargo.toml index 8512ce3e..8377e2c3 100644 --- a/src/Tools/cpp_out/Cargo.toml +++ b/src/Tools/cpp_out/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = {version = "*", features = ["derive"]} +clap = {version = "*", features = ["derive"]} +tool_helper = {path = "../tool_helper"} diff --git a/src/Tools/cpp_out/src/main.rs b/src/Tools/cpp_out/src/main.rs index 0d5d09d9..5dda9caf 100644 --- a/src/Tools/cpp_out/src/main.rs +++ b/src/Tools/cpp_out/src/main.rs @@ -11,47 +11,6 @@ struct CommandLine { 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) => { diff --git a/src/Tools/tool_helper/Cargo.toml b/src/Tools/tool_helper/Cargo.toml new file mode 100644 index 00000000..cd579ff2 --- /dev/null +++ b/src/Tools/tool_helper/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "tool_helper" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs new file mode 100644 index 00000000..a161a427 --- /dev/null +++ b/src/Tools/tool_helper/src/lib.rs @@ -0,0 +1,38 @@ +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())), + } +} \ No newline at end of file