From d912ea949e278bbdd9d293926e6169b145868a70 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 27 Nov 2022 22:15:30 +0100 Subject: [PATCH] Cause error on not a number padded_size value --- src/Tools/Tests/ISO_Planschbecken.xml | 2 +- .../psxcdgen_ex/src/config_reader/mod.rs | 13 +++------- .../psxcdgen_ex/src/config_reader/xml.rs | 25 ++++++++++++++++--- src/Tools/psxcdgen_ex/src/encoder/psx.rs | 1 - src/Tools/tool_helper/src/lib.rs | 13 ++++++++-- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/Tools/Tests/ISO_Planschbecken.xml b/src/Tools/Tests/ISO_Planschbecken.xml index 975c0552..9c895cd5 100644 --- a/src/Tools/Tests/ISO_Planschbecken.xml +++ b/src/Tools/Tests/ISO_Planschbecken.xml @@ -10,7 +10,7 @@ ../Tests/ISO_Planschbecken.xml diff --git a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs index 2eff102a..b635c56a 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs @@ -15,15 +15,10 @@ impl Configuration { } pub struct File { - pub name: String, - pub path: PathBuf, - pub is_hidden: bool, -} - -impl File { - pub fn new() -> File { - File{name: String::new(), path: PathBuf::new(), is_hidden: false} - } + pub name: String, + pub path: PathBuf, + pub is_hidden: bool, + pub padded_size: Option, } pub struct Directory { diff --git a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs index cbfcea8a..df2ababb 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs @@ -1,4 +1,5 @@ use std::path::PathBuf; +use tool_helper::format_if_error; use crate::config_reader::Directory; use super::{Configuration, ErrorString, File}; @@ -46,9 +47,10 @@ fn parse_description(description: roxmltree::Node, config: &mut Configuration) { fn parse_track(track: roxmltree::Node, config: &mut Configuration) -> Result<(), Error> { fn parse_file(file: roxmltree::Node, is_hidden: bool) -> Result { Ok(File{ - name: String::from(file.attribute("name").unwrap_or_default()), - path: PathBuf::from(file.text().unwrap_or_default()), - is_hidden: is_hidden | parse_boolean_attribute(&file, "hidden")? + name: String::from(file.attribute("name").unwrap_or_default()), + path: PathBuf::from(file.text().unwrap_or_default()), + is_hidden: is_hidden | parse_boolean_attribute(&file, "hidden")?, + padded_size: read_padded_size(&file)?, }) } @@ -75,6 +77,17 @@ fn parse_track(track: roxmltree::Node, config: &mut Configuration) -> Result<(), parse_file_system(track, &mut config.root, false) } +fn read_padded_size(xml: &roxmltree::Node) -> Result, Error> { + if let Some(padded_attr) = xml.attribute("padded_size") { + let padded_size = format_if_error!(padded_attr.parse::(), "Failed reading {} as padded size: {error_text}", padded_attr)?; + Ok(Some(padded_size)) + } + + else { + Ok(None) + } +} + fn parse_boolean_attribute(xml: &roxmltree::Node, attribute_name: &str) -> Result { if let Some(bool_str) = xml.attribute(attribute_name) { match bool_str { @@ -109,4 +122,10 @@ impl std::convert::From for Error { fn from(error: roxmltree::Error) -> Error { Error::XML(error) } +} + +impl std::convert::From for Error { + fn from(error: super::Error) -> Error { + Error::Generic(error) + } } \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/encoder/psx.rs b/src/Tools/psxcdgen_ex/src/encoder/psx.rs index 3edb45d0..5e7c39e5 100644 --- a/src/Tools/psxcdgen_ex/src/encoder/psx.rs +++ b/src/Tools/psxcdgen_ex/src/encoder/psx.rs @@ -286,7 +286,6 @@ fn process_directory_record(dir: &Directory, sec_writer: &mut dyn SectorWriter) raw_data = &mut raw_data[bytes_written..raw_data_len]; } - let dir_record_sectors = builder::create_xa_data_for_vec(None, &dir_record); let dir_record_sector_count = dir_record_sectors.len(); diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 630ce824..e2d79eea 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -10,12 +10,12 @@ pub type Input = Box; #[macro_export] macro_rules! format_if_error { ($result:expr, $format_text:literal) => { - tool_helper::callback_if_error($result, |error_text| { + tool_helper::callback_if_any_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| { + tool_helper::callback_if_any_error($result, |error_text| { format!($format_text, $($arg),*, error_text=error_text) }) }; @@ -139,6 +139,15 @@ pub fn callback_if_error String, T>(result: Result, c } } +pub fn callback_if_any_error String, T, E: std::string::ToString>(result: Result, callback: F) -> Result { + match result { + Ok(value) => Ok(value), + Err(error) => { + Err(Error::from_text(callback(error.to_string()))) + } + } +} + pub fn open_output_file(output_path: &PathBuf) -> Result, Error> { Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?)) }