Create tool_helper library
This commit is contained in:
parent
a32b12e05a
commit
390bb1773a
|
@ -47,7 +47,7 @@
|
||||||
{
|
{
|
||||||
"id": "project",
|
"id": "project",
|
||||||
"type": "pickString",
|
"type": "pickString",
|
||||||
"options": ["cdtypes", "cpp_out", "psxcdgen", "psxcdread"],
|
"options": ["cdtypes", "cpp_out", "psxcdgen", "psxcdread", "tool_helper"],
|
||||||
"description": "project to build"
|
"description": "project to build"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,6 +4,7 @@ rem build_all [clean]
|
||||||
set bin_projects=psxcdgen psxcdread
|
set bin_projects=psxcdgen psxcdread
|
||||||
set bin_linux_projects=cpp_out
|
set bin_linux_projects=cpp_out
|
||||||
set clean_projects=cdtypes
|
set clean_projects=cdtypes
|
||||||
|
set clean_projects_linux=tool_helper
|
||||||
|
|
||||||
set projects=%bin_projects%
|
set projects=%bin_projects%
|
||||||
set linux_projects=%bin_linux_projects%
|
set linux_projects=%bin_linux_projects%
|
||||||
|
@ -14,6 +15,7 @@ IF NOT "%~1" == "" (
|
||||||
IF "%~1" == "clean" (
|
IF "%~1" == "clean" (
|
||||||
set build_type=clean
|
set build_type=clean
|
||||||
set projects=%projects% %clean_projects%
|
set projects=%projects% %clean_projects%
|
||||||
|
set linux_projects=%linux_projects% %clean_projects_linux%
|
||||||
) ELSE (
|
) ELSE (
|
||||||
set build_type=%~1
|
set build_type=%~1
|
||||||
)
|
)
|
||||||
|
|
|
@ -6,4 +6,5 @@ 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"]}
|
clap = {version = "*", features = ["derive"]}
|
||||||
|
tool_helper = {path = "../tool_helper"}
|
||||||
|
|
|
@ -11,47 +11,6 @@ struct CommandLine {
|
||||||
output_file: PathBuf,
|
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<()> {
|
fn run_main() -> tool_helper::Result<()> {
|
||||||
match CommandLine::try_parse() {
|
match CommandLine::try_parse() {
|
||||||
Ok(cmd) => {
|
Ok(cmd) => {
|
||||||
|
|
|
@ -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]
|
|
@ -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())),
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue