Create tool_helper library

This commit is contained in:
jaby 2022-09-13 21:06:41 +02:00
parent cdb9a8a2b7
commit e42b3ab6cf
6 changed files with 51 additions and 43 deletions

View File

@ -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"
},
{

View File

@ -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
)

View File

@ -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"}

View File

@ -11,47 +11,6 @@ struct CommandLine {
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) => {

View File

@ -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]

View File

@ -0,0 +1,38 @@
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())),
}
}