From e79df423b00f9982d278cd41a96872c965bd00f4 Mon Sep 17 00:00:00 2001 From: jaby Date: Mon, 19 Sep 2022 21:06:44 +0200 Subject: [PATCH] Improve Error --- src/Tools/cpp_out/src/lib.rs | 5 +- src/Tools/cpp_out/src/main.rs | 12 +-- .../src/images/reduced_tim/mod.rs | 2 +- src/Tools/jaby_engine_fconv/src/main.rs | 5 +- src/Tools/tool_helper/src/lib.rs | 77 ++++++++++++------- 5 files changed, 64 insertions(+), 37 deletions(-) diff --git a/src/Tools/cpp_out/src/lib.rs b/src/Tools/cpp_out/src/lib.rs index 81e4cfa2..7db1f67f 100644 --- a/src/Tools/cpp_out/src/lib.rs +++ b/src/Tools/cpp_out/src/lib.rs @@ -92,11 +92,12 @@ pub fn convert(cfg: Configuration, input: Input, mut output: Output) -> Result<( } }; + let action = String::from("Writing source file"); if is_source_file { - Error::try_or_new(write_source_file(input, &mut output, declarations, cfg.data_name, line_feed), None) + Error::try_or_new_with_action(action, write_source_file(input, &mut output, declarations, cfg.data_name, line_feed)) } else { - Error::try_or_new(write_header_file(input, &mut output, cfg.file_name, declarations, cfg.data_name, line_feed), None) + Error::try_or_new_with_action(action, write_header_file(input, &mut output, cfg.file_name, declarations, cfg.data_name, line_feed)) } } \ No newline at end of file diff --git a/src/Tools/cpp_out/src/main.rs b/src/Tools/cpp_out/src/main.rs index 3a2e62dd..e4c8ffc4 100644 --- a/src/Tools/cpp_out/src/main.rs +++ b/src/Tools/cpp_out/src/main.rs @@ -15,9 +15,9 @@ struct CommandLine { output_file: PathBuf, } -fn configurate(cmd: &mut CommandLine) -> tool_helper::Result { +fn configurate(cmd: &mut CommandLine) -> Result { let file_name = tool_helper::get_file_name_from_path_buf(&cmd.output_file, "output")?; - let extension = tool_helper::os_str_to_string(Error::ok_or_new(cmd.output_file.extension(), ||"File extension required for output".to_owned(), None)?, "extension")?.to_ascii_lowercase(); + let extension = tool_helper::os_str_to_string(Error::ok_or_new(cmd.output_file.extension(), ||"File extension required for output".to_owned())?, "extension")?.to_ascii_lowercase(); let file_type = Error::ok_or_new({ match extension.as_ref() { "h" => Some(FileType::CHeader), @@ -26,12 +26,12 @@ fn configurate(cmd: &mut CommandLine) -> tool_helper::Result { "cpp" => Some(FileType::CPPSource), _ => None } - }, ||format!("{} unkown file extension", extension), None)?; + }, ||format!("{} unkown file extension", extension))?; Ok(Configuration{file_name, data_name: std::mem::take(&mut cmd.data_name), line_feed: cpp_out::LineFeed::Windows, file_type}) } -fn run_main() -> tool_helper::Result<()> { +fn run_main() -> Result<(), Error> { match CommandLine::try_parse() { Ok(mut cmd) => { let cfg = configurate(&mut cmd)?; @@ -40,7 +40,7 @@ fn run_main() -> tool_helper::Result<()> { return cpp_out::convert(cfg, input, output); }, - Err(error) => Err(tool_helper::Error::new(error.to_string(), None)) + Err(error) => Err(tool_helper::Error::from_error(error)) } } @@ -48,7 +48,7 @@ fn main() { match run_main() { Ok(_) => (), Err(error) => { - eprintln!("{}", error.text); + eprintln!("{}", error); std::process::exit(error.exit_code); } } diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs index dbed16cc..5ed6b0a4 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs @@ -19,5 +19,5 @@ pub struct Arguments { pub fn convert(_args: Arguments, input: Input, _output: Output) -> Result<(), Error> { ImageReader::new(Cursor::new(tool_helper::input_to_vec(input)?)); - Err(Error::new("Convert not implemented yet".to_owned(), Some(-2))) + Err(Error::not_implemented("convert")) } \ No newline at end of file diff --git a/src/Tools/jaby_engine_fconv/src/main.rs b/src/Tools/jaby_engine_fconv/src/main.rs index a5ad76b2..98bb6036 100644 --- a/src/Tools/jaby_engine_fconv/src/main.rs +++ b/src/Tools/jaby_engine_fconv/src/main.rs @@ -1,6 +1,7 @@ use jaby_engine_fconv::images::{*}; use clap::{Parser, Subcommand}; use std::path::PathBuf; +use tool_helper::Error; #[derive(Parser)] #[clap(about = "Converts files to various JabyEngine related file formats", long_about = None)] @@ -20,7 +21,7 @@ enum SubCommands { SimpleTIM(reduced_tim::Arguments) } -fn run_main() -> tool_helper::Result<()> { +fn run_main() -> Result<(), Error> { match CommandLine::try_parse() { Ok(cmd) => { let input = tool_helper::open_input(cmd.input_file)?; @@ -30,7 +31,7 @@ fn run_main() -> tool_helper::Result<()> { SubCommands::SimpleTIM(args) => reduced_tim::convert(args, input, output), } }, - Err(error) => Err(tool_helper::Error::new(error.to_string(), None)) + Err(error) => Err(Error::from_error(error)) } } diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 8678bfe6..73075aa0 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -2,64 +2,89 @@ use std::{boxed::Box, io::{Read, Write}, path::PathBuf}; pub type Output = Box; pub type Input = Box; -pub type Result = std::result::Result; pub struct Error { - pub exit_code: i32, - pub text: String, + pub exit_code: i32, + pub action: String, + pub text: String, } impl Error { - pub fn new(text: String, exit_code: Option) -> Error { - Error{exit_code: Self::get_exit_code(exit_code), text} + const DEFAULT_EXITCODE:i32 = -1; + + pub fn from_text(text: String) -> Error { + Error{exit_code: Self::DEFAULT_EXITCODE, action: String::new(), text} } - pub fn try_or_new(result: std::result::Result, exit_code: Option) -> Result where S: std::fmt::Display { + pub fn from_error(error: T) -> Error where T: std::fmt::Display { + Error::from_text(error.to_string()) + } + + pub fn not_implemented(function: &str) -> Error { + Error::from_text(format!("{} not implemented yet", function)) + } + + pub fn try_or_new(result: std::result::Result) -> Result where S: std::fmt::Display { + Self::try_or_new_with_action(String::new(), result) + } + + pub fn try_or_new_with_action(action: String, result: std::result::Result) -> Result where S: std::fmt::Display { match result { Ok(value) => Ok(value), - Err(error) => Err(Error::new(error.to_string(), exit_code)), + Err(error) => Err(Error{exit_code: Self::DEFAULT_EXITCODE, action, text: error.to_string()}), } } - pub fn ok_or_new(option: Option, error_text: F, code: Option) -> Result where F: Fn () -> String{ - Ok(option.ok_or(Error::new(error_text(), code))?) - } - - fn get_exit_code(exit_code: Option) -> i32 { - match exit_code { - Some(exit_code) => exit_code, - None => -1, - } + pub fn ok_or_new(option: Option, error_text: F) -> Result where F: Fn () -> String{ + Ok(option.ok_or(Error{exit_code: Self::DEFAULT_EXITCODE, action: String::new(), text: error_text()})?) } } -pub fn open_output(output_file: Option) -> Result { +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.action.is_empty() { + write!(f, "{}", self.text) + } + + else { + write!(f, "{}:{}", self.action, self.text) + } + } +} + +impl std::convert::From for Error { + fn from(error: std::io::Error) -> Self { + Error{exit_code: -1, action: String::new(), text: error.to_string()} + } +} + +pub fn open_output(output_file: Option) -> Result { match output_file { - Some(output_path) => Ok(Box::new(Error::try_or_new(std::fs::File::create(output_path), None)?)), + Some(output_path) => Ok(Box::new(std::fs::File::create(output_path)?)), None => Ok(Box::new(std::io::stdout())), } } -pub fn open_input(input_file: Option) -> Result { +pub fn open_input(input_file: Option) -> Result { match input_file { - Some(input_path) => Ok(Box::new(Error::try_or_new(std::fs::File::open(input_path), None)?)), + Some(input_path) => Ok(Box::new(std::fs::File::open(input_path)?)), None => Ok(Box::new(std::io::stdin())), } } -pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result { - Ok(Error::ok_or_new(input.to_str(), ||format!("Converting {} to UTF-8 failed", name), None)?.to_owned()) +pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result { + Ok(Error::ok_or_new(input.to_str(), ||format!("Converting {} to UTF-8 failed", name))?.to_owned()) } -pub fn get_file_name_from_path_buf(input: &PathBuf, name: &str) -> Result { - os_str_to_string(Error::ok_or_new(input.file_name(), ||format!("No {} file name found", name), None)?, name) +pub fn get_file_name_from_path_buf(input: &PathBuf, name: &str) -> Result { + os_str_to_string(Error::ok_or_new(input.file_name(), ||format!("No {} file name found", name))?, name) } -pub fn input_to_vec(input: Input) -> Result> { +pub fn input_to_vec(input: Input) -> Result, Error> { let mut data = Vec::new(); for byte in input.bytes() { - data.push(Error::try_or_new(byte, None)?); + data.push(byte?); } Ok(data)