Clean up error handling

This commit is contained in:
Jaby 2022-11-27 22:32:14 +01:00 committed by Jaby
parent a386ba6a06
commit 94b4bd2b2f
3 changed files with 10 additions and 27 deletions

View File

@ -1,6 +1,6 @@
use tool_helper::{Input, Output}; use tool_helper::{Input, Output};
use std::io::Read; use std::io::Read;
pub use tool_helper::Error; pub use tool_helper::{Error, format_if_error};
pub enum LineFeed { pub enum LineFeed {
Unix, 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 { 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 { 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}\"")
} }
} }

View File

@ -34,13 +34,13 @@ impl IndexedImage {
png::BitDepth::Four => IndexPerByte::TwoIndices, png::BitDepth::Four => IndexPerByte::TwoIndices,
png::BitDepth::Eight => IndexPerByte::OneIndex, 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 { 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 { else {
@ -54,7 +54,7 @@ impl IndexedImage {
} }
Err(err) => { Err(err) => {
Err(Error::from_error(err).with_action(action_name)) Err(Error::from_text(format!("{}: {}", action_name, err.to_string())))
} }
} }
} }

View File

@ -23,7 +23,6 @@ macro_rules! format_if_error {
pub struct Error { pub struct Error {
pub exit_code: i32, pub exit_code: i32,
pub action: String,
pub text: String, pub text: String,
} }
@ -35,7 +34,7 @@ impl Error {
} }
pub fn from_text(text: String) -> 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<T>(error: T) -> Error where T: std::fmt::Display { pub fn from_error<T>(error: T) -> Error where T: std::fmt::Display {
@ -55,41 +54,26 @@ impl Error {
} }
pub fn try_or_new<T, S>(result: std::result::Result<T, S>) -> Result<T, Error> where S: std::fmt::Display { pub fn try_or_new<T, S>(result: std::result::Result<T, S>) -> Result<T, Error> where S: std::fmt::Display {
Self::try_or_new_with_action(String::new(), result)
}
pub fn try_or_new_with_action<T, S>(action: String, result: std::result::Result<T, S>) -> Result<T, Error> where S: std::fmt::Display {
match result { match result {
Ok(value) => Ok(value), 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<T, F>(option: Option<T>, error_text: F) -> Result<T, Error> where F: Fn () -> String{ pub fn ok_or_new<T, F>(option: Option<T>, error_text: F) -> Result<T, Error> where F: Fn () -> String{
Ok(option.ok_or(Error::from_callback(error_text))?) 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 { impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.action.is_empty() { write!(f, "{}", self.text)
write!(f, "{}", self.text)
}
else {
write!(f, "{}:{}", self.action, self.text)
}
} }
} }
impl std::convert::From<std::io::Error> for Error { impl std::convert::From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self { 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()}
} }
} }