From 910e8ede11764141c0d90e36e710118670d5b06a Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 16 Nov 2022 04:36:25 +0100 Subject: [PATCH] Improve creation of error messages --- src/Tools/psxcdgen_ex/src/encoder/psx.rs | 6 ++-- src/Tools/tool_helper/src/lib.rs | 38 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Tools/psxcdgen_ex/src/encoder/psx.rs b/src/Tools/psxcdgen_ex/src/encoder/psx.rs index 385e81b6..9fa0e726 100644 --- a/src/Tools/psxcdgen_ex/src/encoder/psx.rs +++ b/src/Tools/psxcdgen_ex/src/encoder/psx.rs @@ -2,7 +2,7 @@ use super::{*, SectorWriter, {CDDesc, Error}}; use super::super::types::{helper::{DirectoryRecordMember, PathTableMember}, layout::Layout, *}; use builder::SubModeBuilder; use cdtypes::types::{cdstring::{AString, DString}, date::*, dir_record::*, helper::{round_bytes_mode2_form1, sector_count_mode2_form1}, path_table::*, pvd as cd_pvd, lsb_msb::*, sector::Mode2Form1}; -use tool_helper::{BufferedInputFile, open_input_file_buffered}; +use tool_helper::{BufferedInputFile, construct_from_error_if as format_if_error, open_input_file_buffered}; use std::io::{Read, Seek, SeekFrom}; const ROOT_DIR_NAME:&'static str = "\x00"; @@ -159,8 +159,8 @@ fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWrit return Err(Error::from_text(format!("System Area required to start at sector 0 of Track - found LBA: {}", system_area_lba))); } - if let Some(license_file) = &system_area.license_file_path { - let license_file = open_input_file_buffered(license_file)?; + if let Some(license_path) = &system_area.license_file_path { + let license_file = format_if_error!(open_input_file_buffered(license_path), "Loading license file from {} failed with: {error_text}", license_path.to_string_lossy())?; write_license_file(sec_writer, license_file) } diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 68976a72..134d2d71 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -7,6 +7,20 @@ pub type BufferedInputFile = BufReader; pub type Output = Box; pub type Input = Box; +#[macro_export] +macro_rules! construct_from_error_if { + ($result:expr, $format_text:literal) => { + tool_helper::callback_if_error($result, |error_text| { + format!($format_text, error_text=error_text) + }) + }; + ($result:expr, $format_text:literal, $($arg:expr)*) => { + tool_helper::callback_if_error($result, |error_text| { + format!($format_text, $($arg),*, error_text=error_text) + }) + }; +} + pub trait ErrorString { fn to_string(self) -> String; } @@ -101,6 +115,30 @@ impl std::convert::From for Error { } } +pub fn prefix_if_error(prefix: &str, result: Result) -> Result { + match result { + Ok(value) => Ok(value), + Err(mut error) => { + let mut new_text = String::from(prefix); + + new_text.push_str(error.text.as_str()); + error.text = new_text; + Err(error) + }, + } +} + +pub fn callback_if_error String, T>(result: Result, callback: F) -> Result { + match result { + Ok(value) => Ok(value), + Err(mut error) => { + error.text = callback(error.text); + + Err(error) + } + } +} + pub fn open_output_file(output_path: &PathBuf) -> Result, Error> { Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?)) }