Cause error on not a number padded_size value

This commit is contained in:
jaby 2022-11-27 22:15:30 +01:00
parent d5c9d16bb5
commit 0d3686b7d0
5 changed files with 38 additions and 16 deletions

View File

@ -10,7 +10,7 @@
<Directory name="Wuff">
<File name="Miau.txt" type="file">../Tests/ISO_Planschbecken.xml</File>
<Directory name="Sub" hidden="true">
<File name="SubM.txt" type="file">../Tests/ISO_Planschbecken.xml</File>
<File name="SubM.txt" type="file" padded_size="planschi">../Tests/ISO_Planschbecken.xml</File>
</Directory>
</Directory>
</Track>

View File

@ -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<usize>,
}
pub struct Directory {

View File

@ -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<File, Error> {
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<Option<usize>, Error> {
if let Some(padded_attr) = xml.attribute("padded_size") {
let padded_size = format_if_error!(padded_attr.parse::<usize>(), "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<bool, Error> {
if let Some(bool_str) = xml.attribute(attribute_name) {
match bool_str {
@ -109,4 +122,10 @@ impl std::convert::From<roxmltree::Error> for Error {
fn from(error: roxmltree::Error) -> Error {
Error::XML(error)
}
}
impl std::convert::From<super::Error> for Error {
fn from(error: super::Error) -> Error {
Error::Generic(error)
}
}

View File

@ -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();

View File

@ -10,12 +10,12 @@ pub type Input = Box<dyn Read>;
#[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<F: Fn(String) -> String, T>(result: Result<T, Error>, c
}
}
pub fn callback_if_any_error<F: Fn(String) -> String, T, E: std::string::ToString>(result: Result<T, E>, callback: F) -> Result<T, Error> {
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<BufWriter<std::fs::File>, Error> {
Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?))
}