From 94b4bd2b2f667f3b14b5efcdb20e5132acdac592 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 27 Nov 2022 22:32:14 +0100 Subject: [PATCH] Clean up error handling --- src/Tools/cpp_out/src/lib.rs | 7 +++--- .../src/images/reduced_tim/color_clut.rs | 6 ++--- src/Tools/tool_helper/src/lib.rs | 24 ++++--------------- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/Tools/cpp_out/src/lib.rs b/src/Tools/cpp_out/src/lib.rs index b7a2427a..99a7d49f 100644 --- a/src/Tools/cpp_out/src/lib.rs +++ b/src/Tools/cpp_out/src/lib.rs @@ -1,6 +1,6 @@ use tool_helper::{Input, Output}; use std::io::Read; -pub use tool_helper::Error; +pub use tool_helper::{Error, format_if_error}; pub enum LineFeed { Unix, @@ -92,12 +92,11 @@ 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_with_action(action, write_source_file(input, &mut output, declarations, cfg.data_name, line_feed)) + format_if_error!(write_source_file(input, &mut output, declarations, cfg.data_name, line_feed), "Writing source file failed with: \"{error_text}\"") } else { - Error::try_or_new_with_action(action, write_header_file(input, &mut output, cfg.file_name, declarations, cfg.data_name, line_feed)) + format_if_error!(write_header_file(input, &mut output, cfg.file_name, declarations, cfg.data_name, line_feed), "Writing header file failed with: \"{error_text}\"") } } \ No newline at end of file diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs index 1f7debb7..660e4387 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs @@ -34,13 +34,13 @@ impl IndexedImage { png::BitDepth::Four => IndexPerByte::TwoIndices, png::BitDepth::Eight => IndexPerByte::OneIndex, _ => { - return Err(Error::from_str("Only 4 and 8bit color depth are supported").with_action(action_name)); + return Err(Error::from_text(format!("{}: Only 4 and 8bit color depth are supported", action_name))); } } }; if info.color_type != png::ColorType::Indexed { - return Err(Error::from_str("PNG file must be indexed").with_action(action_name)); + return Err(Error::from_text(format!("{}: PNG file must be indexed", action_name))); } else { @@ -54,7 +54,7 @@ impl IndexedImage { } Err(err) => { - Err(Error::from_error(err).with_action(action_name)) + Err(Error::from_text(format!("{}: {}", action_name, err.to_string()))) } } } diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 9310adb4..caff774d 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -23,7 +23,6 @@ macro_rules! format_if_error { pub struct Error { pub exit_code: i32, - pub action: String, pub text: String, } @@ -35,7 +34,7 @@ impl Error { } pub fn from_text(text: String) -> Error { - Error{exit_code: Self::DEFAULT_EXITCODE, action: String::new(), text} + Error{exit_code: Self::DEFAULT_EXITCODE, text} } pub fn from_error(error: T) -> Error where T: std::fmt::Display { @@ -55,41 +54,26 @@ impl Error { } 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{exit_code: Self::DEFAULT_EXITCODE, action, text: error.to_string()}), + Err(error) => Err(Error{exit_code: Self::DEFAULT_EXITCODE, text: error.to_string()}), } } pub fn ok_or_new(option: Option, error_text: F) -> Result where F: Fn () -> String{ Ok(option.ok_or(Error::from_callback(error_text))?) } - - pub fn with_action(mut self, action: &str) -> Error { - self.action = action.to_owned(); - self - } } 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) - } + write!(f, "{}", 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()} + Error{exit_code: -1, text: error.to_string()} } }